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.
Related
I am trying to load the static path of the image which is not working but the static path of the css files are working perfectly . I have tried every different way but still getting 404
this is i am trying to access the image which is inside the folder of ip in static folder
<div class="col-auto">
<img src="{% static 'ip/best.png' %}"/>
</div>
I have load the static block above
{% load static %}
and the settings.py configuraion of static url
STATIC_URL = '/static/'
STATICFILES_DIRS=[
os.path.join(BASE_DIR,'static')
]
I believe this is a media file so you need to add this to the project settings:
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
MEDIA_URL = '/media/'
VENV_PATH = os.path.dirname(BASE_DIR)
STATIC_ROOT = os.path.join(VENV_PATH, 'static_root')
you can add the folder to it and refer to it
I want to display static images and CSS file
settings.py
STATIC_PATH = os.path.join(BASE_DIR,'static')
STATIC_URL = '/static/'
STATICFILES_DIRS = ( BASE_DIR +"/articles/static",)
STATIC_ROOT = os.path.join(BASE_DIR,"static",)
In Template I am trying display the images from img folder
article.html
{% load staticfiles %}
<img src = "{% static '/img/default-by.gif' %}" alt = "left slide"/>
Folder Structure
The folder structure where I have tried to access static images according to the django docs https://docs.djangoproject.com/en/1.9/intro/tutorial06/
Folder Structure
NewsArticles
-->articles
--> static
-->articles
-->img
-->default-by.gif
db.sqlite3
manage.py
media
NewsArticles
README.md
static
templates
I am using firebug to check the image location its showing as mentioned http://127.0.0.1:8000/static/img/default-by.gif
You are giving wrong path for your image. It should be like this
{% load staticfiles %}
<img src = "{% static 'articles/img/default-by.gif' %}" alt = "left slide"/>
Hope this will works.
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
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