Displaying image source from MEDIA_ROOT - python

I have my media_root path set to
MEDIA_ROOT = os.path.join(os.path.dirname(__file__), 'media')
MEDIA_URL = 'media/'
with my project structured as so
Project_Name/
media/
profiles/
App_Name
when I try to reference the images in my profiles/ directory, I get 404 no resource found errors.
src="{{ MEDIA_URL }}{{ result.photo }}">
This resolves to http://127.0.0.1:8000/media/profiles/image1.jpg cannot be found. Am I missing absolute path, I am using DJango 1.4, I do not have the media root set in my urls.py but I didn't with my STATIC_URL definitions and I am able to find my static resources?

This is the main path of your project
PROJECT_ROOT = os.path.join(os.path.dirname(__file__), '..')
SITE_ROOT = PROJECT_ROOT
In here, the system call your media folder path where uploaded images store
MEDIA_ROOT = os.path.join(SITE_ROOT, 'media')
MEDIA_URL = '/media/'
In here are your static files where css/js/img/ ... store
STATIC_ROOT = os.path.join(SITE_ROOT, 'static')
STATIC_URL = '/static/'
Additional location for staticfiles
STATICFILES_DIRS = (
os.path.join(SITE_ROOT, 'staticfiles'),
)
urls.py
from django.conf.urls import patterns, include, url
from django.conf.urls.static import static
from django.contrib import admin
from django.contrib.staticfiles.urls import staticfiles_urlpatterns
from project_name import settings
admin.autodiscover()
urlpatterns = patterns('',
.........
.........
) + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
urlpatterns += staticfiles_urlpatterns()

Here is the answer
Images from media folder is not displaying django template

Related

Django Unable to load static files - internal-nginx-static comes in URL

Hope you are all having a very good Friday!
I am running a django project on nginx, and my project is each time going into this url - https://sitename.com/internal-nginx-static-location_uat/project_name/static/images/success.svg
I didn't configure this internal-nginx-static, and If I load the same project on another website, it works fine, is it something I can handle in code, or I have make changes in server conf.
Here is my URL.py Files
from django.conf import settings
from django.contrib import admin
from django.conf.urls.static import static
from django.urls import path, include
urlpatterns = [
path('', include('portal.urls')),
path('admin/', admin.site.urls),
] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
and settings file
STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(DATA_DIR, 'static')
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
MEDIA_URL = '/media/'

404 Static file not found - Django

I have an issue with django. I recently bought an instance of a shared server and I wanted to move my django website from AWS to this server (which use Cpanel). All worked fine with AWS but when I switched to Cpanel all statics files were missing.
this is my settings.py:
STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, 'static')
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
MEDIA_URL = "/media/"
STATICFILES_DIRS = [
os.path.join(BASE_DIR, 'static'),
]
my project structure:
my_project
|-app/
|-...
|-views.py
|-db.sqlite3
|-manage.py
|-media/
|-my_project/
|-...
|-settings.py
|-static/
|-main_page/
|-js/
|-my-script.js
I add static files like this:
{% load static %}
<script src="{% static 'main_page/js/my-script.js' %}"></script>
This is the error:
GET http://my.domain.com/static/main_page/js/my-script.js net::ERR_ABORTED 404 (Not Found)
When I go to the URL of the file it understands it like one of my URLs:
I hope you will help me to solve this issue ;)
thanks.
you need to add the static & media files config in the urls.py , like this
from django.conf import settings
from django.conf.urls.static import static
urlpatterns = [
# ... the rest of your URLconf goes here ...
]
urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
the django docs : https://docs.djangoproject.com/en/3.1/howto/static-files/

How to add media_root and media_url in Django

I am using Django 3, and I have tried every solution available on the internet, but none of them worked for me.
When I set DEBUG=False, I am unable to display an image on the HTML page.
Here are my settings
-root_app
--main_app
---settings.py
---asgi.py
---urls.py
---wsgi.py
--sub_app
---admin.py
---urls.py
---views.py
---static
----style.css
---templates
----home.html
---media
----images
media path in settings.py
MEDIA_ROOT = os.path.join(BASE_DIR,'sub_app','media')
MEDIA_URL = '/media/'
Here what I have done to resolve it
add context_processor
django.template.context_processors.media'
add +static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT) in url_patterns of urls.py of sub_app
I changed static storage to STATICFILES_STORAGE = 'whitenoise.storage.CompressedStaticFilesStorage', otherwise I got 500 error. Check this comment
But still, I am unable to display the image at home.html page.
When I click on image source using inspect element, I got this
/media/images/img.png
These settings resolved the issue
Media Path
MEDIA_ROOT = os.path.join(BASE_DIR, 'sub_app','media')
MEDIA_URL = '/media/'
Static Path
STATIC_ROOT = os.path.join(BASE_DIR, 'sub_app','static')
STATIC_URL = '/static/
Adding re_path line in urls.py of main_app
from django.conf.urls.static import static
from django.views.static import serve
urlpatterns = [
path('admin/', admin.site.urls),
path('',include('sub_app.urls')),
re_path(r'^media/(?P<path>.*)$', serve, kwargs={'document_root': settings.MEDIA_ROOT})
]
Change this
MEDIA_ROOT = os.path.join(BASE_DIR,'sub_app','media')
to
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
in settings.py and use
{{MEDIA_URL}}images/image.png
inside your template.
An edit to Dr. Abhishek's answer: yes, you need the MEDIA_ROOT setting, but you need to append it into the STATICFILES_DIRS list also, which will look something like this:
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
STATICFILES_DIRS = [
# ...
os.path.join(BASE_DIR, 'media'),
]
When you load the image, use a soft-encoded path like this:
{% load static %}
<!-- remember to change the file name/type! -->
<img src="{% static 'images/image.jpg' %}" alt="Image">

