Django: Loading another template on click of a button

2024/10/2 1:31:44

I've been working on a django project for a few weeks now, just playing around so that I can get the hang of it. I am a little bit confused. I have a template now called "home.html". I was wondering if there is anyway for me to have another template called "profile.html" to be set as a link on the home.html template? I have a button that when clicked, should take me directly to the profile.html page, but as I was doing research on this topic, I was getting mixed answers. Some people stated to create a view and have that load the html using javascript, while others stated that just writing the path to the 2nd page template in tags would be easier.

What is the most correct way of getting this done?

I looked at this link: Rendering another template from a button click in Django to try to get a better understanding, as it was the one other question that was closest to what I was asking, but it confused me even more as the django lingo is somewhat confusing for me to understand as a beginner.

Thank you in advance for any help you can provide.


EDIT: To sum it up: I am on the home page currently. There is a button that says "View your profile". Once that button is clicked, I want to leave the home page and have the new page "profile.html" to be loaded on the screen.

Answer

"Most correct way" is to create view which will load your "profile.html" template. I suggest to use generic TemplateView.

Add this line to your urls.py:

from django.views.generic import TemplateViewurlpatterns = patterns('',url(r'^profile/', TemplateView.as_view(template_name='profile.html'),name='profile'),
)

And in home.html use this snippet:

<a href="{% url 'profile' %}">Profile</a>
https://en.xdnf.cn/q/70906.html

Related Q&A

Given two python lists of same length. How to return the best matches of similar values?

Given are two python lists with strings in them (names of persons):list_1 = [J. Payne, George Bush, Billy Idol, M Stuart, Luc van den Bergen] list_2 = [John Payne, George W. Bush, Billy Idol, M. Stuart…

Extracting Javascript gettext messages using Babel CLI extractor

It is stated here that Babel can extract gettext messages for Python and Javascript files.Babel comes with a few builtin extractors: python (which extractsmessages from Python source files), javascript…

Getting TTFB (time till first byte) for an HTTP Request

Here is a python script that loads a url and captures response time:import urllib2 import timeopener = urllib2.build_opener() request = urllib2.Request(http://example.com)start = time.time() resp = ope…

accessing kubernetes python api through a pod

so I need to connect to the python kubernetes client through a pod. Ive been trying to use config.load_incluster_config(), basically following the example from here. However its throwing these errors. …

Understanding DictVectorizer in scikit-learn?

Im exploring the different feature extraction classes that scikit-learn provides. Reading the documentation I did not understand very well what DictVectorizer can be used for? Other questions come to …

Parsing RSS with Elementtree in Python

How do you search for namespace-specific tags in XML using Elementtree in Python?I have an XML/RSS document like:<?xml version="1.0" encoding="UTF-8"?> <rss version=&quo…

String module object has no attribute join

So, I want to create a user text input box in Pygame, and I was told to look at a class module called inputbox. So I downloaded inputbox.py and imported into my main game file. I then ran a function in…

TypeError: the JSON object must be str, not Response with Python 3.4

Im getting this error and I cant figure out what the problem is:Traceback (most recent call last):File "C:/Python34/Scripts/ddg.py", line 8, in <module>data = json.loads(r)File "C:…

Redirect while passing message in django

Im trying to run a redirect after I check to see if the user_settings exist for a user (if they dont exist - the user is taken to the form to input and save them).I want to redirect the user to the app…

Django sorting by date(day)

I want to sort models by day first and then by score, meaning Id like to see the the highest scoring Articles in each day. class Article(models.Model):date_modified = models.DateTimeField(blank=True, n…