7

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.

4
  • to understand your question you want a link to another page using another template? or you want to load a second template inside the current page? Commented Jan 9, 2015 at 16:08
  • @BelowtheRadar I want to click a button and for a completely new page to load, the new page being a template. Is that more clear? Apologies for the confusion. Commented Jan 9, 2015 at 16:11
  • I can't believe you've seen any answers which say you can write the path to the template directly. That wouldn't work at all. Commented Jan 9, 2015 at 16:16
  • @DanielRoseman Yes, haha, after a few trial and error attempts, it finally sunk in that it wasn't the way to go. Commented Jan 9, 2015 at 16:20

3 Answers 3

6

"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 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>
Sign up to request clarification or add additional context in comments.

9 Comments

Hmm, it's returning: Invalid block tag: ''profile'', expected 'empty' or 'endfor'
Where you get this message - at home page or at "/profile/" url? Show the code of the corresponding template.
It's saying I'm getting the message in the profile.html file on line 6 of the gist: gist.github.com/anonymous/bbc495f6b256112a263c
You should write {% url 'profile' %} instead of just {% 'profiles' %}
Awesome! It worked. One last question just to make sure that I understand. Why is it that I didn't have to add anything to the views.py file in order to get this to work?
|
3

Instead of using TemplateView, you can also just have

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

in your home template. In your project's main urls.py just add url of your profile template.

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

Have a function defined as profile_view in views.py. The link will work then :)

Comments

0

Use TemplateView, you can get load a template without doing much.

In your project's main urls.py, add the following.

from django.urls import path
from django.views.generic import TemplateView


url_patterns = [
        path('index/', TemplateView.as_view(template_name='index.html'),
                        name='index'),
]

Then in your template direct the href like this:

<a href="{% url 'index' %}">Your homepage</a>

the url will work like a charm now.

hope this helps :)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.