Django 中的动态登录/注销 URL
我有一个应用程序可以提供这样的页面:/stage/stagename。 舞台名称是可变的。 URL 映射器只是寻找 [0-9A-Za-z]+ 来填充该槽。
现在,由于我的设置,我的应用程序将植根于 /stage/stagename。我无法改变这一点(如果不对我的设置进行大规模更改,这现在太乏味了,也是最后一个选择)。
我需要使用 django.contrib.auth。登录和注销 URL 不能位于我的设置文件中,因为它们会根据我的应用程序的根位置而变化(对于一个,它可能是 /stage/foo/login,对于另一个,它可能是 /stage/bar/login )。
如何让后端使用这样的动态URL?
我还遇到一个问题,需要将 stagename 参数传递给生成 URL 的模板。我怎样才能做到这一点?
I have an app that serves pages like this /stage/stagename. stagename is variable. The URL mappers just look for a [0-9A-Za-z]+ to fill that slot.
Now, because of my setup, My app will be rooted at /stage/stagename. I cannot change this (without massive changes to my setup which is too tedious right now and a last option).
I need to use django.contrib.auth
. The login and logout URLs can't be in my settings file since they will change depend on where my app is rooted (For one it might be /stage/foo/login and for the other, it might might /stage/bar/login).
How can I make the backend use such dynamic URLs?
I also have the issue that I need to pass the stagename parameter to the template which generates my URL. How can I do that?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您可以将
redirect_to
放入登录模板的上下文中,则可以使用一种机制来选择更改重定向 URL。在您的登录模板中,将额外的隐藏输入添加到表单
现在,在您的 urls.py 文件中,您可以指定用于重定向的输入:
如果您这样做,那么您将根据值被重定向到不同的位置
redirect_to
。您可以通过拥有多个命名登录网址来将
redirect_to
引入您的上下文:或者如果您不想做这类事情,您可以在会话中使用某些内容:
Hope这有帮助,如果代码中有拼写错误,我们深表歉意!身份验证登录视图的文档有点难以链接到 链接。转到该链接并向上滚动一点!
, auth_views.login, {'template_name': 'registration/login.html', 'redirect_field_name': 'next', 'extra_context': {'redirect_to': 'bar_url'} }, name='bar_login'),或者如果您不想做这类事情,您可以在会话中使用某些内容:
Hope这有帮助,如果代码中有拼写错误,我们深表歉意!身份验证登录视图的文档有点难以链接到 链接。转到该链接并向上滚动一点!
, auth_views.login, {'template_name': 'registration/login.html', 'redirect_field_name': 'next'}, name='auth_login'),如果您这样做,那么您将根据值被重定向到不同的位置
redirect_to
。您可以通过拥有多个命名登录网址来将
redirect_to
引入您的上下文:或者如果您不想做这类事情,您可以在会话中使用某些内容:
Hope这有帮助,如果代码中有拼写错误,我们深表歉意!身份验证登录视图的文档有点难以链接到 链接。转到该链接并向上滚动一点!
If you can get
redirect_to
into the context for your login template, then there is a mechanism you can use to choose vary the redirect URL.In your login template, add an extra hidden input to the form
Now in your urls.py file, you can specify what input to use for the redirect:
If you do this, then you'll be redirected to different places depending on the value of
redirect_to
.You might be able to get
redirect_to
into your context by having more than one named login url:Or if you don't want to do that sort of thing, you could use something in the session instead:
Hope this helps, and apologies if there are typos in the code! The documentation for the auth login view is a bit hard to link to link. Go to that link and scroll up a bit!
, auth_views.login, {'template_name': 'registration/login.html', 'redirect_field_name': 'next', 'extra_context': {'redirect_to': 'bar_url'} }, name='bar_login'),Or if you don't want to do that sort of thing, you could use something in the session instead:
Hope this helps, and apologies if there are typos in the code! The documentation for the auth login view is a bit hard to link to link. Go to that link and scroll up a bit!
, auth_views.login, {'template_name': 'registration/login.html', 'redirect_field_name': 'next'}, name='auth_login'),If you do this, then you'll be redirected to different places depending on the value of
redirect_to
.You might be able to get
redirect_to
into your context by having more than one named login url:Or if you don't want to do that sort of thing, you could use something in the session instead:
Hope this helps, and apologies if there are typos in the code! The documentation for the auth login view is a bit hard to link to link. Go to that link and scroll up a bit!
可能就是您正在寻找的。
然而,你似乎遇到了比这更大的问题。 “我的设置发生了巨大的变化,这太乏味了”表明了更严重和根本的问题。
Might be what you're looking for.
However, you seem to have bigger problems than this. "massive changes to my setup which is too tedious" indicates more serious and fundamental problems.