- The URL configuration 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.
# example: urlpatterns = [path (route, view, Kwargs=none, Name=none), Re_path (regular expression, views view function, parameter, alias),]# parameter description: # a regular expression String # A Callable object, usually a view function or a string that specifies the path of the view function # Optional default parameter to pass to the View function (dictionary form) # an optional name parameter # example one: # Mysite/mysite/urls.pyfrom Django. URLs import path, re_pathfrom blog Import viewsurlpatterns = [path (' Cur_time ', views.cur_time), Path (' UserInfo ', vi Ews.userinfo), Re_path (R ' ^articles/2003/$ ', views.special_case_2003), Re_path (R ' ^articles/([0-9]{4})/$ ', views.year_archive) Re_path (R ' ^articles/([0-9]{4})/([0-9]{2})/', views.month_archive)]# mysite/blog/views.pydef SPECIAL_CASE_2003 (req): Return HttpResponse (' 2003 ') def year_archive (req, y): # parameter Y receive: ([0-9]{4}) incoming value return H Ttpresponse (' year ': y) def month_archive (req, Y, m): Return HttpResponse (' Year: ' +y+ ' month: ' +m ') # parameter Y receive: ([0-9]{4}) Incoming value, m receive ([0-9]{2}) # example two: Named groups# preliminary knowledge: Regular expression Import Reret = Re.search (' (? P<ID>\D{3})/(? P<name>\w{3}) ', ' wwwwwfffttt555/ooo ') print (Ret.group ()) # Output: 555/oooprint (ret.group (' id ')) # output: 555print (Ret.group (' name ')) # output: ooo# mysite/mysite/urls.pyfrom Django.urls import path, re_pathfrom blog Import viewsurlpatterns = [Re_path (R ' ^articles/(? P<YEAR>[0-9]{4})/(? P<MONTH>[0-9]{2})/', views.month_archive)]# mysite/blog/views.pydef month_archive (req, year, month): # here The parameter must be year,month return HttpResponse (' Year: ' +year+ ' month: ' +month ') # example three: # Mysite/mysite/urls.pyfrom Django.urls Impor T path, re_pathfrom blog Import viewsurlpatterns = [Re_path (R ' ^index/(? P<NAME>[0-9]{4})/', Views.index, {' name ': ' Xiaohu '})]# Mysite/blog/views.pydef index (req, name): # Here the parameter must be Name return HttpResponse (name) # parameter {' name ': ' Xiaohu '} overwrites the parameter in the URL # example four: # Mysite/mysite/urls.pyfrom Django.urls Import path, re_pathfrom blog Import viewsurlpatterns = [Re_path (R ' ^index/', Views.index, name= ' book ') # name means URL Alias Re_path (R ' ^/movie/index/', Views.index, name= ' book ')]# Mysite/blog/views.pydef Index (req): If req.method== ' POST ': username=req. Post.get (' username ') pwd=req. Post.get (' pwd ') if username== ' Xiaohu ' and pwd== ' 123 ': return HttpResponse (' Login successful! ') Return render (req, ' login.html ') # mysite/templates/login.html<body> <!--compared to action= '/index/', the URL address is more flexible-- <form action= "{% url ' book '%}" method= "POST" > <input type= "text" name= "username" > <input typ e= "Password" name= "pwd" > <input type= "Submit" value= "Submit" > </form></body># Access Address: http://l Ocalhost:8080/indexhttp://localhost:8080/movie/index
Resources
URL of the Django framework