Python Django模板语言变量用法
我正在尝试在我的网页上显示表单。模板文件中负责显示表单的部分如下所示:Python Django模板语言变量用法,python,django,Python,Django,我正在尝试在我的网页上显示表单。模板文件中负责显示表单的部分如下所示: {% for choice in poll.choice_set.all %} <input type="radio" name="choice" id="choice{{ forloop.counter }}" value="{{ choice.id }}" /> {% count = 0 %} {% if count == 0 %} <label class="right" for="choi
{% for choice in poll.choice_set.all %}
<input type="radio" name="choice" id="choice{{ forloop.counter }}" value="{{ choice.id }}" />
{% count = 0 %}
{% if count == 0 %}
<label class="right" for="choice{{ forloop.counter }}">{{ choice.choice_text }}</label><br />
{% count += 1 %}
{% elif count == 1 %}
<label class="left" for="choice{{ forloop.counter }}">{{ choice.choice_text }}</label><br />
{% endif %}
{% endfor %}
{%用于poll.choice\u set.all%}
{%count=0%}
{%如果计数=0%}
{{choice.choice_text}}
{%count+=1%}
{%elif count==1%}
{{choice.choice_text}}
{%endif%}
{%endfor%}
因此,您可能会注意到,我希望在poll.choice\u set.all
中有两个选择。我的目标是以不同的方式显示两者(正如标签类所示)。因此,我尝试这样做的方式是声明一个计数器,它最初以一种格式显示一个选项,然后递增计数器,然后以另一种格式显示第二个选项,因为count
是递增的
我非常肯定这是完全错误的,不能这样做。我想知道是否有人能帮助我理解如何实现我想做的事情
所以基本上我有一个民意调查对象,有两个选择。我希望每个选项都以不同的方式显示(不同的CSS类)。在这种情况下,您可以使用forloop.counter或forloop.counter0,因为假设的“choice”变量在每次循环迭代中都会更改一次。你可以简化程序
{% for choice in poll.choice_set.all %}
<input type="radio" name="choice" id="choice{{ forloop.counter }}" value="{{ choice.id }}" />
{% if forloop.counter0 == 0 %}
<label class="right" for="choice{{ forloop.counter }}">{{ choice.choice_text }}</label><br />
{% elif forloop.counter0 == 1 %}
<label class="left" for="choice{{ forloop.counter }}">{{ choice.choice_text }}</label><br />
{% endif %}
{% endfor %}
{%用于poll.choice\u set.all%}
{%if-forloop.counter0==0%}
{{choice.choice_text}}
{%elif forloop.counter0==1%}
{{choice.choice_text}}
{%endif%}
{%endfor%}
或者,如果您想要更短的代码,您可以这样做:
{% for choice in poll.choice_set.all %}
<input type="radio" name="choice" id="choice{{ forloop.counter }}" value="{{ choice.id }}" />
<label class="{% cycle 'right' 'left' %}" for="choice{{ forloop.counter }}">{{ choice.choice_text }}</label><br />
{% endfor %}
{%用于poll.choice\u set.all%}
{{choice.choice_text}}
{%endfor%}
{%cycle“right”和“left”%}将在每次遇到该标记时在“right”和“left”之间切换作为类值,因此如果您有两个以上的选项,则会产生在“right”和“left”之间切换的副作用。在这种特殊情况下,您可以使用forloop.counter或forloop.counter0,因为假设的“choice”变量在每次循环迭代中都会更改一次。你可以简化程序
{% for choice in poll.choice_set.all %}
<input type="radio" name="choice" id="choice{{ forloop.counter }}" value="{{ choice.id }}" />
{% if forloop.counter0 == 0 %}
<label class="right" for="choice{{ forloop.counter }}">{{ choice.choice_text }}</label><br />
{% elif forloop.counter0 == 1 %}
<label class="left" for="choice{{ forloop.counter }}">{{ choice.choice_text }}</label><br />
{% endif %}
{% endfor %}
{%用于poll.choice\u set.all%}
{%if-forloop.counter0==0%}
{{choice.choice_text}}
{%elif forloop.counter0==1%}
{{choice.choice_text}}
{%endif%}
{%endfor%}
或者,如果您想要更短的代码,您可以这样做:
{% for choice in poll.choice_set.all %}
<input type="radio" name="choice" id="choice{{ forloop.counter }}" value="{{ choice.id }}" />
<label class="{% cycle 'right' 'left' %}" for="choice{{ forloop.counter }}">{{ choice.choice_text }}</label><br />
{% endfor %}
{%用于poll.choice\u set.all%}
{{choice.choice_text}}
{%endfor%}
{%cycle“right”“left”%}将在每次遇到该标记时在“right”和“left”之间切换作为类值,因此,如果有两个以上的选项,则会产生在“right”和“left”之间切换的副作用。Django模板不允许变量赋值。换句话说,
count+=1
永远不会起作用。这里有一个相关的例子。@almostflan谢谢,这正是我需要知道的。最后我使用了forloop.counter
,一切都很好。Django的功能太多了,文档太大了。。。即使是基础知识的一小部分也很难掌握(对于初学者来说)。我想只是需要练习。Django模板不允许变量赋值。换句话说,count+=1
永远不会起作用。这里有一个相关的例子。@almostflan谢谢,这正是我需要知道的。最后我使用了forloop.counter
,一切都很好。Django的功能太多了,文档太大了。。。即使是基础知识的一小部分也很难掌握(对于初学者来说)。我想只是需要练习。太好了,谢谢。我同意你的第一个建议。实际上,我注意到正在使用forloop.counter
,并很快发现我可以根据我的场景调整它。你的回答再次证实了这一点。谢谢,太好了,谢谢。我同意你的第一个建议。实际上,我注意到正在使用forloop.counter
,并很快发现我可以根据我的场景调整它。你的回答再次证实了这一点。谢谢