Django request to find previous referrer

后端 未结 2 1006
隐瞒了意图╮
隐瞒了意图╮ 2020-12-12 22:34

I am passing the request to the template page.In django template how to pass the last page from which the new page was initialised.Instead of history.go(-1) i need to use t

相关标签:
2条回答
  • 2020-12-12 23:17

    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 }}
    

    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'
    
    0 讨论(0)
  • 2020-12-12 23:26

    Rajeev, this is what I do:

     <a href="{{ request.META.HTTP_REFERER }}">Referring Page</a>
    
    0 讨论(0)
提交回复
热议问题