定义路径转换器
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('<phoneNumber:phone>',views.phone)
]
app01.views.py
def phone(request,phone):
return HttpResponse(phone)
网友评论