【问题标题】:Django url patterns having reverse lookup issues with optional parameters具有可选参数的反向查找问题的 Django url 模式
【发布时间】:2014-12-24 05:28:24
【问题描述】:

我在反向查找方面遇到了一些奇怪的问题。这是我的网址方案。

url(r'^overview/', 'ledger.views.overview', name='overview'),
url(r'^overview/(?P<tutorial>\w+)$', 'ledger.views.overview', name='overview_tutorial'),

当我调用return redirect('overview_tutorial', tutorial='tutorial') 时,它不是在加载教程版本,而是在加载普通版本,这对我来说很奇怪。我认为通过指定 url 的名称,它将使用该 url,但它与第一​​个 url 匹配。在 url 方案的末尾添加 $ 即可解决问题:

url(r'^overview/$', 'ledger.views.overview', name='overview'),
url(r'^overview/(?P<tutorial>\w+)$', 'ledger.views.overview', name='overview_tutorial'),

但我仍然不明白它为什么这样做。我真正想做的是有一个这样的 url 方案:

url(r'^overview/', 'ledger.views.overview', name='overview'),
url(r'^overview/(?P<tutorial>\w+)$', 'ledger.views.overview', name='overview_tutorial'),
url(r'^overview/(?P<success>\w+)$', 'ledger.views.overview', name='overview_success'),
url(r'^overview/(?P<error>\w+)$', 'ledger.views.overview', name='overview_error')

然后我可以重定向到适当的 url 名称并传入不同的参数。即:

return redirect('overview_success', success='True')  #or
return redirect('overview_error', error='Login failed. Please try your username/password again')

但它们都返回,好像我刚刚调用了教程视图。 (我现在意识到这是因为反向 url 查找必须构建 url,然后通过 url 模式运行它以查看它应该指向的位置)。

然后我尝试这样做:

url(r'^overview/(?P<tutorial>\w+)$', 'ledger.views.overview', name='overview'),
url(r'^overview/(?P<tutorial>\w+)/(?P<success>\w+)$', 'ledger.views.overview', name='overview_success'),
url(r'^overview/(?P<tutorial>\w+)/(?P<success>\w+)/(?P<error>\w+)$', 'ledger.views.overview', name='overview_error'),

但是当我调用return redirect("overview_success", tutorial='', success="Hooray") 时,我再次收到错误:

Reverse for 'overview_success' with arguments '()' and keyword arguments '{'success': 'Hooray', 'tutorial': ''}' not found. 1 pattern(s) tried: ['overview/(?P<tutorial>\\w+)/(?P<success>\\w+)$']

【问题讨论】:

  • 添加$锚点有什么问题?
  • 我可以添加 '$' 锚点,但是如果我指定一个名称,它不应该直接跳到那个方案吗?
  • 问题是没有$anchor,你的第一条规则总是匹配所有这些URL以overview/开头。因此,您需要使用 $anchor 标记字符串的结尾。
  • 您在编辑时遇到的错误是因为没有匹配...您告诉它名为tutorial 的命名组应该匹配\w+,但您给它一个空字符串。您还错误地使用了命名组,它们不应该用于向视图发送消息,有消息传递框架。查看有关如何使用命名组的文档:docs.djangoproject.com/en/1.7/topics/http/urls/#named-groups

标签: django django-urls urlconf


【解决方案1】:

您似乎正在尝试使用您的urlconf 来接受您想要发送给用户的消息。例如你的错误信息

return redirect('overview_error', error='Login failed. Please try your username/password again')

但这不是urlconf 中的命名组的用途。它们用于匹配 url 模式以确定要呈现的视图。因此,当您拨打redirect 时,它不仅仅是将您发送到一个新的网址,而是根据您传递的内容解析该网址。

在您的第二个示例中,您的 redirect 电话

return redirect("overview_success", tutorial='', success="Hooray")

正在尝试匹配您的 url 模式

url(r'^overview/(?P<tutorial>\w+)/(?P<success>\w+)$', 'ledger.views.overview', name='overview_success'),

就像overview//Hooray 这样的东西,你可以看到它不是一个有效的模式,因为传递给tutorial 的空字符串需要1个或多个“单词”字符。

您可以使用消息传递框架向用户发送消息。 https://docs.djangoproject.com/en/1.7/ref/contrib/messages/#module-django.contrib.messages

【讨论】:

    猜你喜欢
    • 2015-11-17
    • 2013-06-09
    • 2013-08-18
    • 2014-02-16
    • 2011-02-05
    • 2013-03-26
    • 2011-10-15
    • 2012-12-05
    • 2018-05-26
    相关资源
    最近更新 更多