Django---URL, views

Source: Internet
Author: User
Tags http post

1. Django URL (routing system)

the URL configuration (URLconf) is like the directory of the Web site that Django supports. Its essence is the URL pattern and the mapping table between the view functions to invoke for that URL pattern; that's how you tell Django to call this code for that URL, and call that code for that URL.

1 urlpatterns = [2    URL (regular expression, views view function, parameter, alias),3 ]

Parameter description:

    • A regular expression string
    • A callable object, typically a view function or a string that specifies the path of a view function
    • Optional default parameters to pass to the view function (in dictionary form)
    • An optional name parameter

Example:

1  fromDjango.conf.urlsImportURL2  fromDjango.contribImportAdmin3 4  fromApp01Import views5 6Urlpatterns = [7 8URL (r'^articles/2003/$', views.special_case_2003),9 Ten     #URL (r ' ^articles/[0-9]{4}/$ ', views.year_archive), One  AURL (r'^articles/([0-9]{4})/$', views.year_archive),#no_named Group -  -URL (r'^articles/([0-9]{4})/([0-9]{2})/$', views.month_archive), the  -URL (r'^articles/([0-9]{4})/([0-9]{2})/([0-9]+)/$', Views.article_detail), -  -]
1  fromDjango.conf.urlsImportURL2   3  from.Import views4   5Urlpatterns = [6URL (r'^articles/2003/$', views.special_case_2003),7URL (r'^articles/(? P<YEAR>[0-9]{4})/$', views.year_archive),8URL (r'^articles/(? P<YEAR>[0-9]{4})/(? P<MONTH>[0-9]{2})/$', views.month_archive),9URL (r'^articles/(? P<YEAR>[0-9]{4})/(? P<MONTH>[0-9]{2})/(? P<DAY>[0-9]{2})/$', Views.article_detail),Ten]
2. Django Views (view function)

Two core objects are generated in an HTTP request:

HTTP Request: HttpRequest object

HTTP response: HttpResponse object

Location: django.http

Before we used the parameter request is the HttpRequest detection method: Isinstance (Request,httprequest)

1 properties and methods of the HttpRequest object:
1 #path: The full path of the requested page, excluding the domain name2 #3 #method: The string representation of the HTTP method used in the request. All uppercase. For example4 #5 #if req.method== "GET":6 #7 #do_something ()8 #9 #ElseIf req.method== "POST":Ten # One #Do_something_else () A # - #GET: Class Dictionary object containing all HTTP GET parameters - # the #POST: Class Dictionary object with all HTTP POST parameters - # - #It is also possible for the server to receive an empty post request, that is, the form form forms through - #The HTTP Post method submits the request, but there may be no data in the form and therefore cannot be used + #if req. Post to determine if the HTTP POST method is used; if req.method== "post" should be used - # + # A # at #cookies: A standard Python Dictionary object containing all cookies; keys and values are strings.  - # - #files: A class Dictionary object that contains all the uploaded files, and each key in files is the value of the name attribute in the <input type= "file" name= ""/> Tag, in Files Each value is also a standard Python Dictionary object that contains the following three keys: - # - #FileName : Upload file name, in string representation - #content_type: Content type for uploading files in #Content : Uploading the original contents of a file - # to # + #User : Is a Django.contrib.auth.models.User object that represents the currently logged-on user. If you access the user's current - #without logging in, user will be initialized to an instance of Django.contrib.auth.models.AnonymousUser. You're the #the user's is_authenticated () method can be used to identify whether the users are logged in: * #if req.user.is_authenticated (); only authenticationmiddleware in Django is activated $ #This property is only available whenPanax Notoginseng # - #session: The only read-write property that represents the current session's Dictionary object, which is only available if you have activated the session support in Django.  the  + #Method AGet_full_path (), for example: Http://127.0.0.1:8000/index33/?name=123, Req.get_full_path () The result is/index33/?name=123 theReq.path:/index33
View CodeNote A common method: request. Post.getlist (")2 HttpResponse objects:

For HttpRequest objects, it is created automatically by Django, but the HttpResponse object must be created by ourselves. Each view request processing method must return a HttpResponse object.

HttpResponse class in Django.http.HttpResponse

Common methods for extending on HttpResponse objects:

1 Page rendering:         render () (recommended) <br>                 render_to_response (),2 page jump:         Redirect (  " path ")3 locals ():    You can pass all the variables in the function directly to the template

Add:

1-----------------------------------url.py2 3URL (r"Login", Views.login),4URL (r"Yuan_back", Views.yuan_back),5 6-----------------------------------views.py7 defLogin (req):8     ifreq.method=="POST":9         if1:Ten             #return Redirect ("/yuan_back/") OneName="Yuanhao" A  -             returnRender (req,"my backend.html", Locals ()) -  the     returnRender (req,"login.html", Locals ()) -  -  - defYuan_back (req): +  -Name="Yuanhao" +  A     returnRender (req,"my backend.html", Locals ()) at  ------------------------------------login.html -  -<form action="/login/"Method="Post"> -<p> name <input type="text"Name="username"></p> -<p> Gender <input type="text"Name="Sex"></p> in<p> Email <input type="text"Name="Email"></p> -<p><input type="Submit"Value="Submit"></p> to</form> +-----------------------------------my backend.html - the  * #Summary: The difference between render and redirect: $ #1 if render pages require template language rendering and need to load database data into HTML, then all this partPanax Notoginseng #In addition to the view function written in Yuan_back, it must also be written in login, the code is duplicated, not decoupled. -  the #2 The most important:url does not jump to/yuan_back/, but is still in/login/, so when refreshed + #You have to log back in again.
View Code

Django---URL, views

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.