Django static path of the image is not working - python

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

Related

Django cannot show image

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.

Django static files not working for other templates except index

I am developing an agency web app with django templates & static files. But I am having trouble with loading static files on pages other than index.
Here's my folder heirarchy.
Again, I am having no problems loading static files on my index page.
Here's code of my additional page services.html (one I am trying to load static files on):
{% extends 'base.html' %}
{% load static %}
{% block content %}
...
...
...
{% endblock %}
My base.html is working totally fine with the homepage index.html.
I've included this on my settings.py
STATIC_URL = '/static/'
STATIC_ROOT = '/static/'
STATICFILES_DIRS = (
os.path.join(BASE_DIR, 'static'),
)
STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles')
And, my views.py and urls.py files are working fine too.
Also my CSS file is correctly linked to the services.html file. I can tell this from the inspection.
Here's the error list shown on inspection. Help will be appreciated. Thank you.
Your static files are stored in the core folder, so one way is to change the settings.py to :
STATIC_URL = '/static/'
STATIC_ROOT = '/static/'
STATICFILES_DIRS = (
os.path.join(BASE_DIR, 'core/static'),
)
STATIC_ROOT = os.path.join(BASE_DIR, 'static')
The other way is to use python manage.py collectstatic. A new staticfiles directory will be created which has folders for admin (the built-in admin has its own static files), staticfiles.json, and whatever directories are in your static folder.
RESOLVED.
I was apparently not using Django HTML tags to link my static CSS/JS files.
So, I initialized static files on my base.html with {% load static %} and replaced links with href="{% static 'assets/css/style.css' %} format. This worked out. Remember that full path shouldn't be written i.e {% static './static/assets/...' %} instead just use {% static 'assets/...' %}.

Django, CSS is loading, however images and js return a 404

I am creating a Django website and have created a login pages for the website. When I run this on the development server the CSS for the page loads but the images and JavaScript are returning a 404 error. but they are in the same assets folder.
Folder Structure:
settings.py:
STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, 'static')
STATICFILES_DIRS = [os.path.join(BASE_DIR, 'assets')]
Login.html:
<div class="h-center">
<img class="logo" src="{% static 'assets/base/img/logo/logo.jpg' %}">
</div>
I don't understand why it is only loading the CSS files.
comment the STATIC_ROOT like this:
STATIC_URL = '/static/'
# STATIC_ROOT = os.path.join(BASE_DIR, 'static')
STATICFILES_DIRS = [os.path.join(BASE_DIR, 'assets')]
STATIC_ROOT is for command "collectstatic"

How to access static files in subfolders?

fellows!
I have problems with static files. I still don't understand how this really works.
Problem is that when I try to keep files in subfolder of the app folder, Django returns 404.
Folders structure:
/static/
product/
images/
img.png
I am trying to access img.png in html template like this:
<img src="{% static "product/images/img.png" %}">
But 404 error is returned.
If I move img.png to the parent folder:
/static/
product/
img.png
images/
And use this root:
<img src="{% static "product/img.png" %}">
All works fine.
This is my Settings.py:
STATICFILES_DIRS = [
os.path.join(BASE_DIR, 'review', 'static')
]
STATIC_ROOT = os.path.join(BASE_DIR, 'review', 'static')
STATIC_URL = '/static/'
STATICFILES_FINDERS = ['compressor.finders.CompressorFinder']
COMPRESS_PRECOMPILERS = (('text/x-scss', 'django_libsass.SassCompiler'),)
COMPRESS_URL = STATIC_URL
COMPRESS_ROOT = STATIC_ROOT
What I should change so I can access static in the subfolders?

Django not show images

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

Categories

Resources