I have six images name as 1.jpg to 6.jpg of each side of dice and I want to display image defined by a random number I generate in runtime.
How can I make dynamic path including this random number? I tried some ways but getting 404 image not found only.
settings.py
STATIC_URL = '/static/'
Folder with images:
D:\programs\Django_projects\src\dice\random_dice\static\images
Dice is my project name and random_dice is my app name.
I already tried this:
<img class="myImage" src='{% static "images/{{ number }}.jpg"%}' alt="can not load the image">
and
<img class="myImage" style="background-image:url('{{ STATIC_URL }} images/{{ number }}.jpg')" alt="can not load the image">
Also tried before the rendering page to make custom name and put in dict but still not working
view.py
def home(request):
X = random.randrange(1, 6)
result = {
"number": X,
"myImage": str(X) + '.jpg'
}
return rander(request, 'home.html', result)
In that condition ...
<img class="myImage" src='{% static "images/{{ myImage }}"%}' alt="can not load the image">
And
<img class="myImage" style="background-image:url('{{ STATIC_URL }} images/{{ myImage }}')" alt="can not load the image">
Should I move image folder to else position or change STATIC_URL or the syntax is wronge? I have no idea please help.
One of possible solutions:
<img class="myImage" src='{% static "images" %}{{ myImage }}' />
static "images" will prepend "images" with STATIC_URL, myImage was taken outside of {% %} and now double curly braces work fine.
Related
I use django in Ubuntu. I set static_url, static_root, media_url and media_root in settings.py like this code.
settings.py
DEBUG = False
STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, 'static')
MEDIA_URL='/media/'
MEDIA_ROOT=os.path.join(BASE_DIR,'static','media')
In index.html I set image like this code.
index.html
<img class="rounded mx-auto d-block" width="25%" src="../media/images/iot_logo.png">
After that, I use command django-admin collectstatic and open website but it not show image. How to fix it?
Use the built-in static tag
{% load static %}
<img class="rounded mx-auto d-block" width="25%" src="{% static 'media/images/iot_logo.png' %}">
Seems like path you given here in src is incorrect,
correct your path and image will be visible to you.
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 try to display all images from a directory that is in the project.
In the views.py
def showimages(request):
path='C:\\Users\Peter\PycharmProjects\displayimages\displayimages\display\static'
img_list = os.listdir(path)
return render(request, 'displayphotos.html', {'images':img_list})
In the html file
{% for image in images %}
<p>{{image}}</p>
<img src="{% static '{{image}}' %}">
{% endfor %}
But the display is
<p>DSC_5390.jpg</p>
<img src="/static/%7B%7Bimage%7D%7D">
<p>DSC_5392.jpg</p>
<img src="/static/%7B%7Bimage%7D%7D">
If I print them in paragraph tag, all file names are correct, but all wrong in img tag. How can I fix it?
you have an easy issue, just write
<img src="{% static {{image}} %}">
insead of
<img src="{% static '{{image}}' %}">
remove the simple quotes
if you have the image inside you static folder than you first need to load the static folder in setting file.
STATIC_URL = '/static/'
STATICFILES_DIRS = [
os.path.join(BASE_DIR, 'static'),
]
and than just load the image with the correct path in your template. if you have a folder for your images than
<img src="{% static 'images/grad-hat.png' %}">
else the image name
<img src="{% static 'grad-hat.png' %}">
first of all let me say that im a student trying to learn django...
i finished my app throught a tutorial and im having a problem with an image showing in my template, its just showing the frame with out the image... here is my html code:
<aside>
<h4>Historico</h4>
{% for month in months %}
<p>{{ month.2 }}</p>
{% endfor %}
<h4>Sobre mi</h4>
<img class="pic" src="{{ STATIC_URL }}/static/img/foto.jpg" alt="foto"/>
<img class="picbig" src="{{ STATIC_URL }}/static/img/foto.jpg" alt="foto grande"/>
<p>Soy un doge san OP platino IV lan pls</p>
</aside>
my image "foto.jpg" isnt showing, here is my settings.py for static
STATIC_URL = '/static/'
STATICFILES_DIRS = (
"C:/Users/Abdul Hamid/PycharmProjects/misitioweb/static",
)
STATICFILES_FINDERS = (
'django.contrib.staticfiles.finders.FileSystemFinder',
'django.contrib.staticfiles.finders.AppDirectoriesFinder',
)
and my img is in a folder like this
`C:\Users\User-Name\PycharmProjects\misitioweb\`
`blog-
-templtates
--frontpage.html
--otherthing.html
-__init__.py
-admin.py
-models.py
-test.py
-views.py
mysite-
-__init__.py
-settings.py
-urls.py
-wsgi.py
manage.py
-etc..
static-
--img
---foto.jpg
`
i have been trying to change the code in the img tag for diferent ressults like loading the static folder into the template like this:
{{from static import staticfiles}}
im doing something wrong, i already tryied searching for more info but nothing have worked... please help me!
anyway
thanks for your time
It looks like your src attribute is incorrect for you image.
<img class="pic" src="{{ STATIC_URL }}/static/img/foto.jpg" alt="foto"/>
should be
<img class="pic" src="{{ STATIC_URL }}img/foto.jpg" alt="foto"/>
Since STATIC_URL is already taking care of adding /static/ to your url. If you use the debugger tools in your browser and take a look at the src for that image you'll probably see it saying "/static/static/img/foto.jpg" when it should be "/static/img/foto.jpg"
Also ensure you have collected the static image files by running:
python manange.py collectstatic
I have a folder, outside of my app, that contains gigabytes of pictures. Then I need to display some of pictures listed in variable images. Now code looks like this:
settings.py
# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/1.6/howto/static-files/
STATIC_URL = '/mnt/Images/'
STATICFILES_DIRS = (
os.path.join(BASE_DIR, "static"),
'/mnt/Images/',
)
results.html
{% for im in images %}
<img width="100" height="100" src="/mnt/Images/1/{{im}}" alt="My Image" />
<strong>{{ im }}</strong><br>
{% endfor %}
It works but I'm not sure if changing STATIC_URL is the right way to go (otherwise it doesn't work). Is there a better way to display images from outside BASE_DIR