Django中的HttpResponseRedirect和Facebook
我有一个表单,上面有两个按钮。根据用户点击哪个按钮,会带他们去不同的网址。
friend_id = request.POST.get('selected_friend_id_list')
history = request.POST.get('statushistory')
if history:
print "dfgdfgdf"
return HttpResponseRedirect('../status/')
else:
return direct_to_template(request, 'friends_list.fbml',
extra_context={'fbuser': user,
'user_lastname':user_lastname,
'activemaintab':activemaintab,
'friends':friends,
'friend_list':friend_list})
这是用来显示的模板:
<input type="submit" value="Calendar View" name="calendarview"/>
<input type="submit" value="Status History" name="statushistory"/>
</form
现在我的问题是,页面没有跳转到指定的网址。如果我使用 HttpResponseRedirect('../'),虽然能显示正确的页面,但网址并没有改变。
当前页面是 "friendlist/status/",所以在提交表单后,我希望网址变成 "friendlist/list/"。理论上,使用 HttpResponseRedirect('../list/') 应该能实现这个,但网址就是不变。
有没有什么办法可以解决这个问题?谢谢!
2 个回答
0
为什么需要使用相对网址?难道不能用绝对网址吗?
2
我遇到的问题是,页面没有跳转到指定的链接。如果我使用 HttpResponseRedirect('../'),虽然能显示正确的页面,但网址却没有变化。
这里提到的“网址”,我猜是指“浏览器上显示的链接”。如果你的问题能更具体一些,那就更好了。
首先,你必须提供一个完整的网址。http://docs.djangoproject.com/en/dev/ref/request-response/#django.http.HttpResponseRedirect
根据标准(RFC 2616,第14.30节),是需要一个完整的网址的。有些浏览器可能能接受相对网址,但有些则不行。
其次,你在程序中绝对不要使用相对网址。
你应该使用 reverse 方法。
from django.core.urlresolvers import reverse
def myview(request):
theURL= reverse('path.to.viewFunction')
return HttpResponseRedirect(theURL)