vendredi 14 novembre 2014

python - Django balise d'inclusion ne pas montrant des données - Stack Overflow


I'n trying to create inclusion tag so I can display data in my navigation bar on every page. The tag will be included in the "base.html" so that way it should display everywhere.


tags.py


@register.inclusion_tag('menu.html')
def show_hoods(HoodList):
gethoods = Hood.objects.all()
return {'gethoods': gethoods}

menu.html


{% for hood in gethoods %}
<h3>{{ hood.name }}</h3>
{% endfor %}

For some reason the menu.html template is blank and is not showing any data.


Also, once I have the menu.html running, will simple {% include 'menu.html' %} work inside the base.html? Will that be automatically rendered?


Edit: Based on the feedback below, the code above is correct, however the base.html code was incorrect as the inclusion_tag is not loaded with {% include %} but {% load %} is used instead.


corrected base.html


{% load tags %}
{% show_hoods hoodlist %}

Thanks for the feedback!




Directly viewing the menu.html template will not display anything as it has no context variables set. gethoods will be empty so there will be nothing for the for loop in the template to loop over.


One of the main purposes of an include tag is to set extra context variables and then render a template using these variables. Directly viewing the template will show the template without the variables, but including the include template ({$ show_hood %} in your case) will add the context variables (gethoods) and render the template using them.


Answering your second question, you add include templates using their name, (the name of the function by default) rather than the {% include %} tag. The {% include %} tag is for when you simply want to render one template inside of another, and where it either doesn't need any context variables or uses the context variables available to its parent template.



I'n trying to create inclusion tag so I can display data in my navigation bar on every page. The tag will be included in the "base.html" so that way it should display everywhere.


tags.py


@register.inclusion_tag('menu.html')
def show_hoods(HoodList):
gethoods = Hood.objects.all()
return {'gethoods': gethoods}

menu.html


{% for hood in gethoods %}
<h3>{{ hood.name }}</h3>
{% endfor %}

For some reason the menu.html template is blank and is not showing any data.


Also, once I have the menu.html running, will simple {% include 'menu.html' %} work inside the base.html? Will that be automatically rendered?


Edit: Based on the feedback below, the code above is correct, however the base.html code was incorrect as the inclusion_tag is not loaded with {% include %} but {% load %} is used instead.


corrected base.html


{% load tags %}
{% show_hoods hoodlist %}

Thanks for the feedback!



Directly viewing the menu.html template will not display anything as it has no context variables set. gethoods will be empty so there will be nothing for the for loop in the template to loop over.


One of the main purposes of an include tag is to set extra context variables and then render a template using these variables. Directly viewing the template will show the template without the variables, but including the include template ({$ show_hood %} in your case) will add the context variables (gethoods) and render the template using them.


Answering your second question, you add include templates using their name, (the name of the function by default) rather than the {% include %} tag. The {% include %} tag is for when you simply want to render one template inside of another, and where it either doesn't need any context variables or uses the context variables available to its parent template.


0 commentaires:

Enregistrer un commentaire