首页 > python web
时间:2020-07-01 python web 查看: 1393
Django默认Path转换器
step1 . 在urls.py 的同级目录下,创建converters.py
class Year_Converters():
  regex = '\d{4}'
  def to_python(self,value):
    return int(value)
  def to_url(self,value):
    # return ;04d' % value
    return str(value)step 2 注册converters 在同级urls,py 文件
from django.urls import path,register_converter
from . import views
from . import converters
 #注册转换器
 register_converter(converters.Year_Converters,'year')
 urlpatterns = [
  path('show1/<year:arg>', views.show1),
]正则 在urls,py 文件中,注意参数需要加() ,这里的参数是元组
from django.urls import re_path
urlpatterns = [
  re_path('article/(\d+)/', views.index),
]正则关键字
re_path(r'^show3/(?P
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持python博客。