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
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 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.
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.
Pls tell me. Why ../static/image.jpg show images, and ../media/image.jpg now show
Need write url? Need change Setting? i dont fiend answer in documentation.
pls help.
2 night search answer.
Need upload from admin-panel photo and show in templates.
<img src="{{ tovar.product_img.url }}">
To display image you need load the static files in your template before referencing them. This is how you should display a picture in your template:
{% load staticfiles %}
<img src="{% static 'image.jpg' %}">
This image needs to be stored in your static folder. ../YourApp/YourApp/static/image.jpg is where I keep my static folder. Obviously it would be better to structure it further with images folder inside the static folder etc..
In your settings file you need the following lines:
# Static asset configuration
BASE_DIR = os.path.dirname(os.path.abspath(__file__))
STATIC_ROOT = 'staticfiles'
STATIC_URL = '/static/'
STATICFILES_DIRS = (
os.path.join(BASE_DIR, 'static'),
)
This should do the trick for you.
In settings.py
MEDIA_ROOT = '/path/to/yourmediafolder/'
MEDIA_URL = '/media/' # whatever but it should same in `urls.py`
In urls.py
urlpatterns += patterns('',
(r'^media/(?P<path>.*)$', 'django.views.static.serve', {
'document_root': settings.MEDIA_ROOT}))
Then in template
<img src="{{ MEDIA_URL }}images/imagename.jpg"/>
Note: Here image should be as '/path/to/yourmediafolder/images/imagename.jpg'
Complete example:
I have an image test.jpg as '/home/me/test.jpg
MEDIA_ROOT = '/home/' # or /home/me/ but change url in image src
MEDIA_URL = '/media/'
#urls.py same as above
In template
<img src="{{ MEDIA_URL }}me/test.jpg"/> # or <img src="{{ MEDIA_URL }}test.jpg"/> as or condition above in MEDIA_ROOT.
Note that {{ MEDIA_URL }}me , no / between them because MEDIA_URL='/media/
You can test by:
http://domain.com/media/me/test.jpg # or http://domain.com/media/test.jpg as OR condition in MEDIA_ROOT
in local:
http://localhost:8000/media/me/test.jpg #in locally
This works (in the html img tag):
src="(left squiggly, percent) static poc.image.name (percent, right squiggly):
...and thanks to whoever pointed out that ImageField has a name in addition to a (useless) path and (useless) url e.g.:
>>> from nutr.models import *
>>> poc=POC.objects.get(pk=87)
>>> poc.name
'Edgar'
In Django==2.0.2 use {% get_media_prefix %}
From the docs
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