gpt4 book ai didi

python - 当我执行 HttpResponseRedirect (302) 时,Django 登录 POST 挂起

转载 作者:行者123 更新时间:2023-12-04 04:26:04 24 4
gpt4 key购买 nike

我是 Juan Manuel,我在 Django 1.8.18 (Python 2.7) 中的登录页面有问题。
当我对用户名/密码表单进行“POST”(通过 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(重新加载),工作正常并且重定向到索引。

网址.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

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)

最佳答案

来自 https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/302
超文本传输​​协议(protocol) (HTTP) 302 Found 重定向状态响应代码表示请求的资源已临时移动到 Location header 给出的 URL。浏览器重定向到此页面,但搜索引擎不会更新其指向资源的链接(在“SEO-speak”中,据说“链接汁”不会发送到新 URL)。
即使规范要求在执行重定向时不更改方法(和主体),但并非所有用户代理都符合此处 - 您仍然可以在那里找到这种类型的错误软件。因此,建议仅将 302 代码设置为 GET 或 HEAD 方法的响应,并改用 307 临时重定向,因为在这种情况下明确禁止更改方法。
如果您希望将使用的方法更改为 GET,请改用 303 See Other。当您想要对不是上传资源而是确认消息的 PUT 方法做出响应时,这很有用,例如:“您已成功上传 XYZ”。
您还可以在使用受支持的 python 3 版本和 django 2.2 LTS 后分享发现吗

关于python - 当我执行 HttpResponseRedirect (302) 时,Django 登录 POST 挂起,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60979106/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com