请求周期:
url > 路由 > 函数或类 > 返回字符串或模板语言
Form 表单提交:
先处理模板语言再讲HTML发出去
提交 > url > 函数或类中的方法 ————
- httpResponse() |
render(request) |
redirect(‘/index‘) |
用户 < 返回字符串 <——
注:当为redirect时,自动发起另外的请求
ajax
$.ajax({ url:‘/index‘, data: {‘k‘:1,‘f‘:1}, type:‘POST‘, dataType:‘JSON‘, traditional:true, success:function(d){ location.reload #刷新 location.href = ‘某个地址‘ #跳转,代替redirect } })
路由系统URL
1.
a. /index/
b. /index/(\d+)
c. /index/(?P<nid>\d+)
d. /index/(?P<nid>\d+) name = ‘root‘
reverse()
{% url ‘root‘ 1%}
e. /crm/ include(‘app01.urls‘)
f. 默认值
/index/ {‘web‘:‘root‘}
def func(request,web)
return ...
g.命名空间
/admin/ include(‘app01.urls‘,namespace=‘author‘)
/crm/ include(‘app01.urls‘,namespace=‘publisher‘)
app01.urls
/index/ name = detail
reverse(‘author‘:detail)
2.
def func(request):
request.POST
request.GET
request.FILES
request.method
request.path_info
return render,HttpResponse,redirect
3.Views
request.body
request.POST
request.GET
request.FILES
requset.xxxx.getlist
request.Meta
request.method
request.path_info
request.COOKIES
-请求的其他信息
from django.core.handles.wsgi import WSGIrequest
a = ‘中国‘
return HttpResponse(a)
return render
return redirect
response[‘name‘] = ‘arnol‘ #返回到响应头
response.set_cookie()
return response
装饰器
FBV
CBV
from django.utils.decorators import method_decorator
@method_decorator(装饰函数)
@method_decorator(装饰函数)
def dispatch(self,request):
return super(类名,self).dispatch(request,*args,**kwargs)
@method_decorator(装饰函数,name=‘dispatch‘)
class Order(Views.view)