Django-urls (routing)

Source: Internet
Author: User

The Django routing entry is the urlpatterns parameter of the urls.py .
Distribute requests to individual view functions or classes by locating the corresponding relationships in urlpatterns .

First, distribute the request to the function
1. You should define the processing function in the views.py of the corresponding app, such as:

1 def business_new (Request): 2     return HttpResponse ("Django")

2, import the corresponding module in the urls.py

1  from Import views

3, in the Urlpatterns to join the corresponding relationship

1 path ('app1/', views.business_new) #fbv function Base View

Ii. distribution of requests to classes
1. You should define the processing class in the views.py of the corresponding app, such as:

1  fromDjango.viewsImportView2 classBlog (View):#should inherit view3     defDispatch (self, request, *args, * *Kwargs):4         Print("before")5result = Super (blog,self). Dispatch (Request, *args, **kwargs)#distributed on request6         Print(" After")7         returnresult8 9     defGet (self,request):Ten         returnHttpResponse ("BLOG") One  A     defPost (self,request): -         Pass

2, import the corresponding module in the urls.py

1  from Import views

3, in the Urlpatterns to join the corresponding relationship

1 path ('blog/', views. Blog.as_view ()),     #CBV    class Base View

Third, the special wording of the route
1, the use of regular, such as:

1 re_path ('article-(\d+)-(\d+)/', views.article)2 Re_path ('article-(? p<article_type_id>\d+)-(? p<category_id>\d+)/', views.article) # Group

can be accessed in the same way as the address/article-1-2:
Method Two will pass 1 to article_type_id,2 to category_id

The Re_path method should be imported when using regular

1  from Import Path,re_path

2. The name attribute, such as

1 re_path ('article-(? p<article_type_id>\d+)-(? p<category_id>\d+)/', views.article,name='article')

After you set this property, you can reverse-generate the URL by using the reverse method, such as:

1 file:views.py 2  from Import Reverse 3 def article (request,**Kwargs):4     ... 5     url = reverse ('article', kwargs=Kwargs)6         Print  (URL)7...     

3, distribute to the app's URLs, such as:

 1  from  django.urls import  path,include #   import include  2  3  urlpatterns = [ 4  path ("  admin/ "  , Admin.site.urls ),  5  path ( '  app1/ " , include ( " app1.url   " ))  6 ] 

When you visit a URL that looks like address/app1/xxx, it goes into App1-url.py to find Urlpatterns

Django-urls (routing)

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.