The Django Book

Appendix F: Built-in Template Tags and Filters

闄勫綍F 鍐呭缓鐨勬ā鏉挎爣绛惧拰杩囨护鍣

Chapter 4 lists a number of the most useful built-in template tags and filters. However, Django ships with many more built-in tags and filters. This appendix lists the ones that were included at the time this book was written, but new tags get added fairly regularly.

绗洓绔犲垪鍑轰簡璁稿鐨勫父鐢ㄥ唴寤烘ā鏉挎爣绛惧拰杩囨护鍣ㄣ傜劧鑰岋紝Django鑷甫浜嗘洿澶氱殑鍐呭缓妯℃澘鏍囩鍙婅繃婊ゅ櫒銆 杩欑珷闄勫綍鍒楀嚭浜嗘埅姝㈠埌缂栧啓鏈功鏃讹紝Django鎵鍖呭惈鐨勫悇涓唴寤烘ā鏉挎爣绛惧拰杩囨护鍣紝浣嗘槸锛屾柊鐨勬爣绛炬槸浼氳瀹氭湡鍦板姞鍏ョ殑銆

The best reference to all the available tags and filters is directly in your admin interface. Djangos admin interface includes a complete reference of all tags and filters available for a given site. To see it, go to your admin interface and click the Documentation link at the upper right of the page.

瀵逛簬鎻愪緵鐨勬爣绛惧拰杩囨护鍣紝鏈濂界殑鍙傝冨氨鏄洿鎺ヨ繘鍏ヤ綘鐨勭鐞嗙晫闈€侱jango鐨勭鐞嗙晫闈㈠寘鍚簡涓浠介拡瀵瑰綋鍓嶇珯鐐圭殑鎵鏈夋爣绛惧拰杩囨护鍣ㄧ殑瀹屾暣鍙傝冦傛兂鐪嬪埌瀹冪殑璇濓紝杩涘叆浣犵殑绠$悊鐣岄潰锛屽崟鍑诲彸涓婅鐨凞ocumentation锛堟枃妗o級閾炬帴銆

The tags and filters sections of the built-in documentation describe all the built-in tags (in fact, the tag and filter references in this appendix come directly from those pages) as well as any custom tag libraries available.

鍐呭缓鏂囨。涓殑鈥滄爣绛惧拰杩囨护鍣ㄢ濆皬鑺傞槓杩颁簡鎵鏈夊唴寤烘爣绛撅紙浜嬪疄涓婏紝鏈檮褰曚腑鐨勬爣绛惧拰杩囨护鍣ㄥ弬鑰冮兘鐩存帴鏉ヨ嚜閭i噷锛夊拰鎵鏈夊彲鐢ㄧ殑瀹氬埗鏍囩搴撱

For those without an admin site available, reference for the stock tags and filters follows. Because Django is highly customizable, the reference in your admin site should be considered the final word on the available tags and filters and what they do.

鑰冭檻鍒伴偅浜涙病鏈夌鐞嗙珯鐐瑰彲鐢ㄧ殑浜轰滑锛岃繖閲屾彁渚涗簡甯哥敤鐨勬爣绛惧拰杩囨护鍣ㄧ殑鍙傝冦傜敱浜嶥jango鏄彲楂樺害瀹氬埗鐨勶紝绠$悊鐣岄潰涓殑閭d簺鍙敤鐨勬爣绛惧拰杩囨护鍣ㄧ殑鍙傝冨彲璁や负鏄渶鍙俊鐨勩

Built-in Tag Reference

鍐呭缓鏍囩鍙傝

block

block

Defines a block that can be overridden by child templates. See the section on template inheritance in Chapter 4 for more information.

瀹氫箟涓涓兘琚瓙妯℃澘瑕嗙洊鐨勫尯鍧椼傚弬瑙佺鍥涚珷鈥滄ā鏉跨户鎵库濅竴鑺傛煡鐪嬫洿澶氫俊鎭

comment

comment

Ignores everything between {% comment %} and {% endcomment %} .

妯℃澘寮曟搸浼氬拷鐣ユ帀 {% comment %}{% endcomment %} 涔嬮棿鐨勬墍鏈夊唴瀹广

cycle

cycle

Cycles among the given strings each time this tag is encountered.

杞祦浣跨敤鏍囩缁欏嚭鐨勫瓧绗︿覆鍒楄〃涓殑鍊笺

Within a loop, it cycles among the given strings each time through the loop:

鍦ㄤ竴涓惊鐜唴锛岃疆娴佷娇鐢ㄧ粰瀹氱殑瀛楃涓插垪琛ㄥ厓绱狅細

{% for o in some_list %}
    <tr class="{% cycle row1,row2 %}">
        ...
    </tr>
{% endfor %}

Outside of a loop, give the values a unique name the first time you call it, and then use that name each successive time through:

鍦ㄥ惊鐜锛屽湪浣犵涓娆¤皟鐢ㄦ椂锛岀粰杩欎簺瀛楃涓插煎畾涔変竴涓笉閲嶅鐨勫悕瀛楋紝浠ュ悗姣忔鍙渶浣跨敤杩欎釜鍚嶅瓧灏辫浜嗭細

<tr class="{% cycle row1,row2,row3 as rowcolors %}">...</tr>
<tr class="{% cycle rowcolors %}">...</tr>
<tr class="{% cycle rowcolors %}">...</tr>

You can use any number of values, separated by commas. Make sure not to put spaces between the values only commas.

浣犲彲浠ヤ娇鐢ㄤ换鎰忔暟閲忕殑鐢ㄩ楀彿鍒嗛殧鐨勫笺傛敞鎰忎笉瑕佸湪鍊间笌鍊间箣闂存湁绌烘牸锛屽彧鏄竴涓楀彿銆

debug

debug

Outputs a whole load of debugging information, including the current context and imported modules.

杈撳嚭瀹屾暣鐨勮皟璇曚俊鎭紝鍖呮嫭褰撳墠鐨勪笂涓嬫枃鍙婂鍏ョ殑妯″潡淇℃伅銆

extends

extends

Signals that this template extends a parent template.

鏍囪褰撳墠妯℃澘鎵╁睍涓涓埗妯℃澘銆

This tag can be used in two ways:

杩欎釜鏍囩鏈変袱绉嶇敤娉曪細

  • {% extends "base.html" %} (with quotes) uses the literal value "base.html" as the name of the parent template to extend.

  • {% extends "base.html" %} 锛堝甫寮曞彿锛 鐩存帴浣跨敤瑕佹墿灞曠殑鐖舵ā鏉跨殑鍚嶅瓧 "base.html"

  • {% extends variable %} uses the value of variable . If the variable evaluates to a string, Django will use that string as the name of the parent template. If the variable evaluates to a Template object, Django will use that object as the parent template.

  • {% extends variable %} 鐢ㄥ彉閲 variable 鐨勫兼潵鎸囧畾鐖舵ā鏉裤傚鏋滃彉閲忔槸涓涓瓧绗︿覆锛孌jango浼氭妸瀛楃涓茬殑鍊煎綋浣滅埗妯℃澘鐨勬枃浠跺悕銆傚鏋滃彉閲忔槸涓涓 Template 瀵硅薄锛孌jango浼氭妸杩欎釜瀵硅薄褰撲綔鐖舵ā鏉裤

See Chapter 4 for many usage examples.

鍙傜湅绗洓绔犳洿澶氬簲鐢ㄥ疄渚嬨

filter

filter

Filters the contents of the variable through variable filters.

閫氳繃鍙彉杩囨护鍣ㄨ繃婊ゅ彉閲忕殑鍐呭銆

Filters can also be piped through each other, and they can have arguments just like in variable syntax.

