Django - route configuration

Routing configuration

  • 'root' in settings.py_ Urlconf 'specifies the file location of the main routing configuration list urlpatterns
# file: <Under the folder with the same name as the project>/urls.py
urlpatterns = [
    path('page/2003/',views.page_2003_view),
    ...   # Configure primary route here

]

 

Path() function

  • Import - from django.urls import path
  • Syntax - path(route,views,name=None)
  • Parameters:
    1. route: string type, matching request path
    2. views: Specifies the name of the view handler function corresponding to the path. [Note: do not add parentheses, otherwise the result of the function is introduced instead of the function]
    3. name: alias the address, which is used in reverse address resolution in the template

Exercise 1 - create a small website

  • Enter web address: http://127.0.0.1:8000 , output in the web page: This is my home page
  • Enter web address: http://127.0.0.1:8000/page/1 , output in web page: This is the web page numbered 1
  • Enter web address: http://127.0.0.1:8000/page/2 , output in the web page: This is the web page numbered 2
# stay urls.py In the file

urlpatterns = [

    # http://127.0.0.1:8000/
    path('', views.index_view),

    # http://127.0.0.1:8000/page/1
    path('page/1', views.page1_view),

    # http://127.0.0.1:8000/page/2
    path('page/2', views.page2_view),

]

 

# stay views.py In the file

def index_view(request):
    html = 'This is my home page'
    return HttpResponse(html)

def page1_view(request):
    html = 'This is page number 1'
    return HttpResponse(html)

def page2_view(request):
    html = 'This is page number 2'
    return HttpResponse(html)

 

reflection

What should I do to create the above 100 Web pages?

For example:
http://127.0.0.1:8000/page/3
http://127.0.0.1:8000/page/4
    ...
http://127.0.0.1:8000/page/99
http://127.0.0.1:8000/page/100

 

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.
  • Example: path ('page / < int: Page > ', views. XXX)
Converter typeeffectSample
str Matches non empty strings other than '/' 'V1 / users / < STR: username >' match / v1/users/guoxiaoao
int Matches 0 or any positive integer. Returns an int "Page / < int: Page >" match / page/100
slug Matches any short label consisting of SACII letters or numbers, hyphens, and underscores "Detail / < slug: SL >" match / detail / this is Django
path Match non empty fields, including path separator '/' "V1 / users / < path: pH >" matches / v1/goods/a/b/c
  • Enter.. / page/n in the client
  • N as much as you enter, you will output the web page numbered n
  • As long as it is digital, the output of 20000000 is no problem
# stay urls.py In the file

urlpatterns = [

    # http://127.0.0.1:8000/page/3-100
    path('page/<int:pg>', views.pagen_view),

]

 

# stay views.py In the file

def pagen_view(request, pg):
    html = 'This is number%s Web page'%(pg)
    return HttpResponse(html)

 

Exercise 2 - small calculator

  • The format of defining a route is
  • http://127.0.0.1:8000/ Integer / operation string [add/sub/mul] / integer
  • Extract data from the route and return it to the browser after corresponding operations
  • The effects are as follows:

Input: 127.0.0.1:8000/100/add/200

Page display result: 300

# stay urls.py In the file

urlpatterns = [

    # http://127.0.0.1:8000 / integer / operation string [add/sub/mul] / integer
    path('<int:n>/<str:op>/<int:m>', views.cal_view),

]

 

# stay views.py In the file

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('The result is:%s'%(result))

 

Routing configuration - re_path() function

  • In the process of url matching, regular expressions can be used for exact matching
  • grammar
    • re_path(reg,view,name=xxx)
    • The regular expression is named grouping pattern (? P < name > pattern); After matching the extracted parameters, it is passed to the view function by keyword parameter transfer

Sample

# Can match http://127.0.0.1:8000/20/mul/40
# Unmatched http://127.0.0.1:8000/20/mul/40
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),
]

\d{1,2} : Indicates match 1-2 Bit integer
\w+ : Indicates matching multiple letters or characters
# stay urls.py In the file

from django.urls import path, re_path

urlpatterns = [
    
    # Only two and two digits can be operated
    re_path(r'^(?P<x>\d{1,2})/(?P<op>\w+)/(?P<y>\d{1,2})$',views.cal2_view),
    
]
# stay views.py In the file

def cal2_view(request,x,op,y):
    html = 'x:%s op:%s y:%s'%(x,op,y)
    # Not here. Print directly a,op,y Value of
    return HttpResponse(html)

 

practice

Access address:

http://127.0.0.1:8000/birthday / four digits / one to two digits/One to two digits

http://127.0.0.1:8000/birthday / one to two digits / one to two digits/Four digit number

Final output: birthday: xxxx year xx month xx day

Effect example:

Enter web address: http://127.0.0.1:8000/birthday/2015/12/11
Displayed as: birthday: December 11, 2015

Enter web address: http://127.0.0.1:8000/birthday/2/28/2008
Displayed as: birthday: February 28, 2008
# stay urls.py In the file

from django.urls import path, re_path

urlpatterns = [
    
    # http://127.0.0.1:8000/birthday / year / April 2 / day 2
    re_path(r'^birthday/(?P<y>\d{4})/(?P<m>\d{1,2})/(?P<d>\d{1,2})$', views.birthday_view),
    # http://127.0.0.1:8000/birthday / year / April 2 / day 2
    re_path(r'^birthday/(?P<d>\d{1,2})/(?P<m>\d{1,2})/(?P<y>\d{4})$', views.birthday_view),
    
]
# stay views.py In the file

def birthday_view(request,y,m,d):
    html = "Birthday:%s year%s month%s day"%(y,m,d)
    return HttpResponse(html)

 

Posted on Sun, 31 Oct 2021 23:00:04 -0400 by allexx_d