In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly explains "the classification of Django routing system and the configuration of URLconf". The content of the explanation is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "the classification of Django routing system and the configuration of URLconf".
Static and dynamic routin
Static route: a route that has been clearly defined, for example, in the following example, the user can only match this route by typing / articles/2003/ on the browser, and nothing else can match this section.
Urlpatterns = [path ('articles/2003/', views.special_case_2003),]
Dynamic routing: only routing rules are defined, such as only entering numbers, or characters of a specific arrangement or length, etc. You don't know what the user will enter, as long as it meets your rules.
URLconf configure Django2.0 basic format from django.urls import pathfrom. Import viewsurlpatterns = [path ('articles/2003/', views.special_case_2003), path (' articles//', views.year_archive), path ('articles///', views.month_archive), path (' articles////', views.article_detail),]
The views.py view for the following routes is as follows:
Def special_case_2003 (request): return HttpResponse ("dddd") def year_archive (request,year): return HttpResponse ("year_archive" + str (year)) def month_archive (request,year,month): return HttpResponse ("month_archive% s def month_archive% (year,month)) def article_detail (request,year,month,slug): return HttpResponse (" article_detail% s% s "% (year,month,slug)
Description:
To capture a variable from URL, use angle brackets ()
Variables that need to be captured can optionally contain a conversion type. For example: use to capture an integer parameter. If there is no converter, match any characters that do not contain (/)
No you have to precede URL with a slash /. For example: articles instead of / articles
Example of request:
/ articles/2005/03/ request will match the third rule
The / articles/2003/ request matches the first rule instead of the second
The / articles/2003 request will not match any rules because the URL does not end with a slash /
/ articles/2003/03/building-a-django-site/ will match the last rule
Default Path converters:
Str: matches any non-empty string without the path delimiter /
Int: matches any integer
Slug: matches any letter, number, underscore (_), and hyphen (-). For example: building-your-1st-django-site
Uuid: matches the UUID string. For example: 075194d3-6885-417e-a8a8-6c931e272f00
Path: matches any non-empty string, including the path delimiter /
Custom Path Converter
A custom Path Converter must contain the following elements:
-A class attribute named `regex`-a `regex` method (self, value) `. This method means that the matching string is converted to the type that should be passed to the view function-a `to_url (self, value) `method. This method handles converting the Python type to the string class FourDigitYearConverter to be used in URL: regex ='[0-9] {4} 'def to_python (self, value): return int (value) def to_url (self, value): return 'd'% value
You need to use register_converter () to register the custom Converter in Django:
From django.urls import register_converter, path from. Import converters, views register_converter (converters.FourDigitYearConverter, 'yyyy') urlpatterns = [path (' articles/2003/', views.special_case_2003), path ('articles//', views.year_archive),...] include sub-url
When there are multiple app, each app can have its own urls.py, which can be simply include in the top-level urls.py.
From django.urls import include, pathurlpatterns = [#... Snip... Path ('community/', include (' aggregator.urls')), path ('contact/', include (' contact.urls')), #... Snip...]
When django matches url, whenever it encounters the include () syntax, it will divide the url into two parts, such as url in the above code. As long as it matches community/, it will throw the whole url to the include ('aggregator.urls') child urls.py. The sub-urls.py is responsible for matching the following parts.
Reduce repetitive url
If there are many repeating parts of the url, you can aggregate them as follows
From django.urls import include, pathfrom apps.main import views as main_viewsfrom credit import views as credit_viewsextra_patterns = [path ('reports/', credit_views.report), path (' reports//', credit_views.report), path ('charge/', credit_views.charge),] urlpatterns = [path (', main_views.homepage), path ('help/', include (' apps.help.urls')), path ('credit/') Include (extra_patterns)),]
In this example, the / credit/reports/ request will be processed by the credit_views.report () method
Pass extra parameters to viewsfrom django.urls import pathfrom. Import views urlpatterns = [path ('blog//', views.year_archive, {' foo': 'bar'}),]
In this example, for the / blog/2005/ request, Django will call views.year_archive (request, year='2005', foo='bar')
Thank you for reading, the above is the content of "the classification of Django routing system and the configuration of URLconf". After the study of this article, I believe you have a deeper understanding of the classification of Django routing system and the configuration of URLconf, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.