django 'url'模板标签错误
我的 URLconf 包含此模式:
url(r'^accounts/logout/$','django.contrib.auth.views.logout', name="logout"),
并且我尝试在具有如下 URL 标记的模板中反转该模式:
<a href="{% url logout next_page=request.path %}">logout</a>
但我不断收到以下错误:
Reverse for 'logout' with arguments '()' and keyword arguments '{'next_page': u'/first-page/child/'}' not found
我认为 django.contrib.auth.views.logout
应该采用选项 next_page
参数。我确信我错过了一些明显的东西,但我不确定它是什么。
My URLconf contains this pattern:
url(r'^accounts/logout/
And I've trying to reverse that in a template with the URL tag like this:
<a href="{% url logout next_page=request.path %}">logout</a>
But I keep getting the following error:
Reverse for 'logout' with arguments '()' and keyword arguments '{'next_page': u'/first-page/child/'}' not found
I thought django.contrib.auth.views.logout
is supposed to take an option next_page
parameter. I'm sure I'm missing something obvious, but I'm not sure what it is.
,'django.contrib.auth.views.logout', name="logout"),
And I've trying to reverse that in a template with the URL tag like this:
But I keep getting the following error:
I thought django.contrib.auth.views.logout
is supposed to take an option next_page
parameter. I'm sure I'm missing something obvious, but I'm not sure what it is.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
是的,你是对的,django.contrib.auth.views.logout确实接受一个可选的“next_page”参数,但不要忘记“url”标签匹配urlconf模式,而不是视图,所以它不知道什么是或者不是视图的参数。因此,这表明您需要在上述模式的正则表达式中使“next_page”成为命名组,您可以这样做,但是有一种更简单的方法来处理重定向...
查看 django.contrib.auth.views.logout,您可以在如果缺少“next_page”参数,视图将重定向到 request.GET 或 request.POST 中提供的带有“redirect_field_name”键的任何 url,该参数默认为“REDIRECT_FIELD_NAME”,而该参数又默认为字符串“next” 。因此,保持 urlconf 不变,您可以在模板中执行以下操作:
Yes you're right, django.contrib.auth.views.logout does accept an optional "next_page" parameter, but don't forget that the "url" tag matches to urlconf patterns, not views, so it's not aware of what is or isn't a parameter of a view. So this suggests that you need to make "next_page" a named group in the regexp for the above pattern, which you could do, but there's an easier way to handle redirects...
Looking at django.contrib.auth.views.logout, you can see that in the absence of a "next_page" parameter, the view redirects to whatever url is provided in either request.GET or request.POST with the key "redirect_field_name", a parameter which defaults to "REDIRECT_FIELD_NAME" which in turn defaults to the string "next". So leaving your urlconf the way it is, you can do something like this in your template:
基本上,Django 的 URL 调度程序会查看 urlconf 和该参数,并说“我不知道将该参数放在哪里”,因为它不查看 url 指向的视图函数,只查看 urlconf 和其中的模式。
现在,您的 url 模式中没有该参数的位置。
即,如果您为其编写自己的模式,或者从您自己的视图调用它,但不是从其默认的 url 模式调用它,则可以使用额外的参数调用 django.contrib.auth.views.logout 。
这些 url 模式之一可能适合您(未经测试):
希望有帮助!
Basically Django's URL dispatcher is looking at the urlconf and that argument and saying "I don't know where to put this argument" because it doesn't look at the view functions the urls point to, only the urlconf and the patterns in it.
Right now there's no place in your url pattern for that argument.
i.e. you can call django.contrib.auth.views.logout with the extra arguments if you write your own pattern for it or if you call it from your own view, but not from its default url pattern.
One of these url patterns might work for you (not tested):
Hope that helps!