When requested by the user's browser, django will first access the urlpatterns variable in the "ROOT_URLCONF" configuration list of the main route in the settings.py file. This variable is an array. There are many path functions in the array. In this path function, we will configure various routes and the name of the view function bound to the route
Exercise 1:
Visit 127.0.0.1:8000 and output it in the web page. This is my home page
Visit 127.0.0.1:8000/page/1 and output it in the web page. This is the web page numbered 1
URL:
from django.urls import path from . import views urlpatterns = [ path('admin/', admin.site.urls), path('',views.index_view), path('page/1',views.page1_view)
VIEWS:
from django.http import HttpResponse def index_view(request): html="<h1>This is my home page</h1>" return HttpResponse(html) def page1_view(request): html="This is page number 1" return HttpResponse(html)
path converter:
Syntax: < converter type: custom name >
Function: if the converter type matches the data of the corresponding type, the data will be passed to the view function by keyword parameter transfer
Converter type: str, int, slug, path
Converter type | effect | Sample |
str | Matches non empty strings other than '/' | /v1/users/<str:zhangsan> |
int | Matches 9 or any integer and returns an int | /page/<int:page> |
slug | Matches segment labels consisting of any ascii alphanumeric underscore and hyphen | /detail/<slug:sl> |
path | Matches non empty fields, including the path separator '/' | /v1/users/<path:ph> |
Exercise 2:
Use the path converter to configure the route. When I enter 1, you will be prompted to visit 1. When I enter 2, the web page will prompt 2
URL:
from django.urls import path from . import views urlpatterns = [ path('admin/', admin.site.urls), #http://127.0.0.1:8000/page/3-100 path('page/<int:pg>',views.pagen_view), ]
VIEWS:
def pagen_view(request,pg): html="This is number%s Web page!"%(pg) return HttpResponse(html)
Exercise 3
Make a small calculator to realize addition, subtraction, multiplication and division, and the page displays the results. For example, addition: 127.0.0.1:8000/99/add/100
URL:
path('<int:n>/<str:op>/<int:m>',views.cal_view),
VIEWS:
#add , subtract , multiply and divide def cal_view(request,n,op,m): if op not in ['add','sub','mul','chu']: 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 else: result = n / m return HttpResponse('The result is:%s'%(result))
Upgraded version of path routing_ Path () can be used for regular expression exact matching
Syntax: re_path(reg,view,name=xxx)
Note: regular expressions are named grouping patterns (? P < name > pattern)
Exercise 4:
Make a small calculator to realize addition, subtraction, multiplication and division, and the page displays the results. For example, addition: 127.0.0.1:8000/99/add/10
Only two digit operations are required
\d: Represents an integer
\d{1,2}: represents an integer of 1 to 2 bits
(? P < x > \ D {1,2}): group name x, regular is an integer matching 1-2 bits
\w: Representation character
\w +: indicates multiple characters
(? P < op > \ W +): group name op, regular is to match multiple characters
Exercise 4
Addition and subtraction program operation for matching two bit integers
URL:
re_path(r'^(?P<x>\d{1,2})/(?P<op>\w+)/(?P<y>\d{1,2})$', views.cal2_view)
VIEWS:
def cal2_view(request,x,op,y): if op not in ['add','sub','mul','chu']: return HttpResponse('Your op is wrong') if op == "add": result = int(x) + int(y) elif op == "sub": result = int(x) - int(y) elif op == "mul": result = int(x) * int(y) else: result = int(x) / int(y) return HttpResponse('The result is:%s'%(result))
Exercise 5:
127.0.0.1:8000/birthday / four digits / one to two digits / one to two digits
127.0.0.1:8000/birthday / one to two digits / one to two digits / four digits
URL:
re_path(r'^birthday/(?P<x>\d{4})/(?P<y>\d{1,2})/(?P<z>\d{1,2})$',views.birthday_view),
re_path(r'^birthday/(?P<y>\d{1,2})\d{1,2}/(?P<z>\d{1,2)/(?P<x>\d{4})$',views.birthday_view),
VIEWS:
def birthday_view(request,x,y,z): html = 'Birthday is%s year%s month%s day'%(x,y,z) return HttpResponse(html)