我在 Django 中有以下代码:
def signup(request)
http_referer = request.META.get('HTTP_REFERER','/')
if request.user.is_authenticated():
return HttpResponseRedirect(reverse('index'))
else:
if request.method == 'GET':
# return HttpResponse(http_referer) Here it returns the right value
args={}
return render(request,'signup.html',args)
elif request.method =='POST':
# return HttpResponse(http_referer) Here it breaks and returns current url
... actual code goes here, but this should be enough
我想保存以HTTP_REFERER
备后用,因为它正在更改。我试过将它存储在变量中,但不知何故变量仍然会自动改变。我想我可以尝试将它暂时存储在数据库中(它可能不会在那里改变),但因为我只需要它在那个函数中并且再也不需要它,这似乎是一个糟糕的解决方案。有没有更好的方法来做到这一点?
编辑
好吧,现在我觉得自己很愚蠢,因为我意识到我正在保存http_referer
每个请求(POST
会自动更改它并且我会保存该更改),而不是GET
像下面的用户建议的那样仅在对会话的请求中保存它:)。