Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/314.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登录帖子在我执行HttpResponseRedirect(302)时挂起_Python_Django_Authentication_Redirect_Http Status Code 302 - Fatal编程技术网

Python Django登录帖子在我执行HttpResponseRedirect(302)时挂起

Python Django登录帖子在我执行HttpResponseRedirect(302)时挂起,python,django,authentication,redirect,http-status-code-302,Python,Django,Authentication,Redirect,Http Status Code 302,我是Juan Manuel,Django 1.8.18(Python 2.7)中的登录页面有问题。 当我“发布”用户名/密码表单(通过authenticate()和login())并且必须重定向(HttpResponseRedirect)到我的索引页时,浏览器会挂起等待响应(它会停留在登录页中)。 POST之后,它希望通过HTTP 302重定向到“/”,并保持不变 [01/Apr/2020 16:19:43] "POST /login/ HTTP/1.1" 302 0 我注意到了一些事情: 1

我是Juan Manuel,Django 1.8.18(Python 2.7)中的登录页面有问题。
当我“发布”用户名/密码表单(通过authenticate()和login())并且必须重定向(HttpResponseRedirect)到我的索引页时,浏览器会挂起等待响应(它会停留在登录页中)。
POST之后,它希望通过HTTP 302重定向到“/”,并保持不变

[01/Apr/2020 16:19:43] "POST /login/ HTTP/1.1" 302 0
我注意到了一些事情:
1) 并非每次都会发生。
2) 在Chrome的开发者模式下,启用“禁用缓存”模式可以正常工作。
3) 在Firefox上运行良好。
4) 对于reverse(),这是同样的问题(内部调用HttpResponseRedirect())。
5) 该问题存在于开发服务器(Django)和生产服务器(Apache)上。
当它像那样挂起时,如果我按F5(重新加载),工作正常,重定向到索引

url.py:

# -*- coding: utf-8 -*-
from django.conf.urls import patterns, include, url
from django.conf import settings
from django.conf.urls.static import static
from django.contrib import admin
from django.contrib.staticfiles.urls import staticfiles_urlpatterns
from .views import *

admin.autodiscover()


urlpatterns = patterns('',
    url(r'^', include('tadese.urls')),    
    url(r'^login/$', login),
    url(r'^login_cuota/$', login_cuota),
    url(r'^logout/$', logout),
    url(r'^admin/', include(admin.site.urls)),
)+ static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

if settings.DEBUG is False:   #if DEBUG is True it will be served automatically
    urlpatterns += patterns('',
        url(r'^staticfiles/(?P<path>.*)$', 'django.views.static.serve', {'document_root': settings.STATIC_ROOT}),
    )

