美文网首页
django path converters

django path converters

作者: tglmm | 来源:发表于2020-07-10 11:18 被阅读0次

定义路径转换器

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)

相关文章

网友评论

      本文标题:django path converters

      本文链接:https://www.haomeiwen.com/subject/csbpcktx.html