【发布时间】:2015-06-14 18:34:32
【问题描述】:
我在 Django 中遇到了 HttpResponseRedirect 的问题。看来,无论我尝试什么参数,它要么引发错误,要么在不更改 URL 的情况下重定向。我在自定义 login_user 视图上使用它,并且我希望地址栏中的 URL 在重定向后更改。如果我使用重定向而不是 HttpResponseRedirect,它不会改变。无论哪种方式,我都可以让它提供正确的模板,但 URL 保持不变。作为 Django 的新手,如果有人可以向我解释如何执行此操作以及为什么我当前的代码不起作用,那将会很有帮助。 我在 Stack Exchange 上看到过几个类似的问题,但答案没有帮助。
这是我的views.py的相关部分(请注意,由于此处的复制和粘贴,缩进变得很奇怪,这不是错误的原因)。
from django.http import *
from django.contrib.auth import authenticate, login, logout
def login_user(request):
logout(request)
username = password = ''
if request.POST:
username = request.POST.get('username')
password = request.POST.get('password')
user = authenticate(username=username, password=password)
if user is not None:
if user.is_active:
login(request, user)
return HttpResponseRedirect('dashboard')
else:
state = "Your account is not active, please contact the app administrator."
else:
state = "Your username and/or password were incorrect."
state = "Please log in below..."
context = RequestContext(request, {
'state': state,
'username': username,
})
return render_to_response('bank/auth.html', {}, context)
dashboard 是另一个视图的名称,它可以在我的索引视图的重定向中正常工作。我也尝试过硬编码网址,但这也不起作用。有什么建议??谢谢。
【问题讨论】:
-
HttpResponseRedirect需要一个有效的url类似的东西(例如:如果你想重定向到localhost:8000/你必须打电话给HttpResponseRedirect('/'),
标签: django