URL 具有多个名称的模式
URL pattern with multiple names
是否可以为一个 URL 模式定义多个 names?我想合并两个视图而不查找对每个视图的所有引用并更改它们。保留这两个名字的另一个好处是,以防我以后想再次拆分它们。
例如合并
url(r'^login/', TemplateView.as_view(template_name='login.html'), name='login'),
url(r'^profile/', TemplateView.as_view(template_name='profile.html'), name='profile'),
到
url(r'^profile/', TemplateView.as_view(template_name='profile.html'), name=('login', 'profile')), #???
不,url 模式的名称不能使用元组。只需两次包含 url 模式,每次使用不同的名称。
url(r'^profile/$', TemplateView.as_view(template_name='profile.html'), name='login'),
url(r'^profile/$', TemplateView.as_view(template_name='profile.html'), name='profile'),
请注意,我用美元终止了正则表达式。没有它,正则表达式将匹配 /profile/sonething-else/
以及 /profile/
。
是否可以为一个 URL 模式定义多个 names?我想合并两个视图而不查找对每个视图的所有引用并更改它们。保留这两个名字的另一个好处是,以防我以后想再次拆分它们。
例如合并
url(r'^login/', TemplateView.as_view(template_name='login.html'), name='login'),
url(r'^profile/', TemplateView.as_view(template_name='profile.html'), name='profile'),
到
url(r'^profile/', TemplateView.as_view(template_name='profile.html'), name=('login', 'profile')), #???
不,url 模式的名称不能使用元组。只需两次包含 url 模式,每次使用不同的名称。
url(r'^profile/$', TemplateView.as_view(template_name='profile.html'), name='login'),
url(r'^profile/$', TemplateView.as_view(template_name='profile.html'), name='profile'),
请注意,我用美元终止了正则表达式。没有它,正则表达式将匹配 /profile/sonething-else/
以及 /profile/
。