Hey Mr. Stack overflow! I am trying to have different information display in the same block depending on a variable "choice" which is simply an int. The way I was planning on doing so was going to be something like the bellow code:
{% extends "index.html"%}
{%block head%}
<p><h1>Welcome to Piss && ink {{user}}</h1></p>
{%endblock head%}
{%block one%}
<p>The temperature in {{city}} is {{temperature}}° </p>
{%endblock one%}
{%if choice1 == 2 %}
{%block two%}
<p>The temperature in {{city}} is {{temperature}}° </p>
{%endblock two%}
{% endif %}
{%comment%}
{%if choice1 == 2 %}
{%block two%}
<p>The temperature in {{city}} is {{temperature}}° </p>
{%endblock%}
{% endif %}
{%endcomment%}
{%block two%}
<form method="post">
{%csrf_token%}
{% if new_event %}
<b><p开发者_开发问答>{{new_event}}</p></b>
{% endif %}
{%endblock%}
Now, the problem I am having is that the template doesn't like that there are two blocks of the same name in the template. For some reason it doesn't seem to care about the {% if %}
statement that is checking where the {% block %}
is supposed to go. I thought that the {% if %}
statement would only execute what was inside itself depending on its parameters but it doesn't seem to be doing that. It displays everything inside the {% if %}
no matter what "choice1" is equal too :( Does anyone have any idea how I might be able to fix this? Thanks
Put the logic inside the block instead of having two blocks of the same name.
Instead of:
{%if choice1 == 2 %}
{%block two%}
<p>The temperature in {{city}} is {{temperature}}° </p>
{%endblock two%}
{% endif %}
{%comment%}
{%if choice1 == 2 %}
{%block two%}
<p>The temperature in {{city}} is {{temperature}}° </p>
{%endblock%}
{% endif %}
{%endcomment%}
{%block two%}
<form method="post">
{%csrf_token%}
{% if new_event %}
<b><p>{{new_event}}</p></b>
{% endif %}
{%endblock%}
use:
{% block two %}
{% if choice1 == 2 %}
<p>The temperature in {{city}} is {{temperature}}° </p>
{% else %}
<form method="post">
{%csrf_token%}
{% if new_event %}
<b><p>{{new_event}}</p></b>
{% endif %}
{% endif %}
{% endblock %}
Put the if inside the block. One block, two if statements
{% block two %}
{% if choice == 1 %}
<p>Some Content</p>
{% endif %}
{% if choice == 2 %}
<p>Other Content</p>
{% endif %
{% endblock two %}
Another way to do this (if the templates differ a lot) would be to do something along the lines of:
{% extends choice_template %}
and set choice_template in the view.
精彩评论