Python Learning day 71st: Django2 and 1 differences and views

Source: Internet
Author: User

Django1 vs. 2 routing differences
In Django1The URL inIn Django2In the Re_pathdjango2The path is added to the1.From Django.urlsImport path2.Regular, exact match is not supported3.Yes5a converter (intStr, Slug, PathUuid)4.Custom Converters:1Write a class:ClassTest:regex =' [0-9]{4} 'def to_python (self, value): #  write a bunch of processing value=value+ ' AAA ' return valuedef to_urlreturn  '%04d '% value2 from django.urls Span class= "Hljs-keyword" >import register_converter3 register_converter  (Test, "TTT" 4 Path ( ' index/<ttt:year> ', Views.index,name= ' index '),  
MVC and MTV
    M          T                 Vmodels   template         viewsM          V                 C(路由+views)models    模板              控制器其实MVC与MTV是一样的,django中为MTV,数据交互层,视图层以及控制层
View Layer: Request Object
RequestObject:#Formform, do not write method, the default is getRequest#1What is the case with get: Request data, request page,#2With PostRequest: Submit data to the server#Request. GETDictionary# request. POST  dictionary #  type of request # print (request.method) #< span class= "bash" >  path # http://127.0.0.1:8000/ Index/ppp/dddd/?name=lqz#  protocol: IP  address and port/ path? Parameters ( data) # print ( Request.path)-->/index/ppp/dddd/#  Print (Request.get_full_path ())-->/index/ppp/dddd/?name=lqz         
Three-piece set
renderHttpResponseredirect
Jsonresponse sending a JSON format string to the front-end page
封装了jsonfrom django.http import JsonResponsedic={‘name‘:‘lqz‘,‘age‘:18}li=[1,2,3,4]return JsonResponse(li,safe=False)
CBV and FBV
CBV(ClassBaseView) andFBV (functionBaseViewCbv:from django.views Import ViewClassTest (View):DefDispatch(Self, request, *args, **kwargs):#Distribute# can add logical judgment statement obj=Super (). Dispatch (Request, *args, **kwargs)# can add logical judgment statement return objdef get (self,request): obj= render (Request,' index.html ') print (type (obj))return objdef Post (self,request):return HttpResponse (' OK ') in the URLs: Re_path (R' index/', views. Test.as_view ()),                
Simple File Upload
HtmlMedium: <form action="" Method="POST" enctype="Multipart/form-data" >User name: <inputType="Text" Name="Name" >Password: <inputType="Text" Name="Password" >Files: <inputType="File" Name="MyFile" ><inputType="Submit" ></form>viewsInClassCBV (View):def dispatch (self, request, *args, **kwargs): obj = super (). Dispatch (Request, *args, **kwargs) return obj def get (self,request): return render (Request, ' index.html ') def post (self,request): AAA = Request. files.get ( ' myfile ') with open ( Aaa.name,for line in Aaa.chunks (): F.write (line) Span class= "Hljs-keyword" >return httpresponse ( ' OK ')   

Python Learning day 71st: Django2 and 1 differences and views

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.