HttpResponseRedirect将传递的URL附加到当前URL

问题描述 投票:0回答:2

我正在使用djnago HttpResponseRedirect登录后返回上一页。问题是,我输入了错误的重定向网址(next网址和当前网址的组合)

假设我已经从“ mysite.com/home”登录,并且登录页面的URL是“ mysite.com/login”,我想返回到“ mysite.com/home” 但是重定向URL是mysite。 com / login /?next = / home /

我做错了什么?

这里是我的view,负责登录:

if request.method == 'POST':
    username = request.POST['username']
    password = request.POST['password']
    user = authenticate(username=username, password=password)
    if user is not None:
        if user.is_active:
            login(request, user)

            # next is "/home/" and that is exactly what I expect
            return HttpResponseRedirect(request.GET.get('next', reverse('products.views.show_homePage')))
django httpresponse
2个回答
0
投票

我只是遇到了同样的问题!

尝试一下:

return HttpResponseRedirect(reverse('products.views.show_homePage'))

请确保明显包括:from django.core.urlresolvers import reverse

这将使页面将重定向发送到您的主页。。希望..

这是我所做的:

登录:

# Login
def login(request):
    if request.user.is_authenticated():
        return HttpResponseRedirect("/test/")  # Redirect to a tool directory page.
    form = loginForm(request.POST or None)
    if request.POST and form.is_valid():
        user = form.login(request)
        if user:
            auth_login(request, user)
            return HttpResponseRedirect("/test/")  # Redirect to a success page.
    return render(request, 'login.html', {'form': form})

注销:

# Logout user method 
def logout(request):
    auth_logout(request)
    return HttpResponseRedirect(reverse('loginregistration.views.login'))  # Redirect to a success page.

0
投票

以下为我工作的作品:Class HttpResponseRedirect可以使用三种URL:

((1)完全限定的URL,例如'https://www.yahoo.com/search/'

(2)没有域的绝对路径,例如'/ search /'

((3)相对路径,例如'search /'

如果您的函数未返回完全限定URL,则可能是将URL附加到现有URL的可能原因。

请参阅Django官方文档以获取更多信息:https://docs.djangoproject.com/en/3.0/ref/request-response/

© www.soinside.com 2019 - 2024. All rights reserved.