Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/362.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
Javascript 浏览器在Django重定向成功时未呈现响应_Javascript_Python_Django_Redirect - Fatal编程技术网

Javascript 浏览器在Django重定向成功时未呈现响应

Javascript 浏览器在Django重定向成功时未呈现响应,javascript,python,django,redirect,Javascript,Python,Django,Redirect,我的目标是检查登录的用户类型。如果用户类型为“special”,那么我想将该用户重定向到一个名为dashboard的单独页面(其视图位于单独的应用程序中)。如果用户不是特殊用户,将呈现常规成功页面 普通用户的情况很好,但特殊用户的情况却失败了。通过使用调试器和Chrome的web检查器,我注意到Django成功地执行了重定向,并将正确的HTML响应返回到浏览器,但浏览器在显示该页面时不采取任何操作。Chrome web inspector上的“网络”选项卡显示了向服务器发出的两个不同请求——原始

我的目标是检查登录的用户类型。如果用户类型为“special”,那么我想将该用户重定向到一个名为dashboard的单独页面(其视图位于单独的应用程序中)。如果用户不是特殊用户,将呈现常规成功页面

普通用户的情况很好,但特殊用户的情况却失败了。通过使用调试器和Chrome的web检查器,我注意到Django成功地执行了重定向,并将正确的HTML响应返回到浏览器,但浏览器在显示该页面时不采取任何操作。Chrome web inspector上的“网络”选项卡显示了向服务器发出的两个不同请求——原始登录请求和重定向到仪表板的请求

我应该如何确保浏览器显示仪表板

以下是views.py中的登录函数:

def login(request):
    if request.method == 'GET':
        return render_home(request, {'login_required': True})
    errors = []
    email = request.POST['email']
    password = request.POST['password']
    user = authenticate(username=email, password=password)
    if user is not None:
        if user.is_active:
            auth_login(request, user)
        else:
            errors = ["User isn't active"]
    else:
        errors = ["User couldn't be authenticated"]

    if (user.is_special):
        url = reverse('special.views.dashboard')
        return HttpResponseRedirect(url)
    template = loader.get_template('users/login.js')
    context = Context({"errors": errors})
    return HttpResponse(template.render(context), mimetype="text/javascript")
@login_required
def dashboard(request):
    user = request.user
    special_data = get_special_data(user)
    data = {'user': user, 'special_data': special_data}
    return render(request, 'special_dashboard.html', data)
以下是特殊应用程序views.py中的仪表板功能:

def login(request):
    if request.method == 'GET':
        return render_home(request, {'login_required': True})
    errors = []
    email = request.POST['email']
    password = request.POST['password']
    user = authenticate(username=email, password=password)
    if user is not None:
        if user.is_active:
            auth_login(request, user)
        else:
            errors = ["User isn't active"]
    else:
        errors = ["User couldn't be authenticated"]

    if (user.is_special):
        url = reverse('special.views.dashboard')
        return HttpResponseRedirect(url)
    template = loader.get_template('users/login.js')
    context = Context({"errors": errors})
    return HttpResponse(template.render(context), mimetype="text/javascript")
@login_required
def dashboard(request):
    user = request.user
    special_data = get_special_data(user)
    data = {'user': user, 'special_data': special_data}
    return render(request, 'special_dashboard.html', data)

在正常情况下,您将从视图返回javascript,这表明您正在使用ajax处理登录表单。如果是这种情况,您还需要在客户端处理重定向。

谢谢,您是对的!我决定通过在现有javascript响应中设置window.location.href变量来重定向,而不是通过Django重定向。