1、获取用户请求数据
  1.1 request.GET 获取request.method='GET'的数据
    request.GET.get('name',None)

  1.2 request.POST 获取request.method='POST'的数据
    #一般针对传入的单个值的情形,如type='text','redio','password':
      request.POST.get('name',None)
    #一般针对传入的多个值的情形,如type='checkbox';<select multiple><option></option></select>
      request.POST.getlist('name',None)

  1.3 request.FILES 获取form表单文件
  获取文件时,POST中不带有其他信息,两者独立开来,更加方便使用。
  特别注意的是,只有当request方法是POST,且发送request的<form>有属性enctype="multipart/form-data"时,request.FILES中包含文件数据,否则request.FILES为空。

  PS:一般情形下,默认开发时,GET和POST在不同情形下使用。
    GET:获取数据
    POST:提交数据

2、checkbox等多选的内容
  request.POST.getlist()

3、上传文件

1 <form action="/login" method="POST" enctype="multipart/form-data"> # 上传文件,form标签做特殊设置
2     <input type="file" name="fafafa"/>
3 </form>
login.html:

 

 

 1 def login(request):
 2     if request.method=='GET':
 3         return render(request,'login.html')
 4     elif request.method=='POST':
 5         obj=request.FILES.get('fafafa')
 6         import os
 7         file_path=os.path.join('upload',obj.name)
 8         f=open(file_path,'wb')
 9         for i in obj.chunks():
10             f.write(i)
11         f.close()            
views.py:

 

 

 

4、FBV & CBV
  4.1 FBV(function base views) 就是在视图里使用函数处理请求

1 from django.conf.urls import url
2 from django.contrib import admin
3 from app01 import views
4 
5 urlpatterns = [
6 url(r'^admin/', admin.site.urls),
7 url(r'^index',views.index),
8 ]
urls.py:
1 from django.shortcuts import HttpResponse
2 def index(request):
3     return HttpResponse('Index')
views.py:

  4.2 CBV(class base views) 就是在视图里使用类处理请求。 

 1 from django.conf.urls import url
 2 from django.contrib import admin
 3 from app01 import views
 4 
 5 urlpatterns = [
 6     url(r'^admin/', admin.site.urls),
 7     url(r'^login',views.login),
 8     url(r'^index',views.index),
 9     url(r'^home/', views.Home.as_view()), #views.类名.as_view()是固定用法。
10 ]
urls.py:
 1 from django.shortcuts import render
 2 from django.views import View
 3 class Home(View):
 4     def get(self,request):
 5         print(request.method)
 6         return render(request, 'home.html')
 7 
 8     def post(self,request):
 9         print(request.method)
10         return render(request, 'home.html')
views.py:

  # 类要继承 View ,类中函数名必须小写。

  注:
    class View(object):
    """
      Intentionally simple parent class for all views. Only implements
      dispatch-by-method and simple sanity checking.
    """

    http_method_names = ['get', 'post', 'put', 'patch', 'delete', 'head', 'options', 'trace'] #所有的request.method方法。

从客户端接收到请求头后,会先执行View中的dispatch方法,再通过反射的方式找到对应的request.method的方法。
如果想要在dispatch中增加部分功能,可以在继承View类时,重写dispatch方法。

 1 from django.views import View
 2 class Home(View):
 3     def dispatch(self, request, *args, **kwargs):
 4     # 调用父类中的dispatch
 5         print('before')
 6         result = super(Home,self).dispatch(request, *args, **kwargs)
 7         print('after')
 8         return result
 9 
10     def get(self,request):
11         print(request.method)
12         return render(request, 'home.html')
13 
14     def post(self,request):
15         print(request.method)
16         return render(request, 'home.html')        
View Code

建议:两者都用,在不同的框架内,有些两者都支持,有些只支持其中之一。两种在生产环境中没有优先级,均可使用。



5、装饰器
后补


6、给用户返回数据的方式

  render(request, "templates内模板的文件的路径", {'k1': [1,2,3,4],"k2": {'name': '张扬','age': 73}})
  redirect("URL")
  HttpResponse(字符串)

posted on 2017-09-29 14:23  Zoe233  阅读(361)  评论(0编辑  收藏  举报