django path converters

定义路径转换器

app01.converters.py

class PhoneNumberConverter:
    regex = '1[3,5,8,7][2-9][0-9]{8}'

    def to_python(self,value):
        return str(value)

    def to_url(self,value):
        return '%s'%(value)

app01.urls.py

from django.urls import path,include,register_converter
from . import views,converters

register_converter(converters.PhoneNumberConverter,'phoneNumber')

urlpatterns = [
    path('index',views.index,name="index"),
    path('myview',views.myview,name="myview"),
    path('',views.phone)
]

app01.views.py

def phone(request,phone):
    return HttpResponse(phone)

你可能感兴趣的:(django path converters)