模板標簽 include 的使用
{% include "police/module/carousel.html" with imgs=imgs div_id='#carousel-index' %}
此處
carousel.html
為需要利用的模板文件, 這里主要是用來展示輪播圖, 其中imgs
和div_id
為需要傳入該模板的變量.
自定義tag的使用
添加目錄和文件
在django app目錄中添加一個templatetags
Python包(Packages), 注意是包, 非目錄.如下圖所示:
image.png
這里我在應(yīng)用下的templatetags
中新建了一個文件custom_tags.py
.
在該文件內(nèi)添加了以下內(nèi)容:
# -*- coding: utf-8 -*-
from django import template
register = template.Library()
@register.filter(name='range1')
def range1(value):
value += 1
return range(1, value)
在模板文件中使用
當前應(yīng)用的templates目錄的模板文件頭添加下面這行:
如: polls/templates/polls/index.html
{% load custom_tags %}
然后, 你就可能使用了, 如:
{% for p in page.totalPages|range1 %}
<li> {{ p }}</li>
{% endfor %}