首页 技术 正文
技术 2022年11月23日
0 收藏 837 点赞 4,180 浏览 2729 个字

使用终端,可以更方便的去实验,但是没有提示信息:

在项目目录下:

D:\MyPython\day23\HelloWorld>python manage.py shell

开始实验:

>>> from django.template import Context,Template
>>> t=Template("hello{{name}}")
>>> c=Context({"name":"lad"})
>>> t.render(c)
结果为
u'hellolad'

其内部实现大概如此。将模板文件读取,和传入的content字典进行渲染,然后使用HttpResponse

将内容返回给用户。

使用HTTPResponse实现:

from django.shortcuts import render,HttpResponse
from django.template.loader import get_template
from django.template import Context,Template
import datetimedef cur_time(req):
t=get_template("cur_time.html")
curtime = datetime.datetime.now()
c=Context({'ctime':curtime})
html = t.render(c)
return HttpResponse(html)

模板语言的使用:

1.{{ 变量名 }}

若是函数没有传递该变量,也不会报错,只是认为不存在,为空

可以使用default模板函数:

{{ name|default:”没有” }}

2.对于列表,字典,对象等传参,可以使用万能’  .  ‘句点号来获取数据

{{ 列表.索引 }}{{字典.键}}{{对象.成员变量}}

3.调用模板语言自带函数,使用管道调用函数处理,使用冒号进行传参

{{ name|default:"空的" }}{{ name|add:}}{{ name|upper}}{{ name|lower}}{{ name|cut:' ' }}将name变量中的空格剔除{{ val|date:'Y-m-d' }} # import datetime #val=datetime.datetime.now()将数据格式化输出{{ 列表|first }}#或字典等{{ 列表|length}}
.....

4.{%  %}

for循环

{% for item in 字典 %}  {{ forloop.counter0 }}:{{item}}{% endfor %}

if判断

{% if 条件 %}  ......
{% elif 条件%}  ......
{% else %}  ......{% endif %}

csrf令牌使用:(只会出现在表格传递中)

<form action="" method="post">
<input name="name" type="text"/>
<input name="age" type="text"/>
{% csrf_token %}
<input type="submit">
</form>
    <form action="" method="post">
<input name="name" type="text"/>
<input name="age" type="text"/>
<input type='hidden' name='csrfmiddlewaretoken' value='pbBjEfV8sp10gsVZ3ymi5E8Mqbl8PIkf' />
<input type="submit">
</form>

此处注意对于render是可以直接使用csrf_token,成功生成令牌

但是对于render_to_response,需要进行处理context_instance,不然前端不会生成令牌

    return render_to_response("post1.html",locals(),context_instance=RequestContext(request))

补充:ajax post使用令牌:

一种是form中包含令牌数据,我们$(“#fm”).serialize可以直接序列化数据传递即可

另外一种需要我们自己添加数据时,手动添令牌:简便方法:

            $.ajax({
url:"",
data:{p:val,csrfmiddlewaretoken: '{{ csrf_token }}'},
type:"POST",
dataType:"json",
success:function(data){
alert("")
}
})

autoescape 输出原生代码,不是转义后的代码:

#value6='<a href="#" rel="external nofollow" >跳转</a>'{{ value6 }}{% autoescape off %}
{{ value6 }}
{% endautoescape %}
#若是不含autoescape,输出的只是字符串,不具有跳转功能,因为为了保证信息安全性,会默认自动将输出数据进行转义

或者使用:

{{ value6|safe }}

而在tornado中则是使用:

{% autoescape None %}
或者
在简单表达语句 {{ … }} 写成 {% raw …%}

url:  {% url “ll” %}其中ll是别名

该函数在使用别名是用到

with:  使用更简单的变量名替换复杂变量名

{% with 简单 = 复杂 %}
{{ 简单 }}
{% with n = name %}
{{ n }}

…..

自定义模板标签(函数):

重点:在APP中目录下创建templatetags目录,放置自定义函数

目录:

blog    APP目录
templatetags 目录存放自定义函数文件
my_func.py  文件
from django import templateregister = template.Library()   #register的名字是固定的,不可改变@register.simple_tag #装饰器
def my_add100(v1):
return v1+

调用:

{% load my_func %}
<h1>{% my_add100 %}</h1>  #输出104

自定义filter(函数):

其他一致,只是改变了装饰器,调用方式也改变

@register.filter
def myadd101(v1,v2):
return v1+v2
<h1>{{ num|myadd101: }}</h1>  #num会作为第一个参数

对于filter只可以插入一个其他参数,simple_tag可以插入多个参数,用空格隔开

但是在条件判断时:filter可以使用,

{% if num|myadd101: ==  %} #num=
<h1>filter ok</h1>  #可以显示
{% endif %}

simple_tag不能,两者各有利弊

注意:在使用自定义模块函数后,需要重启项目

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:8,965
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,486
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,331
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,114
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,747
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,781