Django 中的动态登录/注销 URL

发布于 2024-09-29 16:05:08 字数 429 浏览 1 评论 0原文

我有一个应用程序可以提供这样的页面:/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 技术交流群。

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

发布评论

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

评论(2

懷念過去 2024-10-06 16:05:08

如果您可以将 redirect_to 放入登录模板的上下文中,则可以使用一种机制来选择更改重定向 URL。

在您的登录模板中,将额外的隐藏输入添加到表单

<input type="hidden" name="next" value="{% url redirect_to %}">

现在,在您的 urls.py 文件中,您可以指定用于重定向的输入:

url(r'^login/

如果您这样做,那么您将根据值被重定向到不同的位置redirect_to

您可以通过拥有多个命名登录网址来将 redirect_to 引入您的上下文:

url(r'^login/

或者如果您不想做这类事情,您可以在会话中使用某些内容:

<input type="hidden" name="next" value="{% url session.redirect_to %}">

Hope这有帮助,如果代码中有拼写错误,我们深表歉意!身份验证登录视图的文档有点难以链接到 链接。转到该链接并向上滚动一点!

, auth_views.login, {'template_name': 'registration/login.html', 'redirect_field_name': 'next'}, name='auth_login'),

如果您这样做,那么您将根据值被重定向到不同的位置redirect_to

您可以通过拥有多个命名登录网址来将 redirect_to 引入您的上下文:


或者如果您不想做这类事情,您可以在会话中使用某些内容:


Hope这有帮助,如果代码中有拼写错误,我们深表歉意!身份验证登录视图的文档有点难以链接到 链接。转到该链接并向上滚动一点!

, auth_views.login, {'template_name': 'registration/login.html', 'redirect_field_name': 'next', 'extra_context': {'redirect_to': 'foo_url'} }, name='foo_login'), url(r'^login/

或者如果您不想做这类事情,您可以在会话中使用某些内容:


Hope这有帮助,如果代码中有拼写错误,我们深表歉意!身份验证登录视图的文档有点难以链接到 链接。转到该链接并向上滚动一点!

, auth_views.login, {'template_name': 'registration/login.html', 'redirect_field_name': 'next'}, name='auth_login'),

如果您这样做,那么您将根据值被重定向到不同的位置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

<input type="hidden" name="next" value="{% url redirect_to %}">

Now in your urls.py file, you can specify what input to use for the redirect:

url(r'^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:

url(r'^login/

Or if you don't want to do that sort of thing, you could use something in the session instead:

<input type="hidden" name="next" value="{% url session.redirect_to %}">

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!

, auth_views.login, {'template_name': 'registration/login.html', 'redirect_field_name': 'next', 'extra_context': {'redirect_to': 'foo_url'} }, name='foo_login'), url(r'^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!

, 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!

白首有我共你 2024-10-06 16:05:08

/stage/foo/login,对于另一个,可能是/stage/bar/login)。

url( r'^stage/(?P<name>[^/]+)/login/

可能就是您正在寻找的。

然而,你似乎遇到了比这更大的问题。 “我的设置发生了巨大的变化,这太乏味了”表明了更严重和根本的问题。

, some_view_function )

可能就是您正在寻找的。

然而,你似乎遇到了比这更大的问题。 “我的设置发生了巨大的变化,这太乏味了”表明了更严重和根本的问题。

/stage/foo/login and for the other, it might might /stage/bar/login).

url( r'^stage/(?P<name>[^/]+)/login/

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.

, some_view_function )

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.

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