Django Get absolute url for static files

前端 未结 6 645
孤独总比滥情好
孤独总比滥情好 2021-01-07 22:38

In Django, when I use:

{{ request.build_absolute_uri }}{% static \"img/myimage.jpg\" %}

It produces: \'http://myurl.com//static/img/myimage

6条回答
  •  醉梦人生
    2021-01-07 23:26

    The build_absolute_uri method builds an absolute uri for the current page. That means that if you're on e.g. 'http://myurl.com/login/', the resulted full url would be 'http://myurl.com/login//static/img/myimage.jpg'.

    Instead, use request.get_host() (optionally together with request.scheme for the url scheme), or preferably, use the sites framework to set a template variable to the current site domain. The get_host() method has some issues regarding proxies.

    The get_host() method will return the current domain without a path appended.

提交回复
热议问题