【问题标题】:URL pattern with multiple names具有多个名称的 URL 模式
【发布时间】:2015-10-01 14:42:43
【问题描述】:

是否可以为一个 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')), #???

【问题讨论】:

    标签: django django-urls


    【解决方案1】:

    不,不能使用元组作为 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/。

    【讨论】:

      猜你喜欢
      • 2012-05-03
      • 2015-11-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-08-12
      • 2014-02-03
      • 1970-01-01
      相关资源
      最近更新 更多