杩囨护鍣ㄤ篃鍙互鐩镐簰浼犺緭锛屽畠浠篃鍙互鏈夊弬鏁帮紝灏卞儚鍙橀噺鐨勮娉曚竴鏍枫

Heres a sample usage:

鐪嬭繖涓敤娉曞疄渚嬶細

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

firstof

firstof

Outputs the first variable passed that is not False . Outputs nothing if all the passed variables are False .

杈撳嚭浼犲叆鐨勭涓涓笉鏄 False 鐨勫彉閲忥紝濡傛灉琚紶閫掑彉閲忛兘涓 False 锛屽垯浠涔堜篃涓嶈緭鍑恒

Heres a sample usage:

鐪嬭繖涓敤娉曞疄渚嬶細

{% firstof var1 var2 var3 %}

This is equivalent to the following:

杩欑瓑鍚屼簬濡備笅鍐呭锛

{% if var1 %}
    {{ var1 }}
{% else %}{% if var2 %}
    {{ var2 }}
{% else %}{% if var3 %}
    {{ var3 }}
{% endif %}{% endif %}{% endif %}

for

for

Loops over each item in an array. For example, to display a list of athletes given athlete_list :

杞鏁扮粍涓殑姣忎竴鍏冪礌銆備緥濡傛樉绀轰竴涓寚瀹氱殑杩愬姩鍛樼殑搴忓垪 athlete_list

<ul>
{% for athlete in athlete_list %}
    <li>{{ athlete.name }}</li>
{% endfor %}
</ul>

You can also loop over a list in reverse by using {% for obj in list reversed %} .

浣犱篃鍙互閫嗗悜閬嶅巻涓涓垪琛 {% for obj in list reversed %}

The for loop sets a number of variables available within the loop (see Table F-1).

for 寰幆璁剧疆浜嗚澶氬惊鐜腑鍙敤鐨勫彉閲忥紙瑙佽〃F-1锛夈

Table F-1. Variables Available Inside {% for %}Loops
Variable Description
forloop.counter The current iteration of the loop (1-indexed).
forloop.counter0 The current iteration of the loop (0-indexed).
forloop.revcounter The number of iterations from the end of the loop (1-indexed).
forloop.revcounter0 The number of iterations from the end of the loop (0-indexed).
forloop.first True if this is the first time through the loop.
forloop.last True if this is the last time through the loop.
forloop.parentloop For nested loops, this is the loop above the current one.
琛‵-1. {% for %}寰幆涓殑鍙敤鍙橀噺
鍙橀噺鍚 鎻忚堪
forloop.counter 褰撳墠寰幆娆℃暟锛堢储寮曟渶灏忎负1锛夈
forloop.counter0 褰撳墠寰幆娆℃暟 (绱㈠紩鏈灏忎负0)銆
forloop.revcounter 鍓╀綑寰幆娆℃暟 (绱㈠紩鏈灏忎负1)銆
forloop.revcounter0 鍓╀綑寰幆娆℃暟 (绱㈠紩鏈灏忎负0)銆
forloop.first 绗竴娆″惊鐜椂涓 True
forloop.last 鏈鍚庝竴娆″惊鐜椂涓 True
forloop.parentloop 鐢ㄤ簬宓屽寰幆锛岃〃绀哄綋鍓嶅惊鐜灞傜殑寰幆銆

if

if

The {% if %} tag evaluates a variable, and if that variable is true (i.e., it exists, is not empty, and is not a false Boolean value), the contents of the block are output:

{% if %} 鏍囩娴嬭瘯涓涓彉閲忥紝鑻ュ彉閲忎负鐪燂紙鍗冲叾瀛樺湪銆侀潪绌猴紝涓斾笉鏄竴涓负鍋囩殑甯冨皵鍊硷級锛屽尯鍧椾腑鐨勫唴瀹瑰氨浼氳杈撳嚭锛

{% if athlete_list %}
    Number of athletes: {{ athlete_list|length }}
{% else %}
    No athletes.
{% endif %}

If athlete_list is not empty, the number of athletes will be displayed by the {{ athlete_list|length }} variable.

athlete_list 闈炵┖锛屽彉閲 {{ athlete_list|length }} 灏变細鏄剧ず杩愬姩鍛樼殑鏁伴噺銆

As you can see, the if tag can take an optional {% else %} clause that will be displayed if the test fails.

姝e浣犳墍瑙侊紝 if 鏍囩鏈夊彲閫夌殑 {% else %} 浠庡彞锛岃嫢鏉′欢涓嶆垚绔嬪垯鏄剧ず璇ヤ粠鍙ャ

if tags may use and , or , or not to test a number of variables or to negate a given variable:

if 璇彞鍙娇鐢 andornot 鏉ユ祴璇曞彉閲忔垨鑰呭缁欏畾鐨勫彉閲忓彇鍙嶏細

{% if athlete_list and coach_list %}
    Both athletes and coaches are available.
{% endif %}

{% if not athlete_list %}
    There are no athletes.
{% endif %}

{% if athlete_list or coach_list %}
    There are some athletes or some coaches.
{% endif %}

