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簺鍙敤鐨勬爣绛惧拰杩囨护鍣ㄧ殑鍙傝冨彲璁や负鏄渶鍙俊鐨勩
Defines a block that can be overridden by child templates. See the section on template inheritance in Chapter 4 for more information.
瀹氫箟涓涓兘琚瓙妯℃澘瑕嗙洊鐨勫尯鍧椼傚弬瑙佺鍥涚珷鈥滄ā鏉跨户鎵库濅竴鑺傛煡鐪嬫洿澶氫俊鎭
Ignores everything between {% comment %} and {% endcomment %} .
妯℃澘寮曟搸浼氬拷鐣ユ帀 {% comment %} 鍜 {% endcomment %} 涔嬮棿鐨勬墍鏈夊唴瀹广
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.
浣犲彲浠ヤ娇鐢ㄤ换鎰忔暟閲忕殑鐢ㄩ楀彿鍒嗛殧鐨勫笺傛敞鎰忎笉瑕佸湪鍊间笌鍊间箣闂存湁绌烘牸锛屽彧鏄竴涓楀彿銆
Outputs a whole load of debugging information, including the current context and imported modules.
杈撳嚭瀹屾暣鐨勮皟璇曚俊鎭紝鍖呮嫭褰撳墠鐨勪笂涓嬫枃鍙婂鍏ョ殑妯″潡淇℃伅銆
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.
鍙傜湅绗洓绔犳洿澶氬簲鐢ㄥ疄渚嬨
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 %}
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 %}
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锛夈
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. |
鍙橀噺鍚 | 鎻忚堪 |
---|---|
forloop.counter | 褰撳墠寰幆娆℃暟锛堢储寮曟渶灏忎负1锛夈 |
forloop.counter0 | 褰撳墠寰幆娆℃暟 (绱㈠紩鏈灏忎负0)銆 |
forloop.revcounter | 鍓╀綑寰幆娆℃暟 (绱㈠紩鏈灏忎负1)銆 |
forloop.revcounter0 | 鍓╀綑寰幆娆℃暟 (绱㈠紩鏈灏忎负0)銆 |
forloop.first | 绗竴娆″惊鐜椂涓 True 銆 |
forloop.last | 鏈鍚庝竴娆″惊鐜椂涓 True 銆 |
forloop.parentloop | 鐢ㄤ簬宓屽寰幆锛岃〃绀哄綋鍓嶅惊鐜灞傜殑寰幆銆 |
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 璇彞鍙娇鐢 and 銆 or 鍜 not 鏉ユ祴璇曞彉閲忔垨鑰呭缁欏畾鐨勫彉閲忓彇鍙嶏細
{% 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:
涓嶅厑璁 and 鍜 or 鍚屾椂鍑虹幇鍦ㄤ竴涓 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:
濡傛灉浣犻渶瑕佸悓鏃朵娇鐢 and 鍜 or 鏉ュ疄鐜拌緝楂樼骇鐨勯昏緫锛屽彲浠ョ敤宓屽鐨 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 %}
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釜渚嬪瓙涓棩鏈熸瘡娆″彂鐢熷彉鍖栨椂灏变細鏄剧ず鍑烘潵锛屼絾鍙湁灏忔椂鍜屾棩鏈熼兘鍙戠敓鍙樺寲鏃舵墠浼氭樉绀哄皬鏃躲
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 鏍囩銆
Just like ifequal , except it tests that the two arguments are not equal.
鍜 ifequal 绫讳技锛屼笉杩囧畠鏄敤鏉ユ祴璇曚袱涓弬鏁版槸 涓 鐩哥瓑鐨勩
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 %}
Loads a custom template library. See Chapter 10 for information about custom template libraries.
璇诲叆涓涓嚜瀹氫箟鐨勬ā鏉垮簱銆傜鍗佺珷閲屾湁鍏充簬鑷畾涔夋ā鏉跨殑鐩稿叧淇℃伅璧勬枡
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鏄剧ず浜嗗彲鐢ㄧ殑鏍煎紡瀛楃涓层
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 |
鏍煎紡瀛楃 涓 | 鎻忚堪 | 绀轰緥杈撳嚭 |
---|---|---|
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 | 鏄惁涓洪棸骞寸殑甯冨皵鍊笺 | True 鍒 False |
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 | 鎸囧畾鏈堜唤鐨勫ぉ鏁般 | 28 鍒 31 |
T | 鏈満鐨勬椂鍖恒 | 'EST' , 'MDT' |
w | 涓鍛ㄧ殑绗嚑澶╋紝鏁板瓧锛屽甫鍓嶅闆躲 | '0' (Sunday) 鍒 '6' (Saturday) |
W | ISO-8601 涓骞翠腑鐨勭鍑犲懆锛屼竴鍛ㄤ粠鏄熸湡涓寮濮嬨 | 1 , 23 |
y | 骞翠唤锛屼袱浣嶆暟瀛椼 | '99' |
Y | 骞翠唤锛屽洓浣嶆暟瀛椼 | '1999' |
z | 涓骞寸殑绗嚑澶┿ | 0 鍒 365 |
Z | 浠ョ璁$殑鏃跺尯鍋忕Щ閲忥紝杩欎釜鍋忕Щ閲忓浜嶶TC瑗块儴鏃跺尯鎬绘槸璐熸暟锛 瀵逛簬UTC涓滈儴鏃跺尯鎬绘槸姝f暟銆 | -43200 鍒 43200 |
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鈥濄
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_name 銆 last_name 鍜 gender 灞炴э紝浣犳兂杩欐牱鏄剧ず杩欎釜鍒楄〃锛
* 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 %} 鏋勯犱簡涓涓垪琛ㄥ彉閲忥紝鍒楄〃涓殑姣忎釜瀵硅薄閮芥湁 grouper 鍜 list 灞炴с grouper 鍖呭惈鍒嗙粍鎵渚濇嵁鐨勫睘鎬э紝 list 鍖呭惈涓绯诲垪鎷ユ湁鍏卞悓鐨 grouper 灞炴х殑瀵硅薄銆傝繖鏍 grouper 灏变細鏄 Male 銆 Female 鍜 Unknown 锛 list 灏辨槸灞炰簬杩欏嚑绉嶆у埆鐨勪汉浠
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 %}
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 %}
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 %} 鐨勫瓨鍦ㄩ氬父鏄负浜嗗悜鍚庡吋瀹广
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锛夈
Argument | Output |
---|---|
openblock | {% |
closeblock | %} |
openvariable | {{ |
closevariable | }} |
openbrace | { |
closebrace | } |
opencomment | {# |
closecomment | #} |
鍙傛暟 | 杈撳嚭 |
---|---|
openblock | {% |
closeblock | %} |
openvariable | {{ |
closevariable | }} |
openbrace | { |
closebrace | } |
opencomment | {# |
closecomment | #} |
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锛屽搴︾殑姣旂巼
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).
Example:
渚嬪锛
{{ value|add:"5" }}
Adds the argument to the value.
鍙傛暟涓庤澶勭悊鏁版嵁鐩稿姞鐨勭粨鏋.
Example:
渚嬪锛
{{ string|addslashes }}
Adds backslashes before single and double quotes. This is useful for passing strings to JavaScript, for example.
缁欐晱鎰熷瓧绗︽坊鍔犳枩绾(杞箟). 涓句緥,瑕佸皢涓涓瓧绗︿覆浼犻掔粰 JavaScript 鏃.
Example:
渚嬪锛
{{ string|capfirst }}
Capitalizes the first character of the string.
灏嗗瓧绗︿覆鐨勯瀛楁瘝澶у啓
Example:
渚嬪锛
{{ string|center:"50" }}
Centers the string in a field of a given width.
鍦ㄤ竴涓粰瀹氱殑闀垮害璁╁瓧绗︿覆灞呬腑
Example:
渚嬪锛
{{ string|cut:"spam" }}
Removes all values of the argument from the given string.
鎶婄粰瀹氬瓧绗︿覆涓寘鍚殑鎵鏈夊弬鏁板煎垹闄ゆ帀銆
Example:
{{ value|date:"F j, Y" }}
Formats a date according to the given format (same as the now tag).
鎶婁竴涓猟ate绫诲瀷鎸夌収缁欏畾鐨勬牸寮忚緭鍑猴紙涓”now”鏍囩鐢ㄦ硶涓鏍凤級銆
Example:
渚嬪锛
{{ value|default:"(N/A)" }}
If the value is unavailable, use the given default.
濡傛灉鍙橀噺涓嶅瓨鍦紝浣跨敤榛樿鍊
Example:
渚嬪锛
{{ value|default_if_none:"(N/A)" }}
If the value is None , use the given default.
濡傛灉鍙橀噺鍊间负None锛屼娇鐢ㄩ粯璁ゅ
Example:
渚嬪锛
{{ list|dictsort:"foo" }}
Takes a list of dictionaries and returns that list sorted by the property given in the argument.
鎺ュ彈涓涓瓧鍏稿垪琛,杩斿洖鎸夌粰瀹氬弬鏁扮殑灞炴ф帓搴忓悗鐨勫垪琛.
Example:
渚嬪瓙:
{{ list|dictsortreversed:"foo" }}
Takes a list of dictionaries and returns that list sorted in reverse order by the property given in the argument.
鎺ュ彈涓涓瓧鍏稿垪琛,杩斿洖鎸夌粰瀹氬弬鏁扮殑灞炴ч嗗簭鎺掑簭鍚庣殑鍒楄〃
Example:
{% if value|divisibleby:"2" %} Even! {% else %} Odd! {% else %}
Returns True if the value is divisible by the argument.
濡傛灉鍊艰兘澶熻缁欏畾鐨勫弬鏁版暣闄ょ殑璇濓紝杩斿洖鈥淭rue鈥濄
Example:
渚嬪锛
{{ string|escape }}
Escapes a strings HTML. Specifically, it makes these replacements:
鎸夌収浠ヤ笅鐨勮鍒欙紝杞箟涓涓狧TML瀛楃涓诧細
"&" to "&"
< to "<"
> to ">"
'"' (double quote) to '"'
"'" (single quote) to '''
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’ 绛夌瓑).
Example:
{{ list|first }}
Returns the first item in a list.
杩斿洖鍒楄〃涓殑绗竴涓厓绱.
Example:
{{ string|fix_ampersands }}
Replaces ampersands with & entities.
灏 & 绗﹀彿鏇挎崲涓 & 瀹炰綋
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 .
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)鍒欒繑鍥炲叾鍘熷鍊. 鍚﹀垯杈撳嚭灏辨绘槸鏁存暟.
Example:
渚嬪瓙锛
{{ list|join:", " }}
Joins a list with a string, like Pythons str.join(list) .
鐢ㄤ竴涓瓧绗︿覆灏嗕竴涓垪琛ㄨ繛鎺ヨ捣鏉,绫讳技 Python 鐨 str.join(list).
Example:
渚嬪瓙锛
{{ list|length }}
Returns the length of the value.
杩斿洖瀵硅薄鐨勯暱搴
Example:
{% if list|length_is:"3" %} ... {% endif %}
Returns a Boolean of whether the values length is the argument.
鑻ュ肩殑闀垮害涓庡弬鏁扮浉绛,杩斿洖 True, 鍚﹀垯杩斿洖 False.
Example:
{{ string|linebreaks }}
Converts newlines into <p> and <br /> tags.
鎶婃崲琛岀杞崲鎴<p>鍜<br />鏍囩銆
Example:
{{ string|linebreaksbr }}
Converts newlines into <br /> tags.
鎶婃瘡涓柊琛岃浆鎹负”<br />”鏍囩
Example:
{{ string|linenumbers }}
Displays text with line numbers.
甯﹁鍙锋樉绀烘枃鏈
Example:
{{ string|ljust:"50" }}
Left-aligns the value in a field of a given width.
鍦ㄧ粰瀹氬搴︾殑鍩熷唴灏嗘枃鏈乏瀵归綈.
Example:
{{ string|lower }}
Converts a string into all lowercase.
鎶婁竴涓粰瀹氱殑瀛楃涓茶浆鎹㈡垚灏忓啓銆
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.
灏嗗艰浆鍖栦负涓涓垪琛.瀵逛竴涓暣鏁,瀹冩槸涓涓暟瀛楃殑鍒楄〃.瀵逛竴涓瓧绗︿覆,杩欐槸涓涓瓧绗︾殑鍒楄〃
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.
杈撳叆涓嶄竴瀹氶潪鏄竴涓悎娉曞彿鐮. 瀹冨彲浠ヨ浆鍖栦换鎰忓瓧绗︿覆.
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" }}.
Example:
{{ object|pprint }}
A wrapper around Pythons built-in pprint.pprint for debugging, really.
pprint.pprint 鍜屼竴涓皝瑁呭櫒— 浠呯敤浜庤皟璇.
Example:
{{ list|random }}
Returns a random item from the list.
杩斿洖闅忔満鐨勪粠鍒楄〃涓繑鍥炰竴涓厓绱
Example:
{{ string|removetags:"br p div" }}
Removes a space-separated list of [X]HTML tags from the output.
浠庤緭鍑轰腑鍒犻櫎鍗曠┖鏍煎垎闅旂殑 [X]HTML鏍囩 鍒楄〃
Example:
{{ string|rjust:"50" }}
Right-aligns the value in a field of a given width.
鍦ㄧ粰瀹氬搴︾殑鍩熷唴灏嗘枃鏈彸瀵归綈.
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洿璇︾粏鐨勪俊鎭.
Example:
{{ string|slugify }}
Converts to lowercase, removes nonword characters (alphanumerics and underscores), and converts spaces to hyphens. It also strips leading and trailing whitespace.
杞寲涓哄皬鍐, 绉诲幓闈炲崟璇嶅瓧绗(瀛楁瘝鏁板瓧鍜屼笅鍒掔嚎),灏嗙┖鐧借浆鍖栦负杩炲瓧绗,鍘婚櫎鍓嶅悗绌虹櫧.
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 鏍煎紡瀛楃涓
Example:
{{ string|striptags }}
Strips all [X]HTML tags.
杩囨护鎺塠X]HTML鏍囩.
Example:
{{ value|time:"P" }}
Formats a time according to the given format (same as the now tag).
鎸夋寚瀹氱殑鏍峰紡锛堟牱寮忓畾涔夊悓now鏍囩锛夋潵鏍煎紡鍖栦竴涓椂闂村璞
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”.
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”.
Example:
{{ string|titlecase }}
Converts a string into title case.
鎸夋爣棰樻牸寮忚浆鍖栦竴涓瓧绗︿覆
Example:
{{ string|truncatewords:"15" }}
Truncates a string after a certain number of words.
灏嗕竴涓瓧绗︿覆鎴煭涓烘寚瀹氭暟鐩殑鍗曡瘝.
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.
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>
Example:
渚嬪瓙锛
{{ string|upper }}
Converts a string into all uppercase.
灏嗕竴涓瓧绗︿覆鍏ㄩ儴瀛楁瘝鏀逛负澶у啓銆
Example:
渚嬪瓙锛
<a href="{{ link|urlencode }}">linkage</a>
Escapes a value for use in a URL.
杞箟璇ュ间互鐢ㄤ簬 URL.
Example:
渚嬪瓙锛
{{ string|urlize }}
Converts URLs in plain text into clickable links.
灏哢RLs鐢辩函鏂囨湰鍙樹负鍙偣鍑荤殑閾炬帴銆
Example:
渚嬪瓙锛
{{ string|urlizetrunc:"30" }}
Converts URLs into clickable links, truncating URLs to the given character limit.
灏哢RLs鍙樹负鍙偣鍑荤殑閾炬帴锛屾寜缁欏畾瀛楁瘝闄愭埅鐭璘RLs銆
Example:
渚嬪锛
{{ string|wordcount }}
Returns the number of words.
杩斿洖鍗曡瘝鏁般
Example:
渚嬪锛
{{ string|wordwrap:"75" }}
Wraps words at a specified line length.
鍦ㄦ寚瀹氶暱搴﹀皢鏂囧瓧鎹㈣銆
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, 鏍规嵁琚鐞嗙殑鍊艰繑鍥炵浉搴旂殑瀛楃涓:
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) |
鍊 | 鍙傛暟 | 杈撳嚭 |
---|---|---|
True | "yeah,no,maybe" | yeah |
False | "yeah,no,maybe" | no |
None | "yeah,no,maybe" | maybe |
None | "yeah,no" | "no" (濡傛灉涓嶅瓨鍦 None 鐨勬槧灏勶紝灏 None 鍙樹负 False ) |
鍏充簬鏈瘎娉ㄧ郴缁
鏈珯浣跨敤涓婁笅鏂囧叧鑱旂殑璇勬敞绯荤粺鏉ユ敹闆嗗弽棣堜俊鎭備笉鍚屼簬涓鑸鏁寸珷鍋氳瘎娉ㄧ殑鍋氭硶锛 鎴戜滑鍏佽浣犲姣忎竴涓嫭绔嬬殑鈥滄枃鏈潡鈥濆仛璇勬敞銆備竴涓滄枃鏈潡鈥濈湅璧锋潵鏄繖鏍风殑锛
涓涓滄枃鏈潡鈥濇槸涓涓钀斤紝涓涓垪琛ㄩ」锛屼竴娈典唬鐮侊紝鎴栬呭叾浠栦竴灏忔鍐呭銆 浣犻変腑瀹冧細楂樹寒搴︽樉绀:
瑕佸鏂囨湰鍧楀仛璇勬敞锛屼綘鍙渶瑕佺偣鍑诲畠鏃佽竟鐨勬爣璇嗗潡:
鎴戜滑浼氫粩缁嗛槄璇绘瘡涓瘎璁猴紝濡傛灉鍙兘鐨勮瘽鎴戜滑涔熶細鎶婅瘎娉ㄨ冭檻鍒版湭鏉ョ殑鐗堟湰涓幓:
濡傛灉浣犳効鎰忎綘鐨勮瘎娉ㄨ閲囩敤锛岃纭繚鐣欎笅浣犵殑鍏ㄥ悕 (娉ㄦ剰涓嶆槸鏄电О鎴栫畝绉帮級
Many, many thanks to Jack Slocum; the inspiration and much of the code for the comment system comes from Jack's blog, and this site couldn't have been built without his wonderful
YAHOO.ext
library. Thanks also to Yahoo for YUI itself.