简体   繁体   English

需要Django登录名,无法正确重定向

[英]Django Login Required Not Redirecting Properly

So I am trying to add a @login_required decorator on my pages that aren't the login page, but when I try to login and redirect to a different page, it does not redirect. 因此,我尝试在不是登录页面的页面上添加@login_required装饰器,但是当我尝试登录并重定向到其他页面时,它不会重定向。 I got to my url, then login, and then it adds the /?next=/redirect_page/ . 我进入我的网址,然后登录,然后添加/?next=/redirect_page/ So it goes from www.example.com to www.example.com/?next=/redirect_page/ even though it should see that it is logged in and redirect, not add the next part. 因此,它应该从www.example.com转到www.example.com/?next=/redirect_page/ ,尽管它应该看到它已登录并重定向,而不添加下一部分。 Below is my code. 下面是我的代码。

home.html: home.html:

<form method="post" class="form-signin" action="{% url 'index' %}">
    {% csrf_token %}
    <input type="hidden" name="next" value="{{ next }}" />
    <h2 class="form-signin-heading">Please Sign In</h2>
    {% bootstrap_form form %}
    {% buttons %}
    <button type="submit" class="btn btn-primary">Submit</button>
    {% endbuttons %}
</form>

home/views.py: home / views.py:

from django.shortcuts import render
from django.http import HttpResponseRedirect
from django.contrib.auth import authenticate

from .forms import SignInForm


def index(request):
    # if this is a POST request we need to process the form data
    if request.method == 'POST':
        # create a form instance and populate it with data from the request:
        form = SignInForm(request.POST)
        # check whether it's valid:
        if form.is_valid():
            # process the data in form.cleaned_data as required
            user = authenticate(username=request.POST.get('username', None), password=request.POST.get('password', None))
            if user is not None:
                return HttpResponseRedirect('/redirect_page/')
            else:
                form = SignInForm()

    # if a GET (or any other method) we'll create a blank form
    else:
        form = SignInForm()

    return render(request, "home/home.html", {'form': form})

redirect_page/views.py: redirect_page / views.py:

from django.shortcuts import get_object_or_404, render
from django.contrib.auth.decorators import login_required

from .models import Table


@login_required
def index(request):
    stuff = Table.objects.all()
    context = {"stuff": stuff,}
    return render(request, "redirect_page/index.html", context)


@login_required
def stuff(request, stuff_id):
    stuff = get_object_or_404(Table, pk=word_id)
    return render(request, "redirect_page/detail.html", {"stuff": stuff})

What am I doing wrong? 我究竟做错了什么?

You have authenticated the user, but you have not logged them in. You need to add login(request, user) to the view. 您已经对用户进行了身份验证,但尚未登录。您需要在视图中添加login(request, user)

from django.contrib.auth import authenticate, login

def index(request):
    ...
    if form.is_valid():
        # process the data in form.cleaned_data as required
        user = authenticate(username=request.POST.get('username', None), password=request.POST.get('password', None))
        if user is not None:
            login(request, user)
            return HttpResponseRedirect('/redirect_page/')
        ...

See the docs on how to log a user in for more info. 有关更多信息,请参阅有关如何登录用户的文档。

Note that you shouldn't put form = SignInForm() in the else block when the form isn't valid. 请注意,当表单无效时,不应将form = SignInForm()放在else块中。 This replaces the bound form (which might have useful error messages) with an empty form. 这会将绑定形式(可能包含有用的错误消息)替换为空形式。

In case you have not specified LOGIN_URL variable in your settings do so. 如果您未在设置中指定LOGIN_URL变量,请这样做。 It should work post that. 它应该工作。

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

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