django内置模板标签大全

django 标签

{% firstof var1 var2 var3 %}

{% for athlete in athlete_list %}
    <li>{{ athlete.name }}</li>
{% endfor %}
{% for x, y in points %}
    There is a point at {{ x }},{{ y }}
{% endfor %}
{% for key, value in data.items %}
    {{ key }}: {{ value }}
{% endfor %}
forloop.counter
forloop.counter0
forloop.revcounter
forloop.revcounter0
forloop.first
forloop.last
forloop.parentloop
{% for athlete in athlete_list %}
    <li>{{ athlete.name }}</li>
{% empty %}
    <li>Sorry, no athletes in this list.</li>
{% endfor %}


{% filter force_escape|lower %}
    This text will be HTML-escaped, and will appear in all lowercase.
{% endfilter %}

{% extends "./my/base3.html" %}



{% if athlete_list %}
    Number of athletes: {{ athlete_list|length }}
{% elif athlete_in_locker_room_list %}
    Athletes should be out of the locker room soon!
{% else %}
    No athletes.
{% endif %}

{% if athlete_list and coach_list %}
{% if not athlete_list %}
{% if athlete_list or coach_list %}
{% if not athlete_list or coach_list %}
{% if athlete_list and not coach_list %}
the operators ==, !=, <, >, <=, >=, in, not in, is

# 跟过滤器的组合使用
{% if messages|length >= 100 %}
   You have lots of messages today!
{% endif %}

# 加载自定义标签
% load somelibrary package.otherlibrary %}
# 引入模板
{% include "name_snippet.html" %}
#比较标签 ifequal and ifnotequal
{% ifequal a b %} ... {% endifequal %}
# 检查值是否发生改变 ifchanged
{% for date in days %}
    {% ifchanged %}<h3>{{ date|date:"F" }}</h3>{% endifchanged %}
    <a href="{{ date|date:"M/d"|lower }}/">{{ date|date:"j" }}</a>
{% endfor %}


# 当前时间
{% now "jS F Y H:i" %}

# 关于随机生成的模板
{% lorem [count] [method] [random] %}
count	A number (or variable) containing the number of paragraphs or words to generate (default is 1).
method	Either w for words, p for HTML paragraphs or b for plain-text paragraph blocks (default is b).
random	The word random, which if given, does not use the common paragraph (“Lorem ipsum dolor sit amet…”) when generating text.


# 对数据进行自动分类展示 regroup
{% regroup cities by country as country_list %}
<ul>
{% for country in country_list %}
    <li>{{ country.grouper }}
    <ul>
        {% for city in country.list %}
          <li>{{ city.name }}: {{ city.population }}</li>
        {% endfor %}
    </ul>
    </li>
{% endfor %}
</ul>

# resetcycle 重新遍历


# 移除标签中的空白
{% spaceless %}
    <p>
        <a href="foo/">Foo</a>
    </p>
{% endspaceless %}


# url
{% url 'some-url-name' v1 v2 %}

# templatetag
{% templatetag openblock %} url 'entry_list' {% templatetag closeblock %}
openblock	{%
closeblock	%}
openvariable	{{
closevariable	}}
openbrace	{
closebrace	}
opencomment	{#
closecomment	#}

# 普遍用在js模板中,避免跟django冲突
{% verbatim %}
    {{if dying}}Still alive.{{/if}}
{% endverbatim %}

% verbatim myblock %}
    Avoid template rendering via the {% verbatim %}{% endverbatim %} block.
{% endverbatim myblock %}

# 获取最大值
<img src="bar.png" alt="Bar"
     height="10" width="{% widthratio this_value max_value max_width %}">

# 获取比较后的变量
{% widthratio this_value max_value max_width as width %}
{% blocktrans %}The width is: {{ width }}{% endblocktrans %}

# with对值进行变量命名
{% with total=business.employees.count %}
    {{ total }} employee{{ total|pluralize }}
{% endwith %}




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值