{% if not athlete_list or coach_list %}
    There are no athletes or there are some coaches (OK, so
    writing English translations of Boolean logic sounds
    stupid; it's not our fault).
{% endif %}

{% if athlete_list and not coach_list %}
    There are some athletes and absolutely no coaches.
{% endif %}

if tags dont allow and and or clauses within the same tag, because the order of logic would be ambiguous. For example, this is invalid:

涓嶅厑璁 andor 鍚屾椂鍑虹幇鍦ㄤ竴涓 if 璇彞涓紝鍥犱负杩欐牱浼氭湁閫昏緫涓婄殑闂銆備緥濡傝繖鏍锋槸鏈夎鐥呯殑锛

{% if athlete_list and coach_list or cheerleader_list %}

If you need to combine and and or to do advanced logic, just use nested if tags, for example:

濡傛灉浣犻渶瑕佸悓鏃朵娇鐢 andor 鏉ュ疄鐜拌緝楂樼骇鐨勯昏緫锛屽彲浠ョ敤宓屽鐨 if 鏍囩鏉ュ疄鐜般備緥濡傦細

{% if athlete_list %}
    {% if coach_list or cheerleader_list %}
        We have athletes, and either coaches or cheerleaders!
    {% endif %}
{% endif %}

Multiple uses of the same logical operator are fine, as long as you use the same operator. For example, this is valid:

閲嶅浣跨敤鍚屼竴閫昏緫绗︽槸鍙互鐨勩備緥濡傝繖鏍锋槸姝g‘鐨勶細

{% if athlete_list or coach_list or parent_list or teacher_list %}

ifchanged

ifchanged

Checks if a value has changed from the last iteration of a loop.

妫鏌ュ惊鐜腑涓涓间粠鏈杩戜竴娆¢噸澶嶅叾鏄惁鏀瑰彉銆

The ifchanged block tag is used within a loop. It has two possible uses:

ifchanged 璇彞鍧楃敤浜庡惊鐜腑锛屽叾浣滅敤鏈変袱涓細

It checks its own rendered contents against its previous state and only displays the content if it has changed. For example, this displays a list of days, only displaying the month if it changes:

瀹冧細鎶婅娓叉煋鐨勫唴瀹逛笌鍓嶄竴娆′綔姣旇緝锛屽彂鐢熷彉鍖栨椂鎵嶆樉绀哄畠銆備緥濡傦紝涓嬮潰瑕佹樉绀轰竴涓棩鏈熷垪琛紝鍙湁鏈堜唤鏀瑰彉鏃舵墠浼氭樉绀哄畠锛

<h1>Archive for {{ year }}</h1>

{% for date in days %}
    {% ifchanged %}<h3>{{ date|date:"F" }}</h3>{% endifchanged %}
    <a href="{{ date|date:"M/d"|lower }}/">{{ date|date:"j" }}</a>
{% endfor %}

If given a variable, it checks whether that variable has changed:

濡傛灉缁欑殑鏄竴涓彉閲忥紝灏变細妫鏌ュ畠鏄惁鍙戠敓鏀瑰彉銆

{% for date in days %}
    {% ifchanged date.date %} {{ date.date }} {% endifchanged %}
    {% ifchanged date.hour date.date %}
        {{ date.hour }}
    {% endifchanged %}
{% endfor %}

The preceding shows the date every time it changes, but it only shows the hour if both the hour and the date have changed.

鍓嶉潰閭d釜渚嬪瓙涓棩鏈熸瘡娆″彂鐢熷彉鍖栨椂灏变細鏄剧ず鍑烘潵锛屼絾鍙湁灏忔椂鍜屾棩鏈熼兘鍙戠敓鍙樺寲鏃舵墠浼氭樉绀哄皬鏃躲

ifequal

ifequal

Outputs the contents of the block if the two arguments equal each other.

濡傛灉涓や釜鍙傛暟鐩哥瓑锛屽氨杈撳嚭璇ュ尯鍧楃殑鍐呭銆

Heres an example:

涓句釜渚嬪瓙锛

{% ifequal user.id comment.user_id %}
    ...
{% endifequal %}

As in the {% if %} tag, an {% else %} clause is optional.

姝e {% if %} 鏍囩涓鏍凤紝 {% else %} 璇彞鏄彲閫夌殑銆

The arguments can be hard-coded strings, so the following is valid:

鍙傛暟涔熷彲浠ユ槸纭紪鐮佺殑瀛楃涓诧紝鎵浠ヤ笅闈㈣繖绉嶅啓娉曟槸姝g‘鐨勶細

{% ifequal user.username "adrian" %}
    ...
{% endifequal %}

It is only possible to compare an argument to template variables or strings. You cannot check for equality with Python objects such as True or False . If you need to test if something is true or false, use the if tag instead.

鍙互鐢ㄦ潵姣旇緝鐨勫弬鏁板彧闄愪簬妯℃澘鍙橀噺鎴栬呭瓧绗︿覆锛堝疄闄呬笂鏁存暟鍜屽皬鏁颁篃鏄彲浠ョ殑鈥斺旇瘧娉級锛屼綘涓嶈兘妫鏌ヨ濡 True or False 绛塒ython瀵硅薄鏄惁鐩哥瓑銆傚鏋滀綘闇瑕佹祴璇曟煇鍊肩殑鐪熷亣锛屽彲浠ョ敤 if 鏍囩銆

ifnotequal

ifnotequal

Just like ifequal , except it tests that the two arguments are not equal.

ifequal 绫讳技锛屼笉杩囧畠鏄敤鏉ユ祴璇曚袱涓弬鏁版槸 鐩哥瓑鐨勩

include

include

Loads a template and renders it with the current context. This is a way of including other templates within a template.

鍔犺浇涓涓ā鏉匡紝骞剁敤褰撳墠涓婁笅鏂囧瀹冭繘琛屾覆鏌擄紝杩欐槸鍦ㄤ竴涓ā鏉夸腑鍖呭惈鍏朵粬妯℃澘鐨勪竴绉嶆柟娉曘

The template name can be either a variable or a hard-coded (quoted) string, in either single or double quotes.

妯℃澘鍚嶅彲浠ユ槸涓涓彉閲忔垨鑰呮槸涓涓‖缂栫爜锛堝紩鍙峰紩璧锋潵鐨勶級鐨勫瓧绗︿覆锛屽紩鍙峰彲浠ユ槸鍗曞紩鍙锋垨鑰呭弻寮曞彿銆

This example includes the contents of the template "foo/bar.html" :

杩欎釜渚嬪瓙鍖呭惈浜 "foo/bar.html" 妯℃澘鐨勫唴瀹癸細

{% include "foo/bar.html" %}

This example includes the contents of the template whose name is contained in the variable template_name :

杩欎釜渚嬪瓙鍖呭惈浜嗗悕瀛楃敱鍙橀噺 template_name 鎸囧畾鐨勬ā鏉跨殑鍐呭锛

{% include template_name %}

load

load

Loads a custom template library. See Chapter 10 for information about custom template libraries.

璇诲叆涓涓嚜瀹氫箟鐨勬ā鏉垮簱銆傜鍗佺珷閲屾湁鍏充簬鑷畾涔夋ā鏉跨殑鐩稿叧淇℃伅璧勬枡

now

now

Displays the date, formatted according to the given string.

鏍规嵁缁欏畾鐨勬牸寮忓瓧绗︿覆鏄剧ず褰撳墠鏃ユ湡銆

This tag was inspired by, and uses the same format as, a PHPs date() function (http://php.net/date). Djangos version, however, has some custom extensions.

杩欎釜鏍囩鏉ユ簮浜嶱HP涓殑 date() 鍑芥暟锛 http://php.net/date 锛夛紝骞朵娇鐢ㄤ笌鍏剁浉鍚岀殑鏍煎紡璇硶锛屼絾鏄疍jango瀵瑰叾鍋氫簡鎵╁睍銆

Table F-2 shows the available format strings.

琛‵-2鏄剧ず浜嗗彲鐢ㄧ殑鏍煎紡瀛楃涓层

Table F-2. Available Date Format Strings
Format Character Description Example Output
a 'a.m.' or 'p.m.' . (Note that this is slightly different from PHPs output, because this includes periods to match Associated Press style.) 'a.m.'
A 'AM' or 'PM' . 'AM'
b Month, textual, three letters, lowercase 'jan'
d Day of the month, two digits with leading zeros. '01' to '31'
D Day of the week, textual, three letters. 'Fri'
f Time, in 12-hour hours and minutes, with minutes left off if theyre zero. '1' , '1:30'
F Month, textual, long. 'January'
g Hour, 12-hour format without leading zeros. '1' to '12'
G Hour, 24-hour format without leading zeros. '0' to '23'
h Hour, 12-hour format. '01' to '12'
H Hour, 24-hour format. '00' to '23'
i Minutes. '00' to '59'
j Day of the month without leading zeros. '1' to '31'
l Day of the week, textual, long. 'Friday'
L Boolean for whether its a leap year. True or False
m Month, two digits with leading zeros. '01' to '12'
M Month, textual, three letters. 'Jan'
n Month without leading zeros. '1' to '12'
N Month abbreviation in Associated Press style. 'Jan.' , 'Feb.' , 'March' , 'May'
O Difference to Greenwich Mean Time in hours. '+0200'
P Time, in 12-hour hours, minutes, and a.m./p.m., with minutes left off if theyre zero and the special-case strings 'midnight' and 'noon' if appropriate. '1 a.m.' , '1:30 p.m.' , 'midnight' , 'noon' , '12:30 p.m.'
r RFC 822 formatted date. 'Thu, 21 Dec 2000 16:01:07 +0200'
s Seconds, two digits with leading zeros. '00' to '59'
S English ordinal suffix for day of the month, two characters. 'st' , 'nd' , 'rd' or 'th'
t Number of days in the given month. 28 to 31
T Time zone of this machine. 'EST' , 'MDT'
w Day of the week, digits without leading zeros. '0' (Sunday) to '6' (Saturday)
W ISO-8601 week number of year, with weeks starting on Monday. 1 , 23
y Year, two digits. '99'
Y Year, four digits. '1999'
z Day of the year. 0 to 365
Z Time zone offset in seconds. The offset for time zones west of UTC is always negative, and for those east of UTC it is always positive. -43200 to 43200
琛‵-2. 鍙敤鐨勬棩鏈熸牸寮忓瓧绗︿覆
鏍煎紡瀛楃 涓 鎻忚堪 绀轰緥杈撳嚭
a 'a.m.' 鎴栬 'p.m.' 銆傦紙杩欎笌PHP涓殑杈撳嚭鐣ユ湁涓嶅悓锛 鍥犱负涓轰簡鍖归厤缇庤仈绀鹃鏍硷紝瀹冨寘鍚簡鍙ョ偣銆 'a.m.'
A 'AM' 鎴栬 'PM' 'AM'
b 鏈堜唤锛屾枃瀛楀紡鐨勶紝涓変釜瀛楁瘝锛屽皬鍐欍 'jan'
d 涓鏈堢殑绗嚑澶╋紝涓や綅鏁板瓧锛屽甫鍓嶅闆躲 '01''31'
D 涓鍛ㄧ殑绗嚑澶╋紝鏂囧瓧寮忕殑锛屼笁涓瓧姣嶃 'Fri'
f 鏃堕棿锛12灏忔椂鍒剁殑灏忔椂鍜屽垎閽熸暟锛屽鏋滃垎閽熸暟涓洪浂鍒欎笉鏄剧ず銆 '1' , '1:30'
F 鏈堜唤锛屾枃瀛楀紡鐨勶紝鍏ㄥ悕銆 'January'
g 灏忔椂锛12灏忔椂鍒讹紝娌℃湁鍓嶅闆躲 '1''12'
G 灏忔椂锛24灏忔椂鍒讹紝娌℃湁鍓嶅闆躲 '0''23'
h 灏忔椂锛12灏忔椂鍒躲 '01''12'
H 灏忔椂锛24灏忔椂鍒躲 '00''23'
i 鍒嗛挓銆 '00''59'
j 涓鏈堢殑绗嚑澶╋紝涓嶅甫鍓嶅闆躲 '1''31'
l 涓鍛ㄧ殑绗嚑澶╋紝鏂囧瓧寮忕殑锛屽叏鍚嶃 'Friday'
L 鏄惁涓洪棸骞寸殑甯冨皵鍊笺 TrueFalse
m 鏈堜唤锛屼袱浣嶆暟瀛楋紝甯﹀墠瀵奸浂銆 '01''12'
M 鏈堜唤锛屾枃瀛楀紡鐨勶紝涓変釜瀛楁瘝銆 'Jan'
n 鏈堜唤锛屾病鏈夊墠瀵奸浂銆 '1''12'
N 缇庤仈绀鹃鏍肩殑鏈堜唤缂╁啓銆 'Jan.' , 'Feb.' , 'March' , 'May'
O 涓庢牸鏋楀▉娌绘爣鍑嗘椂闂寸殑鏃堕棿宸紙浠ュ皬鏃惰锛夈 '+0200'
P 鏃堕棿锛12灏忔椂鍒剁殑灏忔椂鍒嗛挓鏁颁互鍙奱.m./p.m.锛屽垎閽熸暟濡傛灉涓洪浂 鍒欎笉鏄剧ず锛岀敤瀛楃涓茶〃绀虹壒娈婃椂闂寸偣锛屽 'midnight''noon' '1 a.m.' , '1:30 p.m.' , 'midnight' , 'noon' , '12:30 p.m.'
r RFC 822 鏍煎紡鐨勬棩鏈熴 'Thu, 21 Dec 2000 16:01:07 +0200'
s 绉掓暟锛屼袱浣嶆暟瀛楋紝甯﹀墠瀵奸浂銆 '00''59'
S 鑻辫搴忔暟鍚庣紑锛岀敤浜庤〃绀轰竴涓湀鐨勭鍑犲ぉ锛屼袱涓瓧姣嶃 'st' , 'nd' , 'rd''th'
t 鎸囧畾鏈堜唤鐨勫ぉ鏁般 2831
T 鏈満鐨勬椂鍖恒 'EST' , 'MDT'
w 涓鍛ㄧ殑绗嚑澶╋紝鏁板瓧锛屽甫鍓嶅闆躲 '0' (Sunday) 鍒 '6' (Saturday)
W ISO-8601 涓骞翠腑鐨勭鍑犲懆锛屼竴鍛ㄤ粠鏄熸湡涓寮濮嬨 1 , 23
y 骞翠唤锛屼袱浣嶆暟瀛椼 '99'
Y 骞翠唤锛屽洓浣嶆暟瀛椼 '1999'
z 涓骞寸殑绗嚑澶┿ 0365
Z 浠ョ璁$殑鏃跺尯鍋忕Щ閲忥紝杩欎釜鍋忕Щ閲忓浜嶶TC瑗块儴鏃跺尯鎬绘槸璐熸暟锛 瀵逛簬UTC涓滈儴鏃跺尯鎬绘槸姝f暟銆 -4320043200

Heres an example:

鐪嬭繖涓緥瀛愶細

It is {% now "jS F Y H:i" %}

Note that you can backslash-escape a format string if you want to use the raw value. In this example, f is backslash-escaped, because otherwise f is a format string that displays the time. The o doesnt need to be escaped, because its not a format character:

璁颁綇锛屽鏋滀綘鎯崇敤涓涓瓧绗︿覆鐨勫師濮嬪肩殑璇濓紝浣犲彲浠ョ敤鍙嶆枩绾胯繘琛岃浆涔夈傚湪杩欎釜渚嬪瓙涓紝f琚敤鍙嶆枩绾胯浆涔変簡锛屽鏋滀笉杞箟鐨勮瘽f灏辨槸鏄剧ず鏃堕棿鐨勬牸寮忓瓧绗︿覆銆俹涓嶇敤杞箟锛屽洜涓哄畠鏈潵灏变笉鏄竴涓牸寮忓瓧姣嶃

It is the {% now "jS o\f F" %}

This would display as It is the 4th of September.

杩欐牱灏变細鏄剧ず鎴愨淚t is the 4th of September鈥濄

regroup

regroup

Regroups a list of alike objects by a common attribute.

鎶婁竴鍒楃浉浼肩殑瀵硅薄鏍规嵁鏌愪竴涓叡鏈夌殑灞炴ч噸鏂板垎缁勩

This complex tag is best illustrated by use of an example. Say that people is a list of Person objects that have first_name , last_name , and gender attributes, and youd like to display a list that looks like this:

瑕佽В閲婃竻杩欎釜澶嶆潅鐨勬爣绛撅紝鏈濂芥潵涓句釜渚嬪瓙銆傛瘮濡傦紝 people 鏄寘鍚 Person 瀵硅薄鐨勪竴涓垪琛紝 杩欎釜瀵硅薄鎷ユ湁 first_namelast_namegender 灞炴э紝浣犳兂杩欐牱鏄剧ず杩欎釜鍒楄〃锛

* Male:
    * George Bush
    * Bill Clinton
* Female:
    * Margaret Thatcher
    * Condoleezza Rice
* Unknown:
    * Pat Smith

The following snippet of template code would accomplish this dubious task:

涓嬮潰杩欐妯℃澘浠g爜灏卞彲浠ュ畬鎴愯繖涓湅璧锋潵寰堝鏉傜殑浠诲姟锛

{% regroup people by gender as grouped %}
<ul>
{% for group in grouped %}
    <li>{{ group.grouper }}
    <ul>
        {% for item in group.list %}
        <li>{{ item }}</li>
        {% endfor %}
    </ul>
    </li>
{% endfor %}
</ul>

As you can see, {% regroup %} populates a variable with a list of objects with grouper and list attributes. grouper contains the item that was grouped by; list contains the list of objects that share that grouper . In this case, grouper would be Male , Female , and Unknown , and list is the list of people with those genders.

濡備綘鎵瑙侊紝 {% regroup %} 鏋勯犱簡涓涓垪琛ㄥ彉閲忥紝鍒楄〃涓殑姣忎釜瀵硅薄閮芥湁 grouperlist 灞炴с grouper 鍖呭惈鍒嗙粍鎵渚濇嵁鐨勫睘鎬э紝 list 鍖呭惈涓绯诲垪鎷ユ湁鍏卞悓鐨 grouper 灞炴х殑瀵硅薄銆傝繖鏍 grouper 灏变細鏄 MaleFemaleUnknownlist 灏辨槸灞炰簬杩欏嚑绉嶆у埆鐨勪汉浠

Note that {% regroup %} does not work when the list to be grouped is not sorted by the key you are grouping by! This means that if your list of people was not sorted by gender, youd need to make sure it is sorted before using it, that is:

璁颁綇锛屽鏋滆鍒嗙粍鐨勫垪琛ㄤ笉鏄寜鐓ф煇涓鍒楁帓濂藉簭鐨勮瘽锛屼綘灏变笉鑳界敤 {% regroup %} 鍦ㄨ繖涓鍒椾笂杩涜閲嶆柊鍒嗙粍锛佸氨鏄濡傛灉浜虹殑鍒楄〃涓嶆槸鎸夌収鎬у埆鎺掑ソ搴忕殑璇濓紝鍦ㄧ敤瀹冧箣鍓嶅氨瑕佸厛瀵瑰畠鎺掑簭锛屽嵆锛

{% regroup people|dictsort:"gender" by gender as grouped %}

spaceless

spaceless

Removes whitespace between HTML tags. This includes tab characters and newlines.

鍘婚櫎HTML鏍囩涔嬮棿鐨勭┖鐧界鍙凤紝鍖呮嫭鍒惰〃绗﹀拰鎹㈣绗︺

Heres an example:

渚嬪锛

{% spaceless %}
    <p>
        <a href="foo/">Foo</a>
    </p>
{% endspaceless %}

This example would return this HTML:

杩斿洖缁撴灉濡備笅锛

<p><a href="foo/">Foo</a></p>

Only space between tags is removed not space between tags and text. In this example, the space around Hello wont be stripped:

浠呬粎 鏍囩 涔嬮棿鐨勭┖鐧界琚垹鎺夛紝鏍囩鍜屾枃鏈箣闂寸殑绌虹櫧绗︽槸涓嶄細琚鐞嗙殑銆傚湪涓嬮潰杩欎釜渚嬪瓙涓紝 Hello 涓よ竟鐨勭┖鐧界鏄笉浼氳鎴帀鐨勶細

{% spaceless %}
    <strong>
        Hello
    </strong>
{% endspaceless %}

ssi

ssi

Outputs the contents of a given file into the page.

鎶婁竴涓寚瀹氱殑鏂囦欢鐨勫唴瀹硅緭鍑哄埌椤甸潰涓娿

Like a simple include tag, {% ssi %} includes the contents of another file which must be specified using an absolute path in the current page:

鍍廼nclude鏍囩涓鏍凤紝 {% ssi %} 浼氬寘鍚彟澶栦竴涓枃浠剁殑鍐呭锛岃繖涓枃浠跺繀椤讳互缁濆璺緞鎸囨槑锛

{% ssi /home/html/ljworld.com/includes/right_generic.html %}

If the optional parsed parameter is given, the contents of the included file are evaluated as template code, within the current context:

濡傛灉鎸囧畾浜嗗彲閫夌殑parsed鍙傛暟鐨勮瘽锛屽寘鍚繘鏉ョ殑鏂囦欢鐨勫唴瀹逛細琚綋浣滄ā鏉夸唬鐮侊紝骞剁敤褰撳墠鐨勪笂涓嬫枃鏉ユ覆鏌擄細

{% ssi /home/html/ljworld.com/includes/right_generic.html parsed %}

Note that if you use {% ssi %} , youll need to define ALLOWED_INCLUDE_ROOTS in your Django settings, as a security measure.

娉ㄦ剰锛屽鏋滀綘瑕佷娇鐢 {% ssi %} 鐨勮瘽锛屼负浜嗗畨鍏ㄨ捣瑙侊紝浣犲繀椤诲湪Django閰嶇疆鏂囦欢涓畾涔堿LLOWED_INCLUDE_ROOTS銆

Most of the time {% include %} works better than {% ssi %} ; {% ssi %} exists mostly for backward compatibility.

澶у鏁版儏鍐典笅 {% include %}{% ssi %} 鏇村ソ鐢紝 {% ssi %} 鐨勫瓨鍦ㄩ氬父鏄负浜嗗悜鍚庡吋瀹广

templatetag

templatetag

Outputs one of the syntax characters used to compose template tags.

杈撳嚭缁勬垚妯℃澘鏍囩鐨勮娉曞瓧绗︺

Since the template system has no concept of escaping, to display one of the bits used in template tags, you must use the {% templatetag %} tag.

妯℃澘绯荤粺娌℃湁杞箟鐨勬蹇碉紝鎵浠ヨ鏄剧ず涓涓粍鎴愭ā鏉挎爣绛剧殑瀛楃鐨勮瘽锛屼綘蹇呴』浣跨敤 {% templatetag %} 鏍囩銆

The argument tells which template bit to output (see Table F-3).

鍙傛暟鐢ㄦ潵鏍囨槑瑕佹樉绀虹殑瀛楃锛堝弬瑙佽〃F-3锛夈

Table F-3. Valid Arguments to templatetag
Argument Output
openblock {%
closeblock %}
openvariable {{
closevariable }}
openbrace {
closebrace }
opencomment {#
closecomment #}
琛‵-3. templatetag鐨勬湁鏁堝弬鏁
鍙傛暟 杈撳嚭
openblock {%
closeblock %}
openvariable {{
closevariable }}
openbrace {
closebrace }
opencomment {#
closecomment #}

url

url

Returns an absolute URL (i.e., a URL without the domain name) matching a given view function and optional parameters. This is a way to output links without violating the DRY principle by having to hard-code URLs in your templates:

鏍规嵁鎵缁欒鍥惧嚱鏁板拰鍙夊弬鏁帮紝杩斿洖涓涓粷瀵圭殑URL锛堝氨鏄笉甯﹀煙鍚嶇殑URL锛夈傜敱浜庢病鏈夊湪妯℃澘涓URL杩涜纭紪鐮侊紝鎵浠ヨ繖绉嶈緭鍑洪摼鎺ョ殑鏂规硶娌℃湁杩濆弽DRY鍘熷垯銆

{% url path.to.some_view arg1,arg2,name1=value1 %}

The first argument is a path to a view function in the format package.package.module.function . Additional arguments are optional and should be comma-separated values that will be used as positional and keyword arguments in the URL. All arguments required by the URLconf should be present.

绗竴涓彉閲忔槸鎸塸ackage.package.module.function褰㈠紡缁欏嚭鐨勬寚鍚戜竴涓獀iew鍑芥暟鐨勮矾寰勩傞偅浜涘彲閫夌殑銆佺敤閫楀彿鍒嗛殧鐨勯檮鍔犲弬鏁拌鐢ㄥ仛URL涓殑浣嶇疆鍜屽叧閿瘝鍙橀噺銆傛墍鏈塙RLconf闇瑕佺殑鍙傛暟閮藉簲璇ユ槸瀛樺湪鐨勩

For example, suppose you have a view, app_name.client , whose URLconf takes a client ID. The URLconf line might look like this:

渚嬪锛屽亣璁句綘鏈変竴涓獀iew锛宎pp_name.client锛屽畠鐨刄RLconf鍖呭惈涓涓猚lient ID鍙傛暟銆俇RLconf瀵瑰簲琛屽彲鑳界湅璧锋潵鍍忚繖鏍凤細

('^client/(\d+)/$', 'app_name.client')

If this applications URLconf is included into the projects URLconf under a path such as this:

濡傛灉杩欎釜搴旂敤鐨刄RLconf鍍忎笅闈竴鏍疯鍖呭惈鍦ㄩ」鐩殑URLconf閲岋細

('^clients/', include('project_name.app_name.urls'))

then, in a template, you can create a link to this view like this:

閭d箞锛屽湪妯℃澘涓紝浣犲彲浠ュ儚杩欐牱鍒涘缓涓涓寚鍚戦偅涓獀iew鐨刲ink杩炴帴锛

{% url app_name.client client.id %}

The template tag will output the string /clients/client/123/ .

妯℃澘鏍囩灏嗚緭鍑哄瓧绗︿覆/clients/client/123/

widthratio

widthratio锛屽搴︾殑姣旂巼

For creating bar charts and such, this tag calculates the ratio of a given value to a maximum value, and then applies that ratio to a constant.

鐐轰簡鐣嚭闀锋鍦栵紝閫欏嬫绫よ▓绠椾竴鍊嬬郸瀹氬肩浉灏嶆柤鏈澶у肩殑姣旂巼锛岀劧寰屽皣閫欏嬫瘮鐜囩郸瀹氫竴鍊嬪父鏁

Heres an example:

涓浜涘疄渚嬶細

<img src="bar.gif" height="10" width="{% widthratio this_value max_value 100 %}" />

If this_value is 175 and max_value is 200, the image in the preceding example will be 88 pixels wide (because 175/200 = .875; .875 * 100 = 87.5, which is rounded up to 88).

Built-in Filter Reference

add

Example:

渚嬪锛

{{ value|add:"5" }}

Adds the argument to the value.

鍙傛暟涓庤澶勭悊鏁版嵁鐩稿姞鐨勭粨鏋.

addslashes

Example:

渚嬪锛

{{ string|addslashes }}

Adds backslashes before single and double quotes. This is useful for passing strings to JavaScript, for example.

缁欐晱鎰熷瓧绗︽坊鍔犳枩绾(杞箟). 涓句緥,瑕佸皢涓涓瓧绗︿覆浼犻掔粰 JavaScript 鏃.

capfirst

Example:

渚嬪锛

{{ string|capfirst }}

Capitalizes the first character of the string.

灏嗗瓧绗︿覆鐨勯瀛楁瘝澶у啓

center

Example:

渚嬪锛

{{ string|center:"50" }}

Centers the string in a field of a given width.

鍦ㄤ竴涓粰瀹氱殑闀垮害璁╁瓧绗︿覆灞呬腑

cut

Example:

渚嬪锛

{{ string|cut:"spam" }}

Removes all values of the argument from the given string.

鎶婄粰瀹氬瓧绗︿覆涓寘鍚殑鎵鏈夊弬鏁板煎垹闄ゆ帀銆

date

Example:

{{ value|date:"F j, Y" }}

Formats a date according to the given format (same as the now tag).

鎶婁竴涓猟ate绫诲瀷鎸夌収缁欏畾鐨勬牸寮忚緭鍑猴紙涓”now”鏍囩鐢ㄦ硶涓鏍凤級銆

default

Example:

渚嬪锛

{{ value|default:"(N/A)" }}

If the value is unavailable, use the given default.

濡傛灉鍙橀噺涓嶅瓨鍦紝浣跨敤榛樿鍊

default_if_none

Example:

渚嬪锛

{{ value|default_if_none:"(N/A)" }}

If the value is None , use the given default.

濡傛灉鍙橀噺鍊间负None锛屼娇鐢ㄩ粯璁ゅ

dictsort

Example:

渚嬪锛

{{ list|dictsort:"foo" }}

Takes a list of dictionaries and returns that list sorted by the property given in the argument.

鎺ュ彈涓涓瓧鍏稿垪琛,杩斿洖鎸夌粰瀹氬弬鏁扮殑灞炴ф帓搴忓悗鐨勫垪琛.

dictsortreversed

Example:

渚嬪瓙:

{{ list|dictsortreversed:"foo" }}

Takes a list of dictionaries and returns that list sorted in reverse order by the property given in the argument.

鎺ュ彈涓涓瓧鍏稿垪琛,杩斿洖鎸夌粰瀹氬弬鏁扮殑灞炴ч嗗簭鎺掑簭鍚庣殑鍒楄〃

divisibleby

Example:

{% if value|divisibleby:"2" %}
    Even!
{% else %}
    Odd!
{% else %}

Returns True if the value is divisible by the argument.

濡傛灉鍊艰兘澶熻缁欏畾鐨勫弬鏁版暣闄ょ殑璇濓紝杩斿洖鈥淭rue鈥濄

escape

Example:

渚嬪锛

{{ string|escape }}

Escapes a strings HTML. Specifically, it makes these replacements:

鎸夌収浠ヤ笅鐨勮鍒欙紝杞箟涓涓狧TML瀛楃涓诧細

  • "&" to "&amp;"

  • < to "&lt;"

  • > to "&gt;"

  • '"' (double quote) to '&quot;'

  • "'" (single quote) to '&#39;'

filesizeformat

Example:

{{ value|filesizeformat }}

Formats the value like a human-readable file size (i.e., '13 KB' , '4.1 MB' , '102 bytes' , etc).

灏嗗兼牸寮忓寲涓 ‘鍙鎬уソ鐨’ 鏂囦欢澶у皬(姣斿 ‘13 KB’, ‘4.1 MB’, ‘102bytes’ 绛夌瓑).

first

Example:

{{ list|first }}

Returns the first item in a list.

杩斿洖鍒楄〃涓殑绗竴涓厓绱.

fix_ampersands

Example:

{{ string|fix_ampersands }}

Replaces ampersands with &amp; entities.

灏 & 绗﹀彿鏇挎崲涓 &amp; 瀹炰綋

floatformat

Examples:

{{ value|floatformat }}
{{ value|floatformat:"2" }}

When used without an argument, rounds a floating-point number to one decimal place but only if theres a decimal part to be displayed, for example:

灏嗕竴涓诞鐐规暟鍥涜垗浜斿叆鍒板皬鏁扮偣鍚1浣 — 濡傛灉鏍规湰娌℃湁灏忔暟,灏忔暟閮ㄥ垎涓嶄細鏄剧ず.

  • 36.123 gets converted to 36.1 .

  • 36.15 gets converted to 36.2 .

  • 36 gets converted to 36 .

If used with a numeric integer argument, floatformat rounds a number to that many decimal places:

濡傛灉浣跨敤鍚屼竴涓暟瀛楃殑鏁存暟鍙傛暟锛屸渇loatformat鈥濊疆鏁帮紝 璁稿灏忔暟浣嶏細

  • 36.1234 with floatformat:3 gets converted to 36.123 .

  • 36 with floatformat:4 gets converted to 36.0000 .

If the argument passed to floatformat is negative, it will round a number to that many decimal places but only if theres a decimal part to be displayed:

  • 36.1234 with floatformat:-3 gets converted to 36.123 .

  • 36 with floatformat:-4 gets converted to 36 .

Using floatformat with no argument is equivalent to using floatformat with an argument of -1 .

get_digit

Example:

{{ value|get_digit:"1" }}

Given a whole number, returns the requested digit of it, where 1 is the rightmost digit, 2 is the second-to-rightmost digit, and so forth. It returns the original value for invalid input (if the input or argument is not an integer, or if the argument is less than 1). Otherwise, output is always an integer.

鎻愪緵涓涓畬鏁寸殑鏁, 杩斿洖璇ユ暟涓璇锋眰鐨勬暟瀛,鍏朵腑 1 鏄渶鍙宠竟鐨勬暟, 2 鏄粠鍙宠竟鏁扮浜屼釜鏁板瓧绛夌瓑. 鑻ヨ緭鍏ュ奸潪娉(鑻ヨ緭鍏ユ垨鍙傛暟涓嶆槸鏁存暟, 鎴栬呭弬鏁板皬浜1)鍒欒繑鍥炲叾鍘熷鍊. 鍚﹀垯杈撳嚭灏辨绘槸鏁存暟.

join

Example:

渚嬪瓙锛

{{ list|join:", " }}

Joins a list with a string, like Pythons str.join(list) .

鐢ㄤ竴涓瓧绗︿覆灏嗕竴涓垪琛ㄨ繛鎺ヨ捣鏉,绫讳技 Python 鐨 str.join(list).

length

Example:

渚嬪瓙锛

{{ list|length }}

Returns the length of the value.

杩斿洖瀵硅薄鐨勯暱搴

length_is

Example:

{% if list|length_is:"3" %}
    ...
{% endif %}

Returns a Boolean of whether the values length is the argument.

鑻ュ肩殑闀垮害涓庡弬鏁扮浉绛,杩斿洖 True, 鍚﹀垯杩斿洖 False.

linebreaks

Example:

{{ string|linebreaks }}

Converts newlines into <p> and <br /> tags.

鎶婃崲琛岀杞崲鎴<p>鍜<br />鏍囩銆

linebreaksbr

Example:

{{ string|linebreaksbr }}

Converts newlines into <br /> tags.

鎶婃瘡涓柊琛岃浆鎹负”<br />”鏍囩

linenumbers

Example:

{{ string|linenumbers }}

Displays text with line numbers.

甯﹁鍙锋樉绀烘枃鏈

ljust

Example:

{{ string|ljust:"50" }}

Left-aligns the value in a field of a given width.

鍦ㄧ粰瀹氬搴︾殑鍩熷唴灏嗘枃鏈乏瀵归綈.

lower

Example:

{{ string|lower }}

Converts a string into all lowercase.

鎶婁竴涓粰瀹氱殑瀛楃涓茶浆鎹㈡垚灏忓啓銆

make_list

Example:

{% for i in number|make_list %}
    ...
{% endfor %}

Returns the value turned into a list. For an integer, its a list of digits. For a string, its a list of characters.

灏嗗艰浆鍖栦负涓涓垪琛.瀵逛竴涓暣鏁,瀹冩槸涓涓暟瀛楃殑鍒楄〃.瀵逛竴涓瓧绗︿覆,杩欐槸涓涓瓧绗︾殑鍒楄〃

phone2numeric

Example:

{{ string|phone2numeric }}

Converts a phone number (possibly containing letters) to its numerical equivalent. For example, '800-COLLECT' will be converted to '800-2655328' .

灏嗕竴涓數璇濆彿鐮(鍙兘鍖呭惈瀛楁瘝)杞寲绛変环鐨勬暟瀛楀.姣斿: ‘800-COLLECT’ 灏嗚杞寲涓 ‘800-2655328’.

The input doesnt have to be a valid phone number. This will happily convert any string.

杈撳叆涓嶄竴瀹氶潪鏄竴涓悎娉曞彿鐮. 瀹冨彲浠ヨ浆鍖栦换鎰忓瓧绗︿覆.

pluralize

Example:

The list has {{ list|length }} item{{ list|pluralize }}.

Returns a plural suffix if the value is not 1. By default, this suffix is 's' .

濡傛灉鍊间笉鏄 1 鐨勮瘽杩斿洖 ‘s’ 鐢ㄤ簬 ‘1 vote’ vs. ‘2 votes’ 杩欑鍦哄悎

Example:

You have {{ num_messages }} message{{ num_messages|pluralize }}.

For words that require a suffix other than 's' , you can provide an alternate suffix as a parameter to the filter.

Example:

You have {{ num_walruses }} walrus{{ num_walrus|pluralize:"es" }}.

For words that dont pluralize by simple suffix, you can specify both a singular and plural suffix, separated by a comma.

Example:

You have {{ num_cherries }} cherr{{ num_cherries|pluralize:"y,ies" }}.

pprint

Example:

{{ object|pprint }}

A wrapper around Pythons built-in pprint.pprint for debugging, really.

pprint.pprint 鍜屼竴涓皝瑁呭櫒— 浠呯敤浜庤皟璇.

random

Example:

{{ list|random }}

Returns a random item from the list.

杩斿洖闅忔満鐨勪粠鍒楄〃涓繑鍥炰竴涓厓绱

removetags

Example:

{{ string|removetags:"br p div" }}

Removes a space-separated list of [X]HTML tags from the output.

浠庤緭鍑轰腑鍒犻櫎鍗曠┖鏍煎垎闅旂殑 [X]HTML鏍囩 鍒楄〃

rjust

Example:

{{ string|rjust:"50" }}

Right-aligns the value in a field of a given width.

鍦ㄧ粰瀹氬搴︾殑鍩熷唴灏嗘枃鏈彸瀵归綈.

slice

Example:

{{ some_list|slice:":2" }}

Returns a slice of the list.

杩斿洖涓涓垪琛ㄧ殑鐗囨

Uses the same syntax as Pythons list slicing. See http://diveintopython.org/native_data_types/lists.html#odbchelper.list.slice for an introduction.

浣跨敤涓 Python 鐨 list 鐩稿悓鐨勮娉, 鍙傞槄 http://diveintopython.org/native_data_types/lists.html#odbchelper.list.slice 浠ヤ簡瑙f洿璇︾粏鐨勪俊鎭.

slugify

Example:

{{ string|slugify }}

Converts to lowercase, removes nonword characters (alphanumerics and underscores), and converts spaces to hyphens. It also strips leading and trailing whitespace.

杞寲涓哄皬鍐, 绉诲幓闈炲崟璇嶅瓧绗(瀛楁瘝鏁板瓧鍜屼笅鍒掔嚎),灏嗙┖鐧借浆鍖栦负杩炲瓧绗,鍘婚櫎鍓嶅悗绌虹櫧.

stringformat

Example:

{{ number|stringformat:"02i" }}

Formats the variable according to the argument, a string formatting specifier. This specifier uses Python string-formatting syntax, with the exception that the leading % is dropped.

鏍规嵁缁欏畾鍙傛暟(涓涓牸寮忓瓧绗︿覆)鏍煎紡鍖栦竴涓彉閲, 杩欎釜鏍煎紡瀛楃涓蹭娇鐢 Python 瀛楃涓叉牸寮忓寲璇硶, 渚嬪涔嬪鏄 “%” 杩愮畻绗﹁鐪佺暐.

See http://docs.python.org/lib/typesseq-strings.html for documentation of Python string formatting.

鍙傞槄 http://docs.python.org/lib/typesseq-strings.html 浠ヤ簡瑙 Python 鏍煎紡瀛楃涓

striptags

Example:

{{ string|striptags }}

Strips all [X]HTML tags.

杩囨护鎺塠X]HTML鏍囩.

time

Example:

{{ value|time:"P" }}

Formats a time according to the given format (same as the now tag).

鎸夋寚瀹氱殑鏍峰紡锛堟牱寮忓畾涔夊悓now鏍囩锛夋潵鏍煎紡鍖栦竴涓椂闂村璞

timesince

Examples:

{{ datetime|timesince }}
{{ datetime|timesince:"other_datetime" }}

Formats a date as the time since that date (e.g., 4 days, 6 hours).

鏍煎紡鍖栦竴涓棩鏈,杩欎釜鏃ユ湡鏄粠缁欏畾鏃ユ湡鍒扮幇鍦ㄧ殑澶╂暟鍜屽皬鏃舵暟(姣斿: “4 days, 6 hours”).

Takes an optional argument that is a variable containing the date to use as the comparison point (without the argument, the comparison point is now ). For example, if blog_date is a date instance representing midnight on 1 June 2006, and comment_date is a date instance for 08:00 on 1 June 2006, then {{ comment_date|timesince:blog_date }} would return 8 hours.

鎺ュ彈涓涓彲閫夌殑鍙傛暟锛岃鍙傛暟鏄竴涓寘鍚瘮杈冩棩鏈熺殑鍙橀噺锛堣鍙傛暟榛樿鍊兼槸 now). 涓句緥鏉ヨ锛 濡傛灉 blog_date 鏄竴涓棩鏈熷疄渚嬭〃绀 2006-06-01 鍗堝锛 鑰 comment_date 鏄竴涓棩鏈熷疄渚嬭〃绀 2006-06-01 鏃╀笂8鐐癸紝閭d箞 {{ comment_date|timesince:blog_date }} 灏嗚繑鍥 “8 hours”.

timeuntil

Examples:

{{ datetime|timeuntil }}
{{ datetime|timeuntil:"other_datetime" }}

Similar to timesince , except that it measures the time from now until the given date or datetime. For example, if today is 1 June 2006 and conference_date is a date instance holding 29 June 2006, then {{ conference_date|timeuntil }} will return 28 days.

绫讳技 timesince, 鍙槸瀹冩瘮杈冨綋鍓嶆椂闂寸洿鍒扮粰瀹氭棩鏈熸椂闂淬備妇渚嬫潵璇达紝濡傛灉浠婂ぉ鏄 2006-06-01 鑰 conference_date 鏄 2006-06-29, 閭d箞 {{ conference_date|timeuntil }} 灏嗚繑鍥 “28 days”.

Takes an optional argument that is a variable containing the date to use as the comparison point (instead of now ). If from_date contains 22 June 2006, then {{ conference_date|timeuntil:from_date }} will return 7 days.

鎺ュ彈涓涓彲閫夌殑鍙傛暟锛岃鍙傛暟鏄竴涓寘鍚瘮杈冩棩鏈熺殑鍙橀噺锛堣鍙傛暟榛樿鍊兼槸 now). 涓句緥鏉ヨ锛 濡傛灉 from_date 鏄竴涓棩鏈熷疄渚嬭〃绀 2006-06-22锛 閭d箞 {{ conference_date|timeuntil:from_date }} 浼氳繑鍥 “7 days”.

title

Example:

{{ string|titlecase }}

Converts a string into title case.

鎸夋爣棰樻牸寮忚浆鍖栦竴涓瓧绗︿覆

truncatewords

Example:

{{ string|truncatewords:"15" }}

Truncates a string after a certain number of words.

灏嗕竴涓瓧绗︿覆鎴煭涓烘寚瀹氭暟鐩殑鍗曡瘝.

truncatewords_html

Example:

{{ string|truncatewords_html:"15" }}

Similar to truncatewords , except that it is aware of HTML tags. Any tags that are opened in the string and not closed before the truncation point are closed immediately after the truncation.

This is less efficient than truncatewords , so it should be used only when it is being passed HTML text.

unordered_list

Example:

<ul>
    {{ list|unordered_list }}
</ul>

Recursively takes a self-nested list and returns an HTML unordered list without opening and closing <ul> tags.

閫掑綊鐨勬帴鍙椾竴涓嚜宓屽鐨勫垪琛ㄥ苟杩斿洖涓涓狧TML鏃犲簭鍒楄〃(姝ゅ垪琛ㄥ彲涓嶆槸pythob璇箟涓殑鍒楄〃) — 鍙槸娌℃湁寮濮嬪拰缁撴潫鐨<ul>鏍囩

The list is assumed to be in the proper format. For example, if var contains ['States', [['Kansas', [['Lawrence', []], ['Topeka', []]]], ['Illinois', []]]] , then {{ var|unordered_list }} would return the following:

鍋囧畾璇ュ垪琛ㄦ槸鍚堥傜殑鏍煎紡. 涓句緥鏉ヨ, 濡傛灉 var 鐨勫间负 [‘States’, [[‘Kansas’, [[‘Lawrence’, []], [‘Topeka’, []]]], [‘Illinois’, []]]], 閭d箞 {{ var|unordered_list }} 灏变細杩斿洖:

<li>States
<ul>
        <li>Kansas
        <ul>
                <li>Lawrence</li>
                <li>Topeka</li>
        </ul>
        </li>
        <li>Illinois</li>
</ul>
</li>

upper

Example:

渚嬪瓙锛

{{ string|upper }}

Converts a string into all uppercase.

灏嗕竴涓瓧绗︿覆鍏ㄩ儴瀛楁瘝鏀逛负澶у啓銆

urlencode

Example:

渚嬪瓙锛

<a href="{{ link|urlencode }}">linkage</a>

Escapes a value for use in a URL.

杞箟璇ュ间互鐢ㄤ簬 URL.

urlize

Example:

渚嬪瓙锛

{{ string|urlize }}

Converts URLs in plain text into clickable links.

灏哢RLs鐢辩函鏂囨湰鍙樹负鍙偣鍑荤殑閾炬帴銆

urlizetrunc

Example:

渚嬪瓙锛

{{ string|urlizetrunc:"30" }}

Converts URLs into clickable links, truncating URLs to the given character limit.

灏哢RLs鍙樹负鍙偣鍑荤殑閾炬帴锛屾寜缁欏畾瀛楁瘝闄愭埅鐭璘RLs銆

wordcount

wordcount

Example:

渚嬪锛

{{ string|wordcount }}

Returns the number of words.

杩斿洖鍗曡瘝鏁般

wordwrap

wordwrap

Example:

渚嬪锛

{{ string|wordwrap:"75" }}

Wraps words at a specified line length.

鍦ㄦ寚瀹氶暱搴﹀皢鏂囧瓧鎹㈣銆

yesno

yesno

Example:

渚嬪锛

{{ boolean|yesno:"Yes,No,Perhaps" }}

Given a string mapping values for True , False , and (optionally) None , returns one of those strings according to the value (see Table F-4).

鎻愪緵涓涓瓧绗︿覆鍙傛暟瀵瑰簲鐫 true, false 鍜 (鍙夌殑) None, 鏍规嵁琚鐞嗙殑鍊艰繑鍥炵浉搴旂殑瀛楃涓:

Table F-4. Examples of the yesno Filter
Value Argument Output
True "yeah,no,maybe" yeah
False "yeah,no,maybe" no
None "yeah,no,maybe" maybe
None "yeah,no" "no" (converts None to False if no mapping for None is given)
琛‵-4. yesno杩囨护鍣ㄧず渚
鍙傛暟 杈撳嚭
True "yeah,no,maybe" yeah
False "yeah,no,maybe" no
None "yeah,no,maybe" maybe
None "yeah,no" "no" (濡傛灉涓嶅瓨鍦 None 鐨勬槧灏勶紝灏 None 鍙樹负 False )
Copyright 2006 Adrian Holovaty and Jacob Kaplan-Moss.
This work is licensed under the GNU Free Document License.
Hosting graciously provided by media temple
Chinese translate hosting by py3k.cn.