Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/heroku/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 需要Django登录,但未正确重定向_Python_Django_Redirect_Login Required - Fatal编程技术网

Python 需要Django登录,但未正确重定向

Python 需要Django登录,但未正确重定向,python,django,redirect,login-required,Python,Django,Redirect,Login Required,因此,我试图在我的非登录页面的页面上添加一个@login\u required装饰程序,但当我尝试登录并重定向到其他页面时,它不会重定向。我访问了我的url,然后登录,然后它添加了/?next=/redirect\u page/。因此,它从www.example.com转到www.example.com/?next=/redirect\u page/,即使它应该看到它已登录并重定向,而不是添加下一部分。下面是我的代码 home.html: <form method="post" class

因此,我试图在我的非登录页面的页面上添加一个
@login\u required
装饰程序,但当我尝试登录并重定向到其他页面时,它不会重定向。我访问了我的url,然后登录,然后它添加了
/?next=/redirect\u page/
。因此,它从
www.example.com
转到
www.example.com/?next=/redirect\u page/
,即使它应该看到它已登录并重定向,而不是添加下一部分。下面是我的代码

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>
重定向页面/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})
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})

我做错了什么?

如果您没有在设置中指定
LOGIN\u URL
变量,请这样做。如果您没有在设置中指定
LOGIN\u URL
变量,那么它应该可以工作。

。它应该可以正常工作。

您已经对用户进行了身份验证,但尚未登录。您需要将
登录(请求、用户)
添加到视图中

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/')
        ...
有关更多信息,请参阅上的文档


请注意,当表单无效时,不应将
form=signinfo()
放入
else
块中。这会将绑定表单(可能包含有用的错误消息)替换为空表单。

您已经对用户进行了身份验证,但尚未登录用户。您需要将
登录(请求、用户)
添加到视图中

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/')
        ...
有关更多信息,请参阅上的文档

请注意,当表单无效时,不应将
form=signinfo()
放入
else
块中。这将用空表单替换绑定表单(可能有有用的错误消息)