【问题标题】:Django Url regex NoReverseMatch errorDjango Url 正则表达式 NoReverseMatch 错误
【发布时间】:2015-07-10 04:13:05
【问题描述】:

有这个 html,我想在按下该 url 时将 2 个参数传递给函数:

html页面

{% for n in notifications %}
                    <li style="..."><b>{{ n.title }}</b> - {{ n.description }}
                    <a href="{% url 'update-notify-status' n.id n.title %}" >Don't show again</a>
{% endfor %}

urls.py

url(r'^notification_htmlv2/update_status/([0-9]{4})/([0-9]{4})$', 'Notifications.views.updateStatus',
                       name="update-notify-status"),

views.py

def updateStatus(request,noteId,noteTile):
q=History.objects.filter(notification_id=noteId,title=noteTile).order_by('-id')[0]

当我启动程序时,它会给出“NoReverseMatch”错误。 我按照这个例子:https://docs.djangoproject.com/en/1.8/topics/http/urls/

url的章节反向解析

【问题讨论】:

  • n.title 可能是一个不会被 ([0-9]{4}) 匹配的字符串,你需要这样的东西:[\w-]+

标签: python html regex django


【解决方案1】:

我打赌n.idn.title 都不匹配([0-9]{4})

您应该更新您的 url 模式以处理任何可能的 idtitle 值。

类似:

r'^notification_htmlv2/update_status/([0-9]+)/([a-zA-Z0-9]+)$'

【讨论】:

  • 我已经添加了您的建议,但继续给出该错误。 Reverse for 'update-notify-status' with arguments '(4L, u'EUW')' and keyword arguments '{}' not found. 1 pattern(s) tried: ['notification_htmlv2/update_status/([0-9]{4})/([a-zA-Z0-9]+)$']
  • @Zecarlos 你没有替换id的模式(你还有([0-9]{4}),我建议([0-9]+)),{4}意味着它只匹配长度为4,长度你的id 是 1。+ 表示匹配一个或多个字符。
  • 现在我明白了,谢谢
猜你喜欢
  • 2016-07-30
  • 2011-03-22
  • 2013-07-26
  • 2014-07-08
  • 1970-01-01
  • 2012-07-25
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多