handler500 = volverHome
handler404 = volverHome
#-*-编码:utf-8-*-
从django.conf.url导入模式,包括,url
从django.conf导入设置
从django.conf.url.static导入静态
从django.contrib导入管理
从django.contrib.staticfiles.url导入staticfiles\u urlpatterns
从。视图导入*
admin.autodiscover()
urlpatterns=模式(“”,
url(r'^',包括('tadese.url'),
url(r“^login/$”,登录),
url(r“^login\u cuota/$”,login\u cuota),
url(r“^logout/$”,注销),
url(r“^admin/”,包括(admin.site.url)),
)+静态(settings.MEDIA\u URL,document\u root=settings.MEDIA\u root)
如果settings.DEBUG为False:#如果DEBUG为True,将自动提供服务
urlpatterns+=模式(“”,
url(r“^staticfiles/(?P.*)$”、“django.views.static.service”、{document\u root':settings.static\u root}),
)
handler500=volverHome
handler404=volverHome
view.py

# -*- coding: utf-8 -*-

from django.contrib.auth import login as django_login, authenticate, logout as django_logout
from django.shortcuts import *
from settings import *
from django.core.urlresolvers import reverse
from django.contrib import messages
from tadese.models import Configuracion, Cuotas, Tributo, UserProfile
from tadese.utilidades import TRIBUTOS_LOGIN
from django.db.models import Q
from django.template.defaulttags import register
from django.conf import settings


def login(request):
    error = None
    LOGIN_REDIRECT_URL = settings.LOGIN_REDIRECT_URL
    if request.method == 'GET':
        if request.user.is_authenticated():
            return volverHome(request)

    try:
        sitio = Configuracion.objects.all().first()
    except Configuracion.DoesNotExist:
        sitio = None

    if sitio <> None:
        unico_padr = (sitio.ver_unico_padron == 'S')
        if sitio.mantenimiento == 1:
            return render_to_response('mantenimiento.html', {'dirMuni': MUNI_DIR, 'sitio': sitio},
                                      context_instance=RequestContext(request))
    else:
        unico_padr = False

    if request.method == 'POST':

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

                if user.userprofile.tipoUsr == 0:
                    request.session["usuario"] = request.POST['username']
                    if unico_padr:
                        try:
                            padr = Cuotas.objects.filter(padron=request.POST['username'], estado=0).order_by(
                                '-id_cuota').first()
                            if padr:
                                LOGIN_REDIRECT_URL = reverse('ver_cuotas', kwargs={'idp': padr.id_padron})
                                return HttpResponseRedirect(LOGIN_REDIRECT_URL)
                        except:
                            padr = None
                    else:
                        LOGIN_REDIRECT_URL = reverse('padrones_responsable')
                return volverHome(request)
            else:
                ## invalid login
                error = u'Verifique que:\n. Los datos sean correctos.\n. Posea cuotas generadas en el sistema.'
        else:
            ## invalid login
            error = u'Verifique que:\n. Los datos sean correctos.\n. Posea cuotas generadas en el sistema.'
        # return direct_to_template(request, 'invalid_login.html')

    if error:
        messages.add_message(request, messages.ERROR, u'%s' % (error))
    tributos = Tributo.objects.filter()
    return render_to_response('index.html', {'dirMuni': MUNI_DIR, 'sitio': sitio, 'tributos': tributos},
                              context_instance=RequestContext(request))


def logout(request):
    request.session.clear()
    django_logout(request)
    return HttpResponseRedirect(LOGIN_URL)


def volverHome(request):
    if not request.user.is_authenticated():
        return HttpResponseRedirect(LOGIN_URL)

    if request.user.userprofile.tipoUsr == 0:
        LOGIN_REDIRECT_URL = reverse('padrones_responsable')
    elif request.user.userprofile.tipoUsr == 1:
        LOGIN_REDIRECT_URL = reverse('padrones_estudio')
    else:
        LOGIN_REDIRECT_URL = reverse('padrones_responsable')

    return HttpResponseRedirect(LOGIN_REDIRECT_URL)

#-*-编码:utf-8-*-
从django.contrib.auth导入作为django_登录的登录,验证,作为django_注销的注销
从django.com导入*
从设置导入*
从django.core.urlResolver反向导入
从django.contrib导入消息
从tadese.models导入配置、Cuotas、Tributo、UserProfile
从tadese.utilidades导入TRIBUTOS\u登录
从django.db.models导入Q
从django.template.defaulttags导入寄存器
从django.conf导入设置
def登录(请求):
错误=无
LOGIN\u REDIRECT\u URL=settings.LOGIN\u REDIRECT\u URL
如果request.method==“GET”:
if request.user.is_经过身份验证():
返回volverHome(请求)
尝试:
sitio=Configuracion.objects.all().first()
除Configuracion.DoesNotExist外:
sitio=无
如果没有:
unico\u padr=(sitio.ver\u unico\u padron=='S')
如果sitio.mantenimiento==1:
返回render_to_response('mantenimiento.html',{'dirMuni':MUNI_DIR',sitio':sitio},
上下文\实例=请求上下文(请求))
其他:
unico_padr=错误
如果request.method==“POST”:
user=authenticate(username=request.POST['username'],password=request.POST['password'],
tributo=request.POST['tributo'])
如果用户不是无:
如果user.u处于活动状态:
django_登录(请求,用户)
如果user.userprofile.tipoUsr==0:
request.session[“usuario”]=request.POST['username']
如果unico_padr:
尝试:
padr=Cuotas.objects.filter(padron=request.POST['username'],estado=0)。订购人(
“-id_cuota”).first()
如果padr:
LOGIN\u REDIRECT\u URL=reverse('ver\u cuotas',kwargs={'idp':padr.id\u padron})
返回HttpResponseRedirect(登录\重定向\ URL)
除:
padr=无
其他:
LOGIN\u REDIRECT\u URL=reverse('padrones\u responsible')
返回volverHome(请求)
其他:
##无效登录
错误=u'Verique:\n。Los datos sean Correctoros。\n。系统中的一代人的地位。”
其他:
##无效登录
错误=u'Verique:\n。Los datos sean Correctoros。\n。系统中的一代人的地位。”
#将直接返回到模板(请求“invalid\u login.html”)
如果出现错误:
messages.add_消息(请求,messages.ERROR,u'%s'(错误))
tributos=Tributo.objects.filter()
返回render_to_response('index.html',{'dirMuni':MUNI_DIR,'sitio':sitio,'tributos':tributos},
上下文\实例=请求上下文(请求))
def注销(请求):
request.session.clear()
django_注销(请求)
返回HttpResponseRedirect(登录地址)
def volverHome(请求):
如果不是,则请求。用户。是否经过身份验证()
返回HttpResponseRedirect(登录地址)
如果request.user.userprofile.tipoUsr==0:
LOGIN\u REDIRECT\u URL=reverse('padrones\u responsible')
elif request.user.userprofile.tipoUsr==1:
LOGIN\u REDIRECT\u URL=reverse('padrones\u estudio')
其他:
LOGIN\u REDIRECT\u URL=reverse('padrones\u responsible')
返回HttpResponseRedirect(登录\重定向\ URL)

来自

超文本传输协议(HTTP)302找到的重定向状态响应代码指示请求的资源已临时移动到位置报头给定的URL。浏览器会重定向到此页面,但搜索引擎不会更新其到资源的链接(在“SEO演讲”中,据说“链接果汁”不会发送到新的URL)

即使规范要求在执行重定向时不更改方法(和主体),但并非所有用户代理都符合此处的要求-您仍然可以在那里找到此类有缺陷的软件。因此,建议仅将302代码设置为GET或HEAD方法和t的响应