Django HttpResponseRedirect vs render_to_response - 如何获取登录表单以满足我需要的方式 [英] Django HttpResponseRedirect vs render_to_response - how to get a login form to behave the way I need it to
问题描述
我已经检查了以下关于HttpResponse,HttpResponseRedirect和render_to_response 之间的差异的stackoverflow问题,以及已经通过官方django文档,但我真的不知道如何最好地获得我正在寻找创建的功能。
I've already checked out the following stackoverflow question regarding the difference between HttpResponse, HttpResponseRedirect, and render_to_response, as well as having gone through the official django docs, but I'm really uncertain how best to get the functionality I'm looking to create.
现在我有一个具有登录功能的 index.html (见下面的 views.py )其中 render_to_response
将我带到 portal / index.html 。但是,由于 urls.py (见下文)规定,浏览器的URL栏中的url是 http://127.0.0.1:8000/login/
。这意味着刷新页面会强制表单再次出现。
Right now I have an index.html with a login function (as seen in the views.py below) where the render_to_response
that brings me to portal/index.html. However, as urls.py (see below) dictates, the url in the url bar of my browser is http://127.0.0.1:8000/login/
. This means that refreshing the page forces the form to go again.
如何获取该URL(一旦登录)看起来像 http://127.0.0.1:8000/
或者,如果这不可行, http://127.0.0.1:8000/portal/
- 这是因为我觉得每次重新加载页面一次都很笨拙登录,它强制浏览器打开提示您确定要再次发送表单。
How do I get that url (once logged in) to look like http://127.0.0.1:8000/
or, if that's not feasible, http://127.0.0.1:8000/portal/
- this is because I think it's clumsy that every time you reload the page once logged in, it forces the browser to open the prompt Are you sure you want to send a form again?.
非常感谢您帮助Django新手!
Thank you so much for helping a Django newbie!
views.py
@cache_page(60 * 15)
def login_user(request):
#inactive_image_url = ""
#invalid_credentials_url = ""
context_instance=RequestContext(request)
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)
state = "You're successfully logged in!"
return render_to_response('ucproject/portal/index.html',
{'state':state, 'username':username}, context_instance=RequestContext(request))
else:
#state_img = inactive_image_url
state = "Your account is not active, please contact UC admin."
else:
#state_img = invalid_credentials_url
state = "Your username and/or password were incorrect."
return render_to_response('ucproject/index.html',
{'state': state,
#'state_img': state_img,
'username': username
}, context_instance=RequestContext(request))
def portal(request):
username = 'username'
return render_to_response('ucproject/portal/index.html',
{'state': state,'username': username}, context_instance=RequestContext(request))
urls.py
# Login / logout.
url(r'^registration/$', 'portal.views.registration'),
url(r'^login/$', 'portal.views.login_user'),
url(r'^portal/$', 'portal.views.portal'),
url(r'^portal/index.html$', 'portal.views.portal'),
推荐答案
在此处使用重定向替换HttpResponse:
Replace the HttpResponse here with a redirect:
if user.is_active:
login(request, user)
state = "You're successfully logged in!"
return render_to_response('ucproject/portal/index.html',
{'state':state, 'username':username}, context_instance=RequestContext(request))
相反,您要使用urls.py中的名称来消除歧义:
Instead, you want to use a name in urls.py to disambiguate:
urls.py
url(r'^portal/$', 'portal.views.portal', name='home'),
url(r'^portal/index.html$', 'portal.views.portal', name='home_index'),
然后在您的视图中使用这样的东西:
then use something like this in your view:
if user.is_active:
login(request, user)
return redirect('home')
redirect
是一个快捷方式功能,但您也可以创建并返回一个
HttpResponseRedirect
对象。通常没有任何意义。
redirect
is a shortcut function, but you could also create and return an HttpResponseRedirect
object. Usually there's no point in doing that.
这篇关于Django HttpResponseRedirect vs render_to_response - 如何获取登录表单以满足我需要的方式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!