This article mainly introduces the Python code implementation of the Django framework, small series feel very good, and now share to everyone, but also for everyone to do a reference. Let's take a look at it with a little knitting.
First, URLconf
When a user requests a Web site through a browser, the corresponding function is found through the URL, and the URL configuration in Django is configured in the settings.py under the folder with the same name as the project.
1. Configuration
(1), test1/settings.py configuration file
The configuration of the URL is specified by default through root_urlconf in settings.py, which points to the urls.py file under Test1:
(2), test1/urls.py in the following configuration:
Attention:
Include configurations in test1/urls.py, create corresponding urls.py in their respective applications, and configure them specifically in urls.py files under each application.
Define the Urlpartterns list, store the URL () object, and the list name is fixed.
2. URL-defined syntax
The URL () object is defined in the Django.conf.urls package, and consists of two syntax structures
(1), Syntax One: contains, in the project name folder under the urls.py contains other apps urls.py
Create a urls.py file in your custom app to define the URL (), and then introduce the custom urls.py in urls.py, under the folder with the same name as the project, in order to configure urls.py to the inside of the app, which makes the data clearer and easier to maintain.
The syntax of introducing booktest/urls.py in test1/urls.py
URL (r ' ^ ', include (' app name. URLs ')), # URLs here refer to the name of the urls.py file in the respective application (booktest), and if it is urls.py, URLs
(2), syntax Two: definition, specify the corresponding relationship between URL and view function
Create urls.py within the respective application, specifying the correspondence between the request address and the view, in the following format:
URL (regular, ' view function name ')
Example:
Create a URL for the first page in booktest/urls.py:
From django.conf.urls import urlfrom booktest import viewsurlpatterns=[ url (r ' ^$ ', Views.index),]
Note: The regular section recommends using R, which means that the string is not escaped, so that you can use \ Write only one in the regular expression. It is not possible to add backslashes at the beginning, it is recommended to add backslashes at the end, because the URL of the browser is at the end plus \ and no more.
3. Get the parameter values that are carried in the URL
The requested URL is treated as a normal Python string, and the match does not include the domain name, get, or POST request parameters, such as: The request address is as follows:
http://localhost:8080/detail/1?a=10
In the above request address, the string matching the URL () function is only:detail/
If you want to bring a restful style parameter to the URL of the request, you need to use the () number in the regular expression of the URL () function, which is divided into: positional parameter, keyword parameter.
Note: Do not mix two parameter modes, only one parameter can be used in a regular expression.
(1), Position parameters
Directly using the () number, passed to the view by positional parameters
The regular expression in the URL () function is written as:
URL (r ' ^detail (\d+)/$ ', views.show_books),
The corresponding function in views.py is as follows:
def show_books (request, ID): # The ID obtained here is 1 return HttpResponse (' Show_books ')
(2), keyword parameters
With the keyword argument, the regular expression in the URL () function is grouped by the name of each group
If the URL visited is: http://localhost:8000/delete/1?a=10
The regular expression in the URL () function is written as:
URL (r ' ^delete (? p<id1>\d+)/$ ', Views.show_book),
The functions in views.py are as follows:
def show_arg (REQUEST,ID1): return HttpResponse (' show%s '%id1)
Note : If you use the keyword argument, the parameter name of the corresponding function in views.py must be named for the group in the regular expression, otherwise an error will be added.
Second, the View
1. What is a view
The view is a function in Python, and the view is typically defined in the "Application/views.py" file, which is the "booktest/views.py" in this example.
The view must return a HttpResponse object or a child object as a response. The response can be the HTML content of a Web page, or it can be a redirect (redirect) function, or a 404 error.
The first parameter of the view must be a HttpRequest object, and other parameters may include: keyword parameter or positional parameter (whichever is the other)
2. Built-in error view
(1), built-in error view
The Django built-in view of handling HTTP errors, major errors and views include:
404:page not found view
500:server Error View
If you want to see the error view instead of debugging information, you need to modify the debug item configuration information in the "test/settings.py" file
(2), 404 Errors and views
After the request address is URL-matched and no matching regular expression is found, call 404 view, which calls the 404.html template for rendering, and the view pass variable Request_path to the template, indicating the URL that caused the error.
Create 404.html in Templates
Enter the following URL in your browser:
http://localhost:8000/test/
The results are as follows:
(3), 500 Errors and views
A 500 error will occur when the code runs in the view, call the built-in error view and render using the templates/500.html template.
Third, HttpRequest object
1. HttpRequest Object
When the server receives an HTTP request, it creates a HttpRequest object based on the message, which does not require us to create it manually, directly using the server-built object. The first argument in a view function must be a HttpRequest object, which is defined in the Django.http module.
2. Properties
path: A string that represents the full path of the requested page and does not contain the domain name and Parameters section.
method: A string that represents the HTTP method used by the request, and the common values are: ' GET ', ' POST '.
In the browser, give the address to make a request using Get method, such as hyperlink.
Click the form's Submit button in the browser to initiate the request if the form's method is set to post.
encoding: A string that represents the encoding of the data being submitted.
If none is the default setting for using the browser, it is generally utf-8.
This property is writable and can be modified to modify the encoding used to access the form data, and then any access to the property will use the new encoding value.
get: A querydict type object, similar to a dictionary, that contains all the arguments for how a GET is requested.
post: Querydict type Object, similar to a dictionary, contains all the parameters of the POST request method.
files: A dictionary-like object that contains all the uploaded files.
Cookies: A standard Python dictionary that contains all the cookies, keys, and values as strings.
session: A read-and-write dictionary-like object that represents the current session and is available only when Django enables session support, as detailed in "State hold."