简体   繁体   English

Django反向URL查找模板错误

[英]Django Reverse URL Lookup Template Error

I'm having a bit of trouble with reverse URL lookups in Django. 我在Django中使用反向URL查找时遇到了一些麻烦。

From the template: 从模板:

<form action="{% url 'blog:save' post.slug %}" method="post">

From urls: 来自网址:

url(r'^post/(?P<slug>\w+)/save/$', views.save, name='save'),

From views: 来自观点:

def save(request, slug):
    return HttpResponse("Not Saved.")

Error I'm getting: 我得到的错误:

Exception Type: NoReverseMatch
Exception Value:
Reverse for 'save' with arguments '(u'',)' and keyword arguments '{}' not found.

post.slug variable in your template is an empty string, but your url requires 1 or more characters ( \\w+ ). 模板中的post.slug变量是一个空字符串,但您的网址需要1个或多个字符( \\w+ )。 So Django builds /post//save/ , but this url is invalid. 所以Django构建/post//save/ ,但这个网址无效。

If you need to save a new post with no slug, use an optional subpattern in url: 如果您需要保存没有slug的新帖子,请在url中使用可选的子模式:

r'^post/(?:(?P<slug>\w+)/)?save/'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM