如何在登录后重定向Django.contrib.auth.views.login?

2024-05-17 17:07:13 发布

您现在位置:Python中文网/ 问答频道 /正文

我在网页中的每个地方都添加了django.contrib.auth.views.login,因此必须在base.html中加载templatetag(返回AuthenticationForm)。此templatetags包括registration/login.html模板。

登录工作正常,但我希望它将用户重定向到登录前的相同页面。现在,它将我重定向到/where_i_am/login wich显示的registration/login.html,其中显示“login ok”或“login fails”消息,但没有base.html的其余部分。

我已经遵循了django文档和一些类似this的问题,但是我无法正确重定向。我已经修改了next变量,但它似乎不起作用(next={{ request.get_full_path }}再次将我重定向到/在哪里/登录…)

你试过类似的东西吗?有什么想法吗?

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

谢谢你。


Tags: djangoauth网页basehtml地方loginregistration
3条回答

下面允许用户重定向到登录后试图访问的页面,但不必编写自定义视图。它包含了你需要添加的所有代码。(顺便说一下,并不是所有的模板上下文处理器都是必需的,但是如果您显式地为其设置了一个值,则会覆盖默认值,因此需要重新添加它们。)

设置.py

TEMPLATE_CONTEXT_PROCESSORS = (
    "django.contrib.auth.context_processors.auth",
    "django.core.context_processors.debug",
    "django.core.context_processors.i18n",
    "django.core.context_processors.media",
    "django.core.context_processors.request",
    "django.core.context_processors.static",
)

url.py

from django.contrib.auth.views import login, logout
...the other imports for your app ...

urlpatterns = patterns('',
    (r'^login/$', login, {'template_name':'login.html'} ),
    (r'^logout/$', logout,{'template_name':'logout.html'}),
    ...the other urls for your app...
)

登录.html

<html>
    <form method="post" action="{% url 'django.contrib.auth.views.login' %}">
        {% csrf_token %}
        {{form}}<br/>
       <input type="submit" value="login" />
       <input type="hidden" name="next" value="{{ next }}" />
   </form>
</html>

注销.html

<html>
<p>You are logged out. To log in again, click <a href="/login/">here</a>.</p> 
</html>

视图.py

@login_required(login_url="/login/")
def view1(request):
    ....this is a view you want to protect with security...

@login_required(login_url="/login/")
def view1(request):
    ....this is a view you want to protect with security...

我在默认登录视图中使用了类似的内容:

{% if form.errors %}
    <p class="error">Sorry, that's not a valid username or password</p>
{% endif %}

<form action="{% url login %}" method="post">
    {% csrf_token%}
    <label for="username">User name:</label>
    <input type="text" name="username" value="" id="username">
    <label for="password">Password:</label>
    <input type="password" name="password" value="" id="password">

    <input type="submit" value="login" />
    <input type="hidden" name="next" value="{{ request.get_full_path }}" />
</form>
    # or if it's not declareв шт urls:
     <form action="{% url django.contrib.auth.views.login %}?next={{ request.get_full_path }}" method="post">

一切正常。

备注:您确定“context_processors.request”包含在设置中吗?忘记包含它是一个常见的问题。

UPD:据我所知,在登录失败时,无法将默认登录视图设置为重定向(这根本不起作用)。
但我可能错了

找到答案:

在settings.py中更改settings.LOGIN_REDIRECT_URL

以下代码来自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
   ...

相关问题 更多 >