Get the App
SLTechnology News&Howtos  ›  Development  › 

How to implement Custom Route Converter in Django

Shulou Source: shulou.com Published: 2022-06-01 13:03:19 10月03日 Update

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!

Tags: Converter method path route attribute file regular expression content function source code article next code value interest parameter name boy buddy Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno MariaDB Redmi Shulou Tech Info Microsoft Xiaomi