URL of the Django framework

Source: Internet
Author: User

    • 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&LT;ID&GT;\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&LT;YEAR&GT;[0-9]{4})/(? P&LT;MONTH&GT;[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&LT;NAME&GT;[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

    • Python Full Stack

URL of the Django framework

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.