Django's MTV stands for: Model: Object (ORM) template for Business objects and databases: responsible for presenting the page to the user view: responsible for the business logic and Call model and template when appropriate. In addition, Django has a URL dispatcher that distributes page requests for URLs to different view processes, and the view calls the corresponding model and templatefirst, the URL controllerurls.py file 1, basic configurationURL(R ' ^index/', Views.index) URL: Matches the path portion of the URLTypically, you add 1URL(R ' ^$/', Views.index), on Access, if there is only a domain name, no path will be found for index//127.0.0.1:80801 Once the match is successful no longer continues2 to capture a value from a URL, you only need to place a pair of parentheses around it. Grouping3 You do not need to add a leading backslash, because each URL has a. For example, it should be ^articles rather than ^/articles. 4 the ' R ' in front of each regular expression is optional but is recommended to add. 2. A packet used to capture a value captured in a URL is always a stringNameless GroupURL (r ' ^articls/(\d{4})/(\d{2})$ ', views.archive), passed to the view as positional parameters: Def archive1 (request,a,b): Famous Groupurl (R ' ^articls/ (? P<y > \d{4}) / (? P<m > \d{2}) $ ", Views.archive2), passed as a keyword parameter to the view function: Def archive2 (request,y,m): 3. Reverse parse URL name parameter, aliasURL (r ' ^login/', Views.login,name= "Login"), in the template:<Ahref='{% url ' Login ' %}'>click</a> The benefits of doing this: usually 1R ' ^login/' corresponds to many parts of HTMLModify the regular in the URLR ' ^login/', you don't have to change the URL of the form in the HTML4, include when the URL is more, easy to confuse, you can use the Include method to configureFrom django.conf.urls import url,includeurlpatterns = [URL (R ' ^app01/',include(' App01.urls ')), ]to create URLs in a app01 filethe URL requested by the user is also the beginning of App01 Second, view (views)Back-end logic, returning the response1.Request learned method: Request.method--Get Specific request methods: Get/post, etc.request. Post.get ()--takes the parameter in the submitted data #get ("key", 1) takes the key value, if not, fetch to 1 request. Post.getlist ()-The data type of the submitted listrequest. Get.get ("key")--take the parameters inside the URLrequest. Get.getlist ("key")--take value, value is Listrequest. Cookies.get ("key")--Get specific cookie valuerequest.sessionRequest.get_full_path ()--Get the URL of the request (including the rear?)Request.path_info--- take to access the directory, "/index/", excluding the back? parameters, etc.Request.path and Path_info Request.body--- byte string, the body of the table request message. It is useful when dealing with non-HTTP forms of messages, such as binary images, Xml,json, and so on. Request.is_ajax ()--judging is not an AJAX request request. Files---> dictionary-like objects that contain all the uploaded file information. Note that FILES contain data only if the requested method is post and the <form> with enctype= "Multipart/form-data" is present . Otherwise, FILES will be an empty dictionary-like object. 2, novice three-piece set from django.shortcuts import httpresponse # Return the string contents directly with return HttpResponse ("string") from django.shortcuts import render #渲染returns the HTML file,return render (request, "login.html",) when the parameter is passed:return render (request, "login.html", {"user_lst": Variable name}) from django.shortcuts import redirect #重定向return Redirect ("http://www.oldboyedu.com";) can be absolute or relative Render Renderingrender (Request, template_name[, context])Combines a given template and a given context dictionary, and returns a rendered HttpResponse object. is rendered before returning to the render(Request, ' xx.html ',locals ()) #把当前函数里的所有局部变量都传进去 #本质是:t=template ("C=context ({"current_date": C})Html=t.render (c)return HttpResponse (HTML)redirect redirect to a pathTwo request ProcessThe process of two requests can be seen in the browser's control window networkthe difference between render and redirect
when you log in, go through the process:first time Request:request url:http://127.0.0.1:8000/login.html/ GET no request data login.html/-------> Views.login------>login ()respond to a login.html pageSecond Request:Request url:http://127.0.0.1:8000/login.html/ POST has request data {"User": "Alex", "pwd": "123"} login.html/-------> Views.login------>login ()Response return Redirect ("/index/") to the browser, notifying the browser to send the request again: "/index/"request for the third time:request url:http://127.0.0.1:8000/index/ GET request no data index/--->url (r ' ^index/', Views.index),---->index ()respond to a index.html
MTV Model-urls and view