1、直接传值
urls.py
"""mysite URL Configuration The `urlpatterns` list routes URLs to views. For more information please see: https://docs.djangoproject.com/en/1.10/topics/http/urls/ Examples: Function views 1. Add an import: from my_app import views 2. Add a URL to urlpatterns: url(r‘^$‘, views.home, name=‘home‘) Class-based views 1. Add an import: from other_app.views import Home 2. Add a URL to urlpatterns: url(r‘^$‘, Home.as_view(), name=‘home‘) Including another URLconf 1. Import the include() function: from django.conf.urls import url, include 2. Add a URL to urlpatterns: url(r‘^blog/‘, include(‘blog.urls‘)) """ from django.conf.urls import url from django.contrib import admin from cmdb import views from django.conf.urls import url, include urlpatterns = [ # url(r‘^web/‘, include(‘cmdb.urls‘)), url(r‘^template/‘,views.template), ]
views.py
def template(request): return render(request,‘template.html‘, {‘k1‘:‘v1‘, ‘k2‘:[11,22,33,44,55], ‘k3‘:{‘nid‘:12,‘name‘:‘alex‘}})
template.html
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title></title> </head> <body> {{ k1 }} {{ k2.0 }} {{ k3.nid }} </body> </html>
备注:例如以上单值传递时 索引 要使用 点 再加 一个索引值来取值。
上面列表通过for循环来取值
template.html
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title></title> </head> <body> {{ k1 }} {{ k2.0 }} {{ k3.nid }} {% for item in k2 %} <p>{{ item }}</p> {% endfor %} </body> </html>
一些例子
重复输出item内容
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title></title> </head> <body> {{ k1 }} {{ k2.0 }} {{ k3.nid }} {% for item in k2 %} <p>{{ item }},{{ item }}</p> {% endfor %} </body> </html>
输出循环序号
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title></title> </head> <body> {{ k1 }} {{ k2.0 }} {{ k3.nid }} {% for item in k2 %} <p>{{ item }},{{ forloop.counter }}</p> {% endfor %} </body> </html>
循环序号从0开始
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title></title> </head> <body> {{ k1 }} {{ k2.0 }} {{ k3.nid }} {% for item in k2 %} <p>{{ item }},{{ forloop.counter }} {{ forloop.counter0 }}</p> {% endfor %} </body> </html>
循环取第一个,或最后一个
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title></title> </head> <body> {{ k1 }} {{ k2.0 }} {{ k3.nid }} {% for item in k2 %} <p>{{ item }},{{ forloop.counter }}, {{ forloop.counter0 }},{{ forloop.first }},{{ forloop.last }}</p> {% endfor %} </body> </html>
2、判断语句在模版中
{% if 1 == 1 %} <h1>123</h1> {% endif %}
{% if k1 == ‘v1‘ %} <h1>V1</h1> {% elif k2 == ‘v2‘ %} <h1>V2</h1> {% else %} <h1>777</h1> {% endif %}
模版语言内置方法(Django自带函数)
{{ item.event_start|date:"Y-m-d H:i:s"}} #时间格式
{{ bio|truncatewords:"30" }} #显示前30个字符
{{ my_list|first|upper }} #字符串第一个字符大写
{{ name|lower }} #字符串变小字
模版语言自定义方法
filter
simple_tag
1、创建指定文件,名称不能改 templatetags
2、创建任意 .py 文件,如:xx.py
from django import template
from django.utils.safestring import mark_safe
#下面一句 在3.5.2要屏蔽,否则不能正常运行自定义方法#
#from django.template.base import resolve_variable, Node, TemplateSyntaxError
# 必须不变
register = template.Library()
# 创建函数
@register.filter
def f1(value):
return value + "666"
3、在html模版的头部执行
{% load xx %}
4、
k1 = ‘VVV‘
{{k1}} => vvv
#在html中生成 对应值
{{k1|f1}} => vvv666
5、 settings.py 中注册app
总结:
filter
限制:传参
支持:模版语言的 if 条件
simple_tag
不支持:模版语言的 if 条件
原文:http://www.cnblogs.com/yaabb163/p/6254404.html