Django: get URL of current page, including parameters, in a template

前端 未结 6 1240
忘了有多久
忘了有多久 2020-12-01 00:05

Is there a way to get the current page URL and all its parameters in a Django template?

For example, a templatetag that would print a full URL like /foo/bar?p

6条回答
  •  一生所求
    2020-12-01 00:19

    In a file context_processors.py (or the like):

    def myurl( request ):
      return { 'myurlx': request.get_full_path() }
    

    In settings.py:

    TEMPLATE_CONTEXT_PROCESSORS = (
      ...
      wherever_it_is.context_processors.myurl,
      ...
    

    In your template.html:

    myurl={{myurlx}}
    

提交回复
热议问题