路由系统URL:
a /index/ --> 函数或类
b /index/(\d+) --> 函数或者类
c /index/(?P<nid>\d+) --> 函数或类(需要有参数接收)
d /index/(?P<nid>\d+) name='root' -->函数或类
resverse()
{% url 'root' 1 %}
e /crm/ include('app01.urls') 路由分发
f 默认值 /index/ {'name':'root'} def func(request, name) ...... 传值
g 命名空间
/admin/ include('app01.urls',namespace='m1')
/crm/ include('app01.urls',namespace='m1')
app01.urls
/index/ name = 'n1'
resverser('m1:n1')
视图:
请求头:from django.core.handlers.wsgi import WSGIRequest
request.environ 封装了所有用户请求信息
模板继承:一个页面只能继承一个母版
templates: {% block content %} {% end block%} 母版
{% extends 'master.html'%} {% blockn content %} .... {% end block%} 子版继承
{% include 'tag.html'%}
自定义函数:
simple_tag:
在APP下创建templatetags目录
创建任意 xx.py 文件
创建template 对象 register:
from django import template
register = template.Library()
@register.simple_tag
def temp(a1,a3):
pass
在setting中注册APP
在顶部load 文件:{% load xx.py %}
{% 函数名 arg1 arg2 ...%}
缺点:不能作为if条件
优点:参数任意
filter:
在APP下创建templatetags目录
创建任意 xx.py 文件
创建template 对象 register:
from django import template
register = template.Library()
@register.filter
def temp(a1,a3):
pass
在setting中注册APP
在顶部load 文件:{% load xx.py %}
{{ 参数1|函数名:"参数二,参数三"}} {{参数1|函数名:数字}}
缺点:参数任意,不能加空格,最多只能有2个参数
优点:能作为if条件
Cookie: 客户端浏览器上的一个文件,保存格式为键值对
request.COOKIES 表示浏览器所有的cookie
request.COOKIES.get('usrename')/request.COOKIES['username'] 获取cookie
request.set_cookie('key','value') 服务端设置cookie --> 设置cookie并且关闭浏览器之后就失效
request.set_cookie()参数:
1.key: 键
2.value: 值
3.max_age: cookie失效时间(秒)
4.expires: cookie失效时间(datetime时间)
5.path = '/': cookie的生效路径
6.domain: cookie生效域名
7.secure:https传输
8.httponly:只支持http传输,不能被javascrip获取