Django请求找到以前的推荐人

Raj*_*eev 56 django django-templates django-models django-views

我将请求传递给模板页面.在django模板中如何传递新页面初始化的最后一页.而不是history.go(-1)我需要使用这个

 {{request.http referer}} ??

 <input type="button" value="Back" /> //onlcick how to call the referrer 
Run Code Online (Sandbox Code Playgroud)

Dan*_*olo 124

That piece of information is in the META attribute of the HttpRequest, and it's the HTTP_REFERER (sic) key, so I believe you should be able to access it in the template as:

{{ request.META.HTTP_REFERER }}
Run Code Online (Sandbox Code Playgroud)

Works in the shell:

>>> from django.template import *
>>> t = Template("{{ request.META.HTTP_REFERER }}")
>>> from django.http import HttpRequest
>>> req = HttpRequest()
>>> req.META
{}
>>> req.META['HTTP_REFERER'] = 'google.com'
>>> c = Context({'request': req})
>>> t.render(c)
u'google.com'
Run Code Online (Sandbox Code Playgroud)


Jef*_*uer 20

拉杰夫,这就是我所做的:

 <a href="{{ request.META.HTTP_REFERER }}">Referring Page</a>
Run Code Online (Sandbox Code Playgroud)


小智 12

这对我有用request.META.get('HTTP_REFERER') 有了这个,如果不存在,你不会得到错误,你会得到 None 代替