Django Get Absolute URL

前端 未结 2 1444
南笙
南笙 2020-12-29 09:17

I want to get absolute url in templates. I can\'t do with url. It gives me a relative URL. I need to get this:

http://domain.tld/article/post
2条回答
  •  独厮守ぢ
    2020-12-29 09:50

    After a long time meeting with Django, I learned a lot of things. For this issue, I created an absolute URL templatetag.

    Add this to your template tags, then use like default url tag:

    {% absurl 'some-view' with, arguments %}

    Here is the Gist for the absolute URL templatetag, you will need to add request object to template_context_processors, otherwise this will not work. To achieve this, open your settings.py and add these following lines:

    from django.conf import global_settings
    TEMPLATE_CONTEXT_PROCESSORS = global_settings.TEMPLATE_CONTEXT_PROCESSORS + (
        'django.core.context_processors.request',
    )
    

提交回复
热议问题