[Django]-Django Get absolute url for static files

26👍

The request object is available within your templates and you can easily access attributes such as request.scheme or request.META.HTTP_HOST to construct your base URL that you can prepend ahead of your static URL to get the full URL.

Final example would look something like this:

<img src="{{request.scheme}}://{{request.META.HTTP_HOST}}{% static 'img/myimage.jpg' %}">

19👍

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.

👤knbk

7👍

I just made a quick template tag for doing this. Create files /myapp/templatetags/__init__.py and /myapp/templatetags/my_tag_library.py, if you don’t have them already, and add the following to my_tag_library.py:

from django import template
from django.templatetags import static

register = template.Library()

class FullStaticNode(static.StaticNode):
    def url(self, context):
        request = context['request']
        return request.build_absolute_uri(super().url(context))


@register.tag('fullstatic')
def do_static(parser, token):
    return FullStaticNode.handle_token(parser, token)

Then in your templates, just {% load my_tag_library %} and use e.g. {% fullstatic my_image.jpg %}.

In response to earlier comments wondering why someone would need to do this, my particular use case was that I wanted to put a link to a static file inside of an open graph protocol meta tag, and those links need to be absolute. In development the static files get served locally, but in production they get served remotely, so I couldn’t just prepend the host to get the full url.

2👍

Use this for apps:

{{ request.build_absolute_uri|slice:":-1" }}{% static "img/myimage.jpg" %}

Use this generally:

{{ request.scheme }}://{{ request.META.HTTP_HOST }}{% static "img/myimage.jpg" %}
👤Bamdad

1👍

Is this worth an update (Django 2+)?

This helped me specifically because I was trying to put a query in the link, i.e. the myimage.jpg was actually pulling from the DB. I needed a way to put it in the src, which was to replace ‘myimage.jpg’ with {{ img_link_field_in_model }}.

<img src="{% get_static_prefix %}img/myimage.jpg">

will produce:

<img src="/static/img/myimage.jpg">

The example of the query is:

<img src="{% get_static_prefix %}img/{{img_link_from_model}}">
👤Dr J

-4👍

Not entirely sure what you’re asking, but since the {% static .. %} is only adding /static/ to the front of your path you specify, you could just do that yourself:

{{ request.build_absolute_uri }}static/img/myimage.jpg

Not very modular, but then again most times you don’t need direct access to the full url since it will just append it onto whatever url you’re at if you use it as a src for some html object.

-6👍

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')) }}

Leave a comment