简体   繁体   中英

Passing variables into extends template?

I have a template named base.html . As the name suggests, it is where the header and footer reside. In between these 2 elements, is a {% block content %} where the child template can extend this template and add content inside block content.

However, inside header I want the user's name to be shown. For example, {{ user.username }} but Django can't seem to recognize this when I extend this template to a child template. Is there a way I can pass in objects to the extends template? That way the logged in user's name is shown?

This is a rough example of what I'm trying to do. user.username does not show even when the user is logged in.

base.html

<header>
  <h1>Hello, {{ user.username }}</h1>
</header>
{% block content %}{% endblock %}
<footer>
 ///Some content
</footer>

child.html

{% extends 'base.html' %}

{% block content %}
 //Some content
{% endblock %}

views.py for child.html

ChildView(TemplateView):
  template_name = 'child.html'

In your child template add this to the top

{% extends 'base.html' %}

This will allow you to "inherit" the context variable.

Alternatively, if you only want to pass say just the user data to the template, you could do the following in your base.html

{% include 'header.html' with my_user=user %}

This answer summarises the differences between extend and include functionality quite well.


Edit :

In response to your comments and updated question, you aren't accessing the user object correctly. To do so you must use {{ request.user }} . This is because there is a context processor that passes the user object to every template .

As an aside, if you were to explicitly send the user from the view, you could then access the user with {{ user }} as you have done. However, this is obviously quite unnecessary.

That is because the content in blocks in child templates are overriden.

base.html

{% block my_block %}
This content is overriden by child templates
{% endblock my_block %}

child.html

{% extends 'base.html' %}

{% block my_block %}
This content is shown
{% endblock my_block %}

If you want some content shown in all templates, you shouldn't put inside a block content, but directly in your base template.

base.html

{{ user.username }}
{% block my_block %}
This content is overriden by child templates
{% endblock my_block %}

So, it all comes down to how the layout of your page is done. If the header is always the same, you should not use the block tag.

If it's almost the same, but changes in details, use blocks to change the details:

header :

<h1>This doesn't change ever 
    {% block this_changes %}
     the child themplate will provide the content
    {% endblock this_changes %}</h1>

   <b>User: {{ user.username }}</b>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM