美文网首页
5. Django --- Django 模板的使用

5. Django --- Django 模板的使用

作者: HelloJames | 来源:发表于2018-01-06 20:16 被阅读28次

模板标签 include 的使用

{% include "police/module/carousel.html" with imgs=imgs div_id='#carousel-index' %}

此处carousel.html为需要利用的模板文件, 这里主要是用来展示轮播图, 其中imgsdiv_id为需要传入该模板的变量.

自定义tag的使用

添加目录和文件

在django app目录中添加一个templatetags Python包(Packages), 注意是包, 非目录.如下图所示:

image.png

这里我在应用下的templatetags中新建了一个文件custom_tags.py.
在该文件内添加了以下内容:

# -*- coding: utf-8 -*-

from django import template

register = template.Library()


@register.filter(name='range1')
def range1(value):
    value += 1
    return range(1, value)

在模板文件中使用

当前应用的templates目录的模板文件头添加下面这行:
如: polls/templates/polls/index.html

{% load custom_tags %}

然后, 你就可能使用了, 如:

{% for p in page.totalPages|range1 %}
    <li> {{ p }}</li>
{% endfor %}

相关文章

网友评论

      本文标题:5. Django --- Django 模板的使用

      本文链接:https://www.haomeiwen.com/subject/pnhenxtx.html