Django Get absolute url for static files

前端 未结 6 646
孤独总比滥情好
孤独总比滥情好 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条回答
  •  -上瘾入骨i
    2021-01-07 23:21

    build_absolute_uri takes the location as an argument which handles the double slash problem. Unfortunately you cannot pass arguments via the django template language.

    You will need to build a custom template tag or filter which accepts an argument to the build_absolute_uri function.

    One of the many reasons I prefer Jinja as I can just do this:

    {{ request.build_absolute_uri(static('img/foo.png')) }}
    

提交回复
热议问题