Django static files not working on Heroku

My django application is working well on local server. But, when I deploy it on Heroku, the static files are not being served (getting a 404 error). Please help!
from django.conf.urls import patterns, include, url
from django.contrib import admin
from django.conf.urls.static import static
from django.conf import settings
urlpatterns = patterns('',
url(r'^$', 'product.views.home', name='home'),
url(r'^admin/', include(admin.site.urls)),
)
if settings.DEBUG:
urlpatterns += static(settings.STATIC_URL,document_root=settings.STATIC_ROOT)
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
static files settings:
TEMPLATE_DIRS = (
os.path.join(BASE_DIR, 'templates'),
)
STATIC_URL = '/static/'
MEDIA_URL = '/media/'
MEDIA_ROOT = os.path.join(BASE_DIR, "static", "media")
STATIC_ROOT = os.path.join(BASE_DIR, "static", "static_root")
STATICFILES_DIRS = (
os.path.join(BASE_DIR, "static", "static_dirs"),
)
WSGI file -
import os
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "acton.settings")
from django.core.wsgi import get_wsgi_application
application = get_wsgi_application()
try:
from dj_static import Cling
application = Cling(get_wsgi_application())
except:
pass
This is my setting for static files to deploy on Heroku.
Hope it will help :
import os
BASE_DIR = os.path.dirname(os.path.abspath(file))
STATIC_ROOT = 'staticfiles'
STATIC_URL ='/static/'
MEDIA_ROOT = os.path.join(PROJECT_PATH, "media")
MEDIA_URL = "/media/"
STATICFILES_DIRS = (
os.path.join(BASE_DIR, 'static'),
)
Your setting.py file is incorrectly configured.Static and media files should be
STATICFILES_DIRS = os.path.join(BASE_DIR, 'static')
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
For anyone else that comes across this problem, for me it was that I was missing the whitenoise configuration from my wsgi.py file.
Specifically the following was missing from my wsgi.py file:
from whitenoise.django import DjangoWhiteNoise
application = DjangoWhiteNoise(application)
Docs are here: http://whitenoise.evans.io/en/stable/

Static Files With Django Development Server 1.7 Slightly Different Directory Settings

This is my website directory:
django_project
\bin
\include
\lib
\src
\django_project
settings.py
\app2
manage.py
\static
\js
\css
\media
\templates
base.html
What I have added to my settings.py is:
STATIC_URL = '/static/'
TEMPLATE_DIRS = (
os.path.join(os.path.dirname(BASE_DIR), "templates"),
)
if DEBUG:
MEDIA_URL = '/media/'
STATIC_ROOT = os.path.join(os.path.dirname(BASE_DIR), "static"),
MEDIA_ROOT = os.path.join(os.path.dirname(BASE_DIR), "media"),
STATICFILES_DIRS = (
os.path.join(os.path.dirname(BASE_DIR), "static"),
),
and to my urls.py:
if settings.DEBUG:
urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
and hey, it does'nt work. The way I refer to them in the base.html is:
<link href="/static/css/bootstrap.min.css" rel="stylesheet">
Any ideas?
Thank you.
Hasan
Running the settings that you've pasted gave me an error ImproperlyConfigured: The STATICFILES_DIRS setting should not contain the STATIC_ROOT setting. Commenting-out os.path.join(os.path.dirname(BASE_DIR), "static"), in the settings removed the error and the template correctly found the CSS file without the URLs.py modifications.
First of all as #r---------k mentioned, I should not have had trailing commas except for the tuples. So the settings.py should look like:
if DEBUG:
MEDIA_URL = '/media/'
STATIC_ROOT = os.path.join(os.path.dirname(BASE_DIR), "static")
MEDIA_ROOT = os.path.join(os.path.dirname(BASE_DIR), "media")
STATICFILES_DIRS = (
os.path.join(os.path.dirname(BASE_DIR), "static", "static")
),
And the second thing is that, as you may notice, STATIC_ROOT and STATICFILES_DIRS cannot possibly have the same value. I added another static folder inside the static folder and put my js and css folders inside of it:
STATICFILES_DIRS = (
os.path.join(os.path.dirname(BASE_DIR), "static", "static")
),
The directory looks like:
django_project
\bin
\include
\lib
\src
\django_project
settings.py
\app2
manage.py
\static
\static
\js
\css
\media
\templates
base.html
Finally, you should have the urlpatterns in your urls.py:
if settings.DEBUG:
urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
This seems to work in current django (2.2) in your urls.py:
from django.conf import settings
from django.conf.urls.static import serve
from django.urls import include, path
urlpatterns += [
path(settings.STATIC_URL[1:], serve, {'document_root': settings.STATIC_ROOT })
]
Remember to run ./manage.py collectstatic to collect the static files to settings.STATIC_ROOT

Categories