In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-26 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly introduces Django how to achieve a custom route converter, the article introduces in great detail, has a certain reference value, interested friends must read it!
Analysis of the source code of the path converter built into django
Before we customize the route converters, let's take a look at what the route converters built into django say, source code path from django.urls
Import convertersclass IntConverter: regex ='[0-9] + 'def to_python (self, value): return int (value) def to_url (self, value): return str (value) class StringConverter: regex =' [^ /] + 'def to_python (self, value): return value def to_url (self) Value): return valueclass UUIDConverter: regex ='[0-9a-f] {8}-[0-9a-f] {4}-[0-9a-f] {12} 'def to_python (self, value): return uuid.UUID (value) def to_url (self) Value): return str (value) class SlugConverter (StringConverter): regex ='[- a-zA-Z0-9 _] + 'class PathConverter (StringConverter): regex ='. + 'DEFAULT_CONVERTERS = {' int': IntConverter (), 'path': PathConverter (),' slug': SlugConverter (), 'str': StringConverter (),' uuid': UUIDConverter (),} REGISTERED_CONVERTERS = {} def register_converter (converter) Type_name): REGISTERED_ Converse [type _ name] = converter () get_converters.cache_clear ()
From the analysis above, we can see that the built-in path converter in django first defines a class, in which a class attribute regex is defined as the value of the regular expression, then two methods to_python and to_url are defined, and finally a register_converter function is defined to register the path converter with django.
Here we divide him into five steps:
1. Create a converters.py and define a class in the file.
two。 Define a property regex in the class, which is the regular expression used to hold the url converter rules.
3. Implement the to_python (self,value) method, which converts the value in url and passes it to the view function.
4. Implement the to_url (self,value) method, which converts the passed parameters into a correct url when doing url inversion.
5. Register the defined converter with django.
Small case
Next, we define a converter that satisfies 4-digit path matching.
Create a new converters.py file with the following code:
Class FourDigitYearConverter: # define the regular expression regex ='[0-9] {4} 'def to_python (self, value): return value def to_url (self, value): return 'd'% value
Register a custom converter under the urls.py file
From django.urls import path, converters# registered custom converter register_converter (converters.FourDigitYearConverter, 'yyyy') # yyyy is the type name of the custom converter urlpatterns = [path (' articles/', views.articles_yyyy),] these are all the contents of the article "how Django implements custom route converters". Thank you for reading! Hope to share the content to help you, more related knowledge, welcome to follow the industry information channel!
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.