Django: loading another template on button click

I've been working on a django project for a couple of weeks now, just playing around so I can hang it up. I am a bit confused. I have a template now called "home.html". I was wondering if anyway I have another template named "profile.html" that will be set as a link to the home.html template? I have a button that, when clicked, should output directly to the profile.html page, but when I did my research on this topic, I got mixed answers. Some people stated that they created the view and loaded the html using javascript, while others stated that it would be easier to just write the path to the second page template in tags.

What is the most correct way to do this?

I went through this link: Rendering another template with a click button in Django to try and get a better understanding as this was another question that came closest to what I asked, but it confused me even more since django lingua somewhat confusing to me as a beginner.

Thanks in advance for any help you can provide.


EDIT . To summarize: I'm on the homepage now. There is a button that says "View Profile". After clicking this button, I want to leave the home page and load the new "profile.html" page onto the screen.

+3


source to share


2 answers


The "best way" is to create a view that will load your "profile.html" template. I suggest using generic TemplateView

.

Add this line to your urls.py :

from django.views.generic import TemplateView

urlpatterns = 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>

      

+3


source


Instead of using TemplateView, you can also just

<a href="{% url 'profile' %}">View your profile</a>

      

in the home template. In your main urls.py project, just add your profile template url.



url_patterns = [
        url(r'profile/',profile_view,name="profile")
]

      

Function defined as profile_view in views.py. The link will work then :)

+2


source







All Articles