Django模版
模版默认在每一个app下templates目录下寻找;
可以在setting里面的templates下dirs自定义路径
from django.shortcuts import render,render_to_response
from django import template
from django.template.loader import get_template,render_to_string
1:
t = template.Template('<html><head></head><body>{{title}}</body></html'>
c = template.Context({'title':'这是标题A'})
html = t.render(c)
return HttpResponse(html)
2:
t = get_template('books.html')
html = t.render()
return HttpResponse(html)
3:
html = render_to_string('books.html')
return HttpResponse(html)
4:
return render_to_response('books.htm;')
5(常用!):
render(requests,index.html,{name:123} 第二个参数html页面名字,第三个可传递参数到页面上{{}}表示
模版变量过滤器
默认寻找html文件都是在每一个app下面的templates目录下寻找
html中模版变量用{{}}代替,例如return render('index.html',{'name':123} {{name}}
#过滤器:
在所有内容渲染之前做一层处理:
语法:
{{变量或其他|语法}} 支持多层管道
注意事项:
千万不要用两个管道||这个在其他语言例如JavaScript为or
#常用方法(注意:这些所有的方法,都是在eturn render 里面的变量进行测试的):
length:长度
add:字符串相加,数字相加,列表,如果失败,会返回空字符串 {{变量|add: "abc"}}
upper:转换成大写 {{变量|upper}}
lower:转换成小写 {{变量|lower}}
capfirst:首字母大写 {{变量|lower|capfirst}}
cut:切割 {{name|cut:" "}} 将空格切掉
default:提供一个默认值,变量为false or '' 使用 {{变量|default:11111}}
first:返回列表的第一个值
last:返回列表的最后一个值
join:连接字符串 {{变量|join:','}}
truncatechars:切断超过多少个就会用...代替 {{变量|truncatechars:4}} 默认...占三个
truncatewords:截取单词 {{变量|truncatewords:4}}
slice:切割列表
striptags:将html标签去掉 {{变量 | striptags}}
safe:关闭变量的自动转译,如果跟上safe那么html标签会生效 {{<h1>大标签</h1>| safe}}
date:格式化日期:注意格式化的时候变量要为时间,例如(变量:datetime.datetime.now())
{{name|date}} {{name|date:'Y-m-d-G:H:i:s'}}
time:格式化时间:
只能用时间 小时,日,分,秒等
date和time过滤器格式:
Y: 四位数字的年 如1998
y: 两位数字的年 如98
m: 两位数的月 如07
n: 一位数的月 如7
d: 两位数的日 如07 29
j: 一位数的日 如7 29
g: 12小时制的一位数的小时 如7 2 9
G: 24小时制的一位数小时 如0 7 23
h: 12小时制的两位数的小时 如07 01 12
H: 24小时制的两位数的小时 如01 13 24
i: 分钟 从00-59
s: 秒钟 从00-59
#模版标签:{%tag%}{%endtag%}:
if/elif/else
and/or/in/not/==/!=/<=/>=
for in 跟python语法类似
lood:加载第三方标签最常用 加载前端文件 {%load static%}
url:返回一个命名了的url绝对路径 "{% url 'start' article_id='1' comment_id='2' %}"
with:缓存一个变量 {% with name as c %} <h2>{{c}}</h2>{% endwith %}
autoescape:开启和关闭自动转译
forloop.counter 当前迭代的次数 下标从1开始
forloop.countero 当前迭代的次数 下标从0开始
forloop.revcounter 跟forloop.counter一样下标从大到小
forloop.revcounterO 跟forloop.counterO一样 下标从大到小
forloop.first 返回bool类型 如果是第一次迭代 返回true 否则返回false
forloop.last 返回bool类型 如果是最后一次次迭代 返回true 否则返回false
forloop.parentloop 如果发生多层for循环嵌套,那么这个变量返回的是上一层的for
<ul>
{% for i in name %}
{% if forloop.first %}
<li>{{i}} true</li>
{% else %}
<li>{{i}}</li>
{% endif %}
{% endfor %}
</ul>
#模版继承:
{% extends '父模版名字' %} 需要在html最上面
{% block 自定义名称 %}{% endblock 自定义名称 %}父模版开放接口 子模版调用进行编辑,如果不定义那么,子模版中无法自定义编辑其他内容,只能继承父模版内容
{{block.super}} 如果在父模版定义的block接口中 定义了父模版的一些内容,那么在子模版调用block接口的时候,父模版接口中的内容不存在,这个时候,需要调用如上方法
#include:
可以包含一个html模版到当前模版中,和继承不同,include相当于把include的文件拷贝一份到当前位置,例如有一个includetemplate.html模版,要在subtemplaee.html模版中进行饮用,代码如下:
{% extends 'base.html' %}
{% block bodyblock %}
这是博客详情
<br/>
{% include "includetempplate.html" %}
{% endblock %} 这段代码是 直接将includetemplates 文件里面的内容直接复制进来
{% comment %}{% endcomment %} 注释
#static 静态文件:
在每一个app下默认搜索static目录
{% load static %} html文件加载static
<link rel="stylesheet" href="{% static 'index.css' %}"> 加载css 寻找static/index.css