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

前端 未结 6 1253
忘了有多久
忘了有多久 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:39

    Use Django's build in context processor to get the request in template context. In settings add request processor to TEMPLATE_CONTEXT_PROCESSORS

    TEMPLATE_CONTEXT_PROCESSORS = (
    
        # Put your context processors here
    
        'django.core.context_processors.request',
    )
    

    And in template use:

    {{ request.get_full_path }}
    

    This way you do not need to write any new code by yourself.

提交回复
热议问题