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.
- [Django]-Row level permissions in django
- [Django]-Python / pip, how do I install a specific version of a git repository from github (what is the correct url)?
- [Django]-Django with Pluggable MongoDB Storage troubles
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.
- [Django]-No module named MySQLdb
- [Django]-Error while installing uWSGI on mac
- [Django]-Django Rest Framework, passing parameters with GET request, classed based views
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" %}
- [Django]-Import Error: No module named django
- [Django]-Template filter to trim any leading or trailing whitespace
- [Django]-How to make a POST simple JSON using Django REST Framework? CSRF token missing or incorrect
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}}">
- [Django]-How to configure where to redirect after a log out in Django?
- [Django]-Can I set a specific default time for a Django datetime field?
- [Django]-How to create a custom decorator in Django?
-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.
- [Django]-Is there Django List View model sort?
- [Django]-ImproperlyConfigured: Requested setting INSTALLED_APPS, but settings are not configured
- [Django]-How to add new languages into Django? My language "Uyghur" or "Uighur" is not supported in Django
-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')) }}
- [Django]-Disable autocomplete on textfield in Django?
- [Django]-How do I include related model fields using Django Rest Framework?
- [Django]-Django The 'image' attribute has no file associated with it