code example
tooltip file
searching for the correct path
FILES:
settings.py
STATICFILES_DIRS = [
BASE_DIR / "static" ,
]
base.html
{% load static %}
How do I reference my static files in the following line of html code?...
<li class="ftco-animate"><span class="ion-logo-facebook"></span></li>
i.e. {% static 'website/...' %}
As described in the docs, assuming your filepath is BASE_DIR/static/sub_dir/example.pdf you can reference it like this:
<li class="ftco-animate">
<a href="{% static 'sub_dir/example.pdf' %}" data-toggle="tooltip" data-placement="top" title="Facebook">
<span class="ion-logo-facebook"></span>
</a>
</li>
It is good practice to place your static files in a sub-directory named same as your app name. That way, it will be much easier to reference the files from different apps later on. That is why the example in the docs says {% static 'my_app/example.jpg' %}.
Also please make sure you go through the documentations and follow all the steps mentioned there.
Related
I have an img folder in the same directory as my html files. I want to put images onto my site but I am doing something wrong.
this is the code:
{% block a %}
<div class="row">
<div class="col-sm-4 center">
<img src="img/img01.jpeg">
</div>
</div>
{% endblock %}
this is what happens when I run the local server.
this is my file directory
Websites generally need to serve additional files such as images, JavaScript, or CSS. In Django, we refer to these files as “static files”.
Those files need to be handled another way as your templates. The documentation can be found here.
It comes basically down to defining a seperate static directory inside your app. It is really good explained in the documentation.
I am unable to upload
style="background-image:url(images/home_slider.jpg)"
file in django. I have made two changes in settings .That is as follows:
STATIC_URL = '/static/'
STATICFILES_DIRS=[ os.path.join(BASE_DIR,'static')]
STATIC_ROOT=os.path.join(BASE_DIR,'assets')
I also tried this
style=" {% static'background-image:url(images/home_slider.jpg)' %}"
But unable to remove error.
Adding more details:
load static files in your template
{% load staticfiles %}
Usage of img tag:
<img src="{% static 'images/image.jpg'%}" alt="">
In your case:
style="background-image: url('{% static "images/image.jpg" %}');"
Add static only where you use the path of the file. So:
style="background-image: url({% static 'images/home_slider.jpg' %});"
Don't forget to place {% load static %} at the top.
I have a Django project with a static/img folder I want to get this image loaded but since it's in a variable and static points to my static folder, not static/img I'm not sure how to get it in. I tried f'img/{project.image}'
I'm iterating through projects so
project.image is set to equal "some_image_for_this_project.jpg" in my database
<img class="card-img-top" src="{% static project.image %}">
Probably not the most elegant solution, but this is how it should work:
<img src="{% static 'images/' %}{{ project.image }}>
Maybe it will help
1.) Youre template.html
{% load static %}
{% static 'img/name_img.jpg' %}
*This solution works for the directory structure
app
/__pycache__
/migrations
/templates
/static/img/[YOURE_FILES...]
__init__.py
[...]
I am trying to create image tags for every file in a directory located in static in my Django project. What is the best way to do this? I suppose I could write pure python with os.listdir and just use strings to concatenate the image tags together using the filenames, but I'm sure there's a better way using Django's HTML formatting. Haven't been able to figure it out.
Example: In a folder there is img1.jpg and img2.jpg.
How can I produce:
<img src="img1.jpg" alt="img1.jpg" height="100px" width="100px">
<br>
<img src="img2.jpg" alt="img2.jpg" height="100px" width="100px">
<br>
I'm not clear about what you're looking for, but from
create image tags for every file in a directory located in static
I'm assuming that you're looking to load image using the {% static %} tag:
{% load static %}
...
<img src="{% static 'path_to_image' %}" alt="img1.jpg" height="100px" width="100px">
<!-- Watch out the single and double quotes -->
Do remember to include this in your settings.py:
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
STATIC_URL = '/static/'
STATICFILES_DIRS = [
os.path.join(BASE_DIR, 'static'), // or wherever you put your static files
]
I think it's now automatic, but check your INSTALLED_APPS:
INSTALLED_APPS = [
...
'django.contrib.staticfiles',
]
Compared to hard-coded paths like C:/foo/baz/bar/, it's better to load static tags because it always works on different platforms as every operating system has its own way to handle files, which I hope answers your question.
I'm trying to do load the following static file
img file
where image is in a for loop of images derived from a database.
But I simply got an error Could not parse the remainder: '{{' from ''static/matrices/'{{'
What should I do to fix this? I can't use relative paths because this will be used by subsections as well, with the same html template.
You should pass a full string to the static tag from staticfiles. This is so it can use your staticstorages to find your file.
{% load staticfiles %}
{% with 'images/'|add:image.title|add:'.png' as image_static %}
{% static image_static %}
{% endwith %}
But in your use case it might be better if you just store the path of the images on the image model itself.
I got this to work by using an empty string for the static path and then using my variables in their own section, like this:
<a href= "{% static "" %}{{obj.a}}/{{obj.b}}/{{obj.c}}.gz" >Name</a>
You can use get_static_prefix template tag. get_static_prefix is a variable that contains the path specified in your STATIC_URL. Your code would be:
{% load static %}
img file
or
{% load static %}
{% get_static_prefix as STATIC_PREFIX %}
img file
Reference: get_static_prefix
You should avoid nesting tags.
What are you trying to solve? Isn't the image part of dynamic content? The static tag is for static content not uploaded media files.
If you must use the static tag the correct way would be something in the order of;
{% static image %} or {% static image.file %}
Depending on the object layout. If you're using an ImageField (inherits FileField) the image object already holds the path, so there's no need to manually add extensions.
What I ended up doing was to just split the path from the name itself. Those images represents tabs, are static and in a sequence. They are not connected to the database imagewise.
I didn´t want to repeat the html for every run so I ended up doing a forloop like this, a bit simplified.
{% for option in options_obj %}
<img class="highlight" src="{% static "store/img/editor/tab-" %}
{{ option.name.lower }}-selected.png">
{% endfor %}
EDIT:
Even though this does work in most situations it can really mess stuff up as well. For me this occured when having different images for different language settings and at the same time using tools like CachedStaticFilesStorage. If you need to add language code or something else to your image this is a more solid solution
{% for option in options_obj %}
<img class="highlight" src="{% static "store/img/editor/tab-"
|add:LANGUAGE_CODE|add:"-selected.png" %}">
{% endfor %}
Too many quotes!
Just
img file
and you don't have to call the static in the link because you already load the static