登录后如何重定向django.contrib.auth.views.login? [英] How to redirect django.contrib.auth.views.login after login?

查看:139
本文介绍了登录后如何重定向django.contrib.auth.views.login?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在网页的所有位置添加了django.contrib.auth.views.login ,因为我必须加载

I added django.contrib.auth.views.login everywhere in my webpage, for that I had to load a templatetag (that returns the AuthenticationForm) in my base.html. This templatetags includes the registration/login.html template.

登录正常,但是我希望它将用户重定向到登录前的同一页面.现在,它将我重定向到/wherever_i_am/login,其中显示registration/login.html带有"login ok"(登录成功)或"login failed"(登录失败)消息,但没有base.html的其余部分.

The login is working ok but I want it to redirect the users to the same page they are before login. Now, it redirects me to /wherever_i_am/login wich shows registration/login.html with the 'login ok' or 'login fails' messages but without the rest of base.html.

我遵循了django文档以及一些诸如 this 之类的问题,但我无法正确重定向.我已经修改了next变量,但它似乎不起作用(next={{ request.get_full_path }}再次将我重定向到/wherever_i_am/login ...)

I have followed django documentation and a few SO questions like this but I cannot redirect correctly. I have modified the next variable but it doesn't seem to work (next={{ request.get_full_path }} redirects me to /wherever_i_am/login ...again)

您尝试过类似的方法吗?有什么想法吗?

Have you tried something similar? any ideas?

UPDATE1 现在,问题可能是这样的:如果我想在网页的任何地方都包含登录表单,是否必须声明自己的登录视图?

UPDATE1 Now, the question could be something like: Do I have to declare my own login view if I want to include the login form everywhere in my web page?

谢谢.

推荐答案

找到答案:

更改settings.py,更改LOGIN_REDIRECT_URL,

Change settings.LOGIN_REDIRECT_URL in your settings.py,

下面的代码是从Django复制的:

below code is copy from django:

   if request.method == "POST":
    form = authentication_form(data=request.POST)
    if form.is_valid():
        # Ensure the user-originating redirection url is safe.
        if not is_safe_url(url=redirect_to, host=request.get_host()):
            redirect_to = settings.LOGIN_REDIRECT_URL
   ...

这篇关于登录后如何重定向django.contrib.auth.views.login?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