gpt4 book ai didi

python - Django:HttpResponseRedirect 不工作

转载 作者:太空狗 更新时间:2023-10-29 22:30:47 24 4
gpt4 key购买 nike

我是 Python/Django 和整体编程的新手。我需要有关 HttpResponseRedirect 的帮助,因为它在我的登录 View 中不起作用。它确实适用于我的主视图文件,但不是我想要的方式。

我没有重定向到所需的页面('/'),而是只在同一页面上看到了这个:

Content-Type: text/html; charset=utf-8 Location: /

用户实际登录但停留在同一页面上。因此,如果我手动转到所需的页面,我会看到我已登录。

这里是相关的代码片段。我到处都在使用自己的观点。我希望通过这种方式进行练习和更好地理解。

网址.py

from django.conf.urls import patterns, include, url
from django.conf.urls.static import static
from django.contrib.staticfiles.urls import staticfiles_urlpatterns
from django.contrib import admin
from backend import settings
admin.autodiscover()

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

urlpatterns += staticfiles_urlpatterns()

View .py

from dashboard.dashviews import left, right, topright
from authentication.authviews import LoginView, LogoutView
from django.shortcuts import render
from django.http import HttpResponseRedirect
from django.contrib.auth.decorators import login_required

@login_required(login_url='/login/')
def dash(request):
return render(request, 'dash_template.html',
{'left': left.dash_left(request),
'right': right.dash_right(),
'topright': topright.top_right(request)})

def login(request):
if not request.user.is_authenticated():
return render(request, 'login.html', {'login': LoginView.login_view(request)})
else:
return HttpResponseRedirect('/')

def logout(request):
return render(request, 'logout.html', {'logout': LogoutView.logout_view(request)}) and HttpResponseRedirect('/login/')

登录 View .py

from django.contrib import auth
from django.http import HttpResponseRedirect

def login_view(request):
username = request.POST.get('username', '')
password = request.POST.get('password', '')
user = auth.authenticate(username=username, password=password)
if user is not None and user.is_active:
# Correct password, and the user is marked "active"
auth.login(request, user)
# Redirect to dashboard
return HttpResponseRedirect('/')
else:
# Show a message
return 'Please enter your username and password below.'

login.html - 简单表单

<center>

<p>{{ login }}</p>

{% if form.errors %}
<p class="error">Sorry, that's not a valid username or password</p>
{% endif %}


<form action="./" method="post">
<table border="0">
<tr>
<td>
<label for="username">Username:</label>
</td>
<td>
<input type="text" name="username" value="" id="username">
</td>
</tr>
<tr>
<td>
<label for="password">Password:</label>
</td>
<td>
<input type="password" name="password" value="" id="password">
</td>
</tr>
</table>
<input type="submit" value="login" />
<input type="hidden" name="next" value="/" />
{% csrf_token %}
</form>

</center>

我遵循了 Django 书籍教程,根据它,一切都应该工作得很好。如您所见,我也尝试过在我的表单中使用“下一个”隐藏字段,但这也不起作用。任何帮助,将不胜感激。我想知道我在这里错过了什么。谢谢!

最佳答案

您在 html 中看到“Content-Type: text/html; charset=utf-8 Location:/”的原因是因为您返回了一个 HttpResponse 对象作为要在响应中呈现的上下文数据的一部分,而不是这是实际的回应。你注销看起来也有点奇怪。但是要使用你目前拥有的东西:

改变你的观点.py

if not request.user.is_authenticated():
# LoginView.login_view will return a HttpResponse object
return LoginView.login_view(request)
else:
...

然后更改您的 LoginView.login 中的 View 以始终返回您想要的 Response 对象(重定向或您想要呈现的页面)

def login_view(request):
# if the request method is POST the process the POST values otherwise just render the page
if request.method == 'POST':
username = request.POST.get('username', '')
password = request.POST.get('password', '')
user = auth.authenticate(username=username, password=password)
if user is not None and user.is_active:
# Correct password, and the user is marked "active"
auth.login(request, user)
# Redirect to dashboard
return HttpResponseRedirect('/')
else:
# Show a message
err_msg = 'Please enter your username and password below.'
return render(request, 'login.html', {'login': err_msg})

关于python - Django:HttpResponseRedirect 不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19647351/

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