Python “next”参数,重定向,django.contrib.auth.login

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3441436/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 11:07:44  来源:igfitidea点击:

The "next" parameter, redirect, django.contrib.auth.login

pythondjangodjango-forms

提问by django-d

I'm trying to redirect users to custom url "/gallery/(username)/"after successfully logging in. It currently redirects to the default "/account/profile/"url While I know what I can override the redirect url in my settings.py, my url is dynamic thus it will not work.

我试图"/gallery/(username)/"在成功登录后将用户重定向到自定义 url 。它当前重定向到默认"/account/profile/"url 虽然我知道我可以覆盖我的重定向 url settings.py,但我的 url 是动态的,因此它不起作用。

Documentation states that I need to use the "next"parameter and context processors.I have the {{next}}in my template, but I'm confused on how to actually pass the "/gallery/(username)". Any help would be greatly appreciated.

文档指出我需要使用"next"参数并且context processors.我的{{next}}模板中有 ,但是我对如何实际传递"/gallery/(username)". 任何帮助将不胜感激。

p.s: I'm trying to steer away from writing my own login view.

ps:我试图避免编写自己的登录视图。

采纳答案by Jarret Hardie

I confess I usually use 2 redirects in order to get something like this to work.

我承认我通常使用 2 个重定向来使这样的事情起作用。

First, Make your own registration/login.htmlpage. You can copy-and-paste the html example in this section of the authentication docsto make the process a little easier. Instead of using the dynamic '{{ next }}variable from the context, however, hardwire the value of next to go to a generic landing view of logged-in users

首先,制作自己的registration/login.html页面。您可以复制并粘贴身份验证文档这一部分中的 html 示例,以使该过程更容易一些。然而,不是使用'{{ next }}上下文中的动态变量,而是将 next 的值硬连接到登录用户的通用登陆视图

<input type="submit" value="login" />
<input type="hidden" name="next" value="/gallery/" />

Then, in the view that you map to the /gallery/URL, extract the User object from the request (since the user will now be logged in, especially if the gallery view is wrapped in a @permission_requiredor @login_requireddecorator. Use that view to redirect to the appropriate user-specific gallery page:

然后,在映射到/gallery/URL的视图中,从请求中提取 User 对象(因为用户现在将登录,特别是如果画廊视图包含在@permission_required@login_required装饰器中。使用该视图重定向到适当的用户- 特定的画廊页面:

@login_required
def gallery(request):
    url = '/gallery/%s/' % request.user.username
    return HttpResponseRedirect(url)

回答by ozk

create your own view for logging in, with it's own url, don't use the admin's one. you can store the next page in the session, or pass it as a GET parameter to the login view (i.e. /login?next=gallery) just don't forget to sanitize and validate that value before redirecting to it.

创建您自己的登录视图,使用它自己的网址,不要使用管理员的网址。您可以在会话中存储下一页,或将其作为 GET 参数传递给登录视图(即 /login?next=gallery),只是不要忘记在重定向到它之前清理和验证该值。

回答by Andrey Fedoseev

If you already have the custom template for login form you need to add the following inside your <form>tag:

如果您已经有登录表单的自定义模板,则需要在<form>标签中添加以下内容:

<input type="hidden" name="next" value="{{next}}" />

BTW, you don't have to create your own login view. django.contrib.auth.views.login works fine. You only need to create a template for it (registration/login.html)

顺便说一句,您不必创建自己的登录视图。django.contrib.auth.views.login 工作正常。您只需要为其创建一个模板 ( registration/login.html)

回答by naw

You can use a static redirect to /loggedin/and then associate the url to a view that makes the correct redirect.

您可以使用静态重定向/loggedin/,然后将 url 关联到进行正确重定向的视图。

Login takes an extra step but if you want to use django's view it does the job.

登录需要一个额外的步骤,但如果你想使用 django 的视图,它就可以完成这项工作。

回答by Guruprasad

Django's login view django.contrib.auth.views.loginaccepts a dictionary named extra_context. The values in the dictionary are directly passed to the template. So you can use that to set the nextparameter. Once that is done, you can set a hidden field with name nextand value {{ next }}so that it gets rendered in the template.

Django 的登录视图django.contrib.auth.views.login接受一个名为extra_context. 字典中的值直接传递给模板。所以你可以用它来设置next参数。完成后,您可以设置一个带有名称next和值的隐藏字段,{{ next }}以便它在模板中呈现。

回答by antiplex

being an newbie to django and stumbling over this somewhat older thread i found a differing solution for the problem of dynamically (=override a custom default only if needed) setting the next-param that i'd like to share (working fine with django 1.5, earlier versions untested):

作为 django 的新手并且在这个有点旧的线程上磕磕绊绊,我发现了一个不同的解决方案来解决动态问题(=仅在需要时覆盖自定义默认值)设置我想共享的下一个参数(与 django 1.5 一起工作正常) ,早期版本未经测试):

just as django-d i wanted avoid repetition and a custom login-view, so i used the stock django.contrib.auth.views.login-view by adding the line of

就像 django-d 我想避免重复和自定义登录视图,所以我django.contrib.auth.views.login通过添加行来使用股票视图

url(r'^login/$', 'django.contrib.auth.views.login', {'template_name': 'myapp/login.html',}, name='login'),

to my urls.py and within the login.html-templates form-element:

到我的 urls.py 和 login.html-templates 表单元素中:

{% if not next or not next.strip %}
  {# avoid django.contrib.auth.views.login s default of /account/profile/ #}
  {% url 'afterlogindefaultview' as next %}
{% endif %}
<input type="hidden" name="next" value="{{ next }}" />

which to my understanding follows the decoupling-practice of the url-configurations from the views. so for views that should redirect to my apps login and afterwards head to a non-default view i use

据我所知,这遵循了 url 配置与视图的解耦实践。所以对于应该重定向到我的应用程序登录然后转到我使用的非默认视图的视图

return HttpResponseRedirect('%s?next=%s' % (reverse('login'), reverse('mycustomnext')) )

from the view where i want to have the user to log in. i use this to get back to the view where i left off for logging the user in.

从我想让用户登录的视图中。我使用它返回到我停止用户登录的视图。