djiango控制语句

时间:2023-03-09 04:56:57
djiango控制语句
{#    从0开始的索引#}
{% for foo in value %}
{# 从0开始的索引#}
<p>{{ forloop.counter0 }}: {{ foo }}</p>
{# 是否第一次循环#}
<p>{{ forloop.first }}</p>
{# 是否最后一次循环#}
<p>{{ forloop.last }}</p>
{#如果列表为空,则走以下内容#}
{% empty %}
<p>There are no athletes. Only computer programmers.</p> {# <p>{{ forloop.parentloop }}</p>#} {% endfor %} {# 从1开始的索引#}
{% for foo in value %}
<p>{{ forloop.counter }}: {{ foo }}</p> {% endfor %} {# 索引倒叙#}
{% for foo in value %}
<p>{{ forloop.revcounter0 }}: {{ foo }}</p> {% endfor %}

for循环

------{%csrf_token%}:csrf_token标签

for表单中, 用于生成csrf_token的标签,用于防治跨站攻击验证。注意如果你在view的index里用的是render_to_response方法,不会生效

其实,这里是会生成一个input标签,和其他表单标签一起提交给后台的。

------{% url "别名"%}:路由配置

re_path(r'pay/login/', views.login, name="James")
<form action={% url "James" %} method="post">
<input type="text" name="userName">
<input type="password" name="password">
<input type="submit" name="submit"> ------{% with %}:用更简单的变量名替代复杂的变量名
{% with total=fhjsaldfhjsdfhlasdfhljsdal %} {{ total }} {% endwith %} ------{% verbatim %}: 禁止render  

  {% verbatim %}
  {{ hello }}
  {% endverbatim %}

------自定义 filter 和simple_tag

filter可以用在if等语句后,simple_tag不可以
-------------------------------.html
{% load xxx %} #首行 # num=12
{{ num|filter_multi:2 }} #24 {{ num|filter_multi:"[22,333,4444]" }} {% simple_tag_multi 2 5 %} 参数不限,但不能放在if for语句中
{% simple_tag_multi num 5 %}