django 'url'模板标签错误

发布于 2024-08-04 03:50:14 字数 537 浏览 4 评论 0原文

我的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

究竟谁懂我的在乎 2024-08-11 03:50:14

是的,你是对的,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 不变,您可以在模板中执行以下操作:

<a href='{% url logout %}?next={{ request.path }}'>logout</a>

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:

<a href='{% url logout %}?next={{ request.path }}'>logout</a>
我偏爱纯白色 2024-08-11 03:50:14

基本上,Django 的 URL 调度程序会查看 urlconf 和该参数,并说“我不知道将该参数放在哪里”,因为它不查看 url 指向的视图函数,只查看 urlconf 和其中的模式。

现在,您的 url 模式中没有该参数的位置。

即,如果您为其编写自己的模式,或者从您自己的视图调用它,但不是从其默认的 url 模式调用它,则可以使用额外的参数调用 django.contrib.auth.views.logout 。

这些 url 模式之一可能适合您(未经测试):

url(r'^accounts/logout/(?P<next_page>.*)?

希望有帮助!

,'django.contrib.auth.views.logout', name="logout"), url(r'^accounts/logout/

希望有帮助!

,'django.contrib.auth.views.logout', kwargs={'next_page':None}, name="logout"),

希望有帮助!

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):

url(r'^accounts/logout/(?P<next_page>.*)?

Hope that helps!

,'django.contrib.auth.views.logout', name="logout"), url(r'^accounts/logout/

Hope that helps!

,'django.contrib.auth.views.logout', kwargs={'next_page':None}, name="logout"),

Hope that helps!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文