简体   繁体   English

Django中的动态登录/注销URL

[英]Dynamic login/logout URLs in Django

I have an app that serves pages like this /stage/ stagename . 我有一个提供此类/ stage / stagename页面的应用程序。 stagename is variable. stagename是变量。 The URL mappers just look for a [0-9A-Za-z]+ to fill that slot. URL映射器仅查找[0-9A-Za-z] +即可填充该广告位。

Now, because of my setup, My app will be rooted at /stage/ stagename . 现在,由于我的设置,我的应用程序将基于/ 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 . 我需要使用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). 登录URL和注销URL不能在我的设置文件中,因为它们将根据我的应用程序的植根位置而改变(对于一个URL,它可能是/ stage / foo / login;对于另一个URL,它可能是/ stage / bar / login。 )。

How can I make the backend use such dynamic URLs? 如何使后端使用此类动态URL?

I also have the issue that I need to pass the stagename parameter to the template which generates my URL. 我还有一个问题,我需要将stagename参数传递给生成我的URL的模板。 How can I do that? 我怎样才能做到这一点?

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. 如果您可以将redirect_to放入登录模板的上下文中,那么可以使用一种机制来选择更改重定向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: 现在,在您的urls.py文件中,您可以指定用于重定向的输入:

url(r'^login/$',
    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 . 如果执行此操作,则将根据redirect_to的值redirect_to您重定向到其他位置。

You might be able to get redirect_to into your context by having more than one named login url: 您可以通过具有多个命名的登录URL来将redirect_to放入您的上下文中:

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

url(r'^login/$',
    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: 或者,如果您不想执行此类操作,则可以在会话中使用某些方法:

<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 . auth登录视图的文档很难链接到link Go to that link and scroll up a bit! 转到该链接并向上滚动一点!

/stage/foo/login and for the other, it might might /stage/bar/login). / stage / foo / login,对于另一个,可能是/ stage / bar / login)。

url( r'^stage/(?P<name>[^/]+)/login/$', 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. “对我的设置进行的大量更改过于繁琐”表示存在更严重和根本的问题。

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

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