码迷,mamicode.com
首页 > 其他好文 > 详细

自定义转换器

时间:2020-02-02 17:25:00      阅读:90      评论:0      收藏:0      [点我收藏+]

标签:register   turn   cut   shortcut   eve   response   end   regex   reverse   

 1 from django.urls import path,register_converter
 2 from app01 import views
 3 
 4 class MyCon:
 5     regex=‘[0-9]{4}‘
 6     def to_python(self,value):
 7         return int(value)
 8     def to_url(self,value):
 9         return ‘%04d‘ % value
10 
11 register_converter(MyCon,‘aa‘)
12 
13 urlpatterns = [
14     path(‘admin/‘, admin.site.urls),
15     path(‘test/<aa:year>‘, views.test,name=‘test‘),
16 ]
1 from django.shortcuts import render,HttpResponse,reverse
2 
3 
4 def test(request,year):
5     print(year)
6     print(type(year))
7     url=reverse(‘test‘,args=(5,))
8     print(url)
9     return HttpResponse(‘ok‘)

 

自定义转换器

标签:register   turn   cut   shortcut   eve   response   end   regex   reverse   

原文地址:https://www.cnblogs.com/xuqidong/p/12252644.html

踩
(0)
赞
(0)
   
举报
评论 一句话评论(0)
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!