Note the pairing of the Django version with the Python version. I use Python 3.8 and Django 2.2.1
The routing configuration
1. Path () function
- In creating urls.py in the project
# 1. Import from Django. urls import path # 2. Syntax -path (route, views, name=None) # 3. Parameter route: it is a string of characters. Matched request path views: Specifies the name of the view processing function corresponding to the path. Name: Specifies an alias name for the address, which is used for reverse address resolution in the templateCopy the code
The path – summon up
- Syntax: < Call up type: custom name >
- Function: If the converter type matches the data of the corresponding type. The data is passed to the view function as a keyword
- Example: path (‘path/int:page’, views.xxxx)
- example
- Urls.py in the project:
path('page/<int:pg>', views.pagen_view),
Copy the code
Import from. Import ViewCopy the code
- In the project veiws.py:
Def pagen_view(request, pg): HTML = '%s' % (pg) return HttpResponse(HTML)Copy the code
HTTP import HttpResponse, request from Django. HTTP import HttpResponse, requestCopy the code
run
Python manage.py runServer 9999Copy the code
- Just type in an integer, ok, whatever you want as long as it’s an integer
Small exercises (small calculator)
- Define a route in the following format:
- http://127.0.0.1:9999/ Integer/Operation The character string is [add/ SUD /mul]/ an integer
- The data is extracted from the route and returned to the browser
- The effect is as follows:
- Enter: 127.0.0.1:99999/100 /add/200
- The result is 300
# views.py def cal_view(request, n, op, m): if op not in ['add', 'sub', 'mul']: return HttpResponse('your op is wrong') result = 0 if op == 'add': result = n + m elif op == 'sub': Result = n -m elif op == 'mul': result = n * m return HttpResponse(' result is: %s' % (result))Copy the code
# urls.py
urlpatterns = {
path('<int:n>/<str:op>/<int:m>', views.cal_view),
}
Copy the code
2.re_path()
- In the url matching process, regular expression can be used for accurate matching
- Grammar:
- re_path(reg, view,name== xxx)
- Regular expressions for named grouping patterns (? Ppattern); Match the extracted parameters and pass them to the view function by keyword
- example
Match http://127.0.0.1:8000/20/mul/40 # # can match http://127.0.0.1:800/200/mul/400 urlpatterns = [path (' admin/', Admin. Site. Urls), re_path (r '^ (? P < x > \ d {1, 2})/(? P < op > \ w +)/(? P < y > \ d {1, 2}) $', views. Cal_view),]Copy the code
- practice
- Sample effect:
- http://127.0.0.1:9999 /birthday/ four digits/one or two digits/one or two digits
- http://127.0.0.1:9999 /birthday/ One or two digits/one or two digits/four digits
URL and view functions
URL – structure
- Definition – Namely, Uniform Resource Locator
- Function – Indicates the address of a resource on the Internet
- The general syntax format of a URL is (note: [] represents the content of the URL can be omitted) :
- protocol://hostname[:port]/ path[?query] [#fragment]
- TXXX. XXXX. Cn/video/showV…
Protocol (protocol)xxx.xx.com
- HTTP Access to the resource through HTTP. Format: http://
- HTTPS Access the resource using secure HTTPS in the format of https://
- File resources are files on the local computer. Format: file:///
Hostname (hostname)
- It is the domain name System (DNS) host name, domain name, or IP address of the server on which resources are stored
Port (Port number)
- Integer, optional, omitted use the default port of the scheme
- Each transport protocol has a default port number.
Path (Routing address)
- A string separated by zero or more “/” symbols, usually used to represent a directory or file address on a host. The routing address determines how the server handles the request
Query
- /video/showVideo? menuld=657421&vaersion=AID999
- Optional. Used to pass parameters to dynamic web pages, can have multiple parameters, separated by “” “& “”, each parameter name and value separated by” = “symbol.
Fagment (Piece of information)
-version = AID 9999 # subject
- A string that specifies a fragment in a network resource. For example, if there are multiple definitions in a web page, you can use fragment to locate a specific definition
How does Django handle URL requests
Processing URL requests
- Browser address bar –>http://127.0.0.1:8000/page/2003/
-
1.Django uses ROOT_URLCONF to find the main routing file from the configuration file. By default. Urls for this file in the project’s namesakes directory; For example mysite1 mysite1 / urls. Py
-
2.Django loads the urlpattrens variable in the main routing file [array containing many routes]
-
3. Match paths in urlPatterns in turn to the first appropriate interrupt subsequent match
-
4. Match – Call the corresponding view function to process the request and return the response
-
5. The match fails – a 404 response is returned
-
The view function
- View functions are functions that receive a browser request (an HttpRequset object) and return the response via an HttpResponse object. This function accepts the browser request and returns the corresponding response content to the browser based on the business logic
- grammar
Def xxx_view(request[, other parameters....) ) Return HttpResponse objectCopy the code
- example
/ views.py from django. HTTP import HttpResponse def page1_view(request): This is the first page </h1>' return HttpResponse(HTML)Copy the code