11๐
You shouldnโt use collectstatic
for your media directory. Remove '/home/admin/webapps/mainfolder/mainapp/media'
from STATICFILES_DIRS
, then set
MEDIA_ROOT = '/home/admin/webapps/mainfolder/mainapp/media'
Once youโve done this, the static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
should serve media files when DEBUG = True
.
For DEBUG = False
, you have to configure Apache to serve the media files.
26๐
In your urls.py
file:
add this line
from django.views.static import serve
add those two urls in urlpatterns:
url(r'^media/(?P<path>.*)$', serve,{'document_root': settings.MEDIA_ROOT}),
url(r'^static/(?P<path>.*)$', serve,{'document_root': settings.STATIC_ROOT}),
It worked for me ๐
9๐
If you are using Nginx, let it to serve media files
For Example
go to nginx/sites-available & add this
location /media/ { root */home/myprojectdir/myproject*; }
- How to override Django admin's views?
- How do I update an already existing row when using ModelForms?
- Is there a way to combine behavior of SESSION_EXPIRE_AT_BROWSER_CLOSE and SESSION_COOKIE_AGE
6๐
I have faced the same issue with DEBUG=False
. I solved it by configuring Nginx media location in the server block like this
location /media/ {
root FULL_PATH_TO_APP;
}
FULL_PATH_TO_APP is the full path to the directory where my media
folder exists.
- Django rest auth email instead of username
- Pivoting data and complex annotations in Django ORM
- Why isn't psycopg2 executing any of my SQL functions? (IndexError: tuple index out of range)
- Big integer field in django models
- How do you update a django template context variable after an AJAX call?
3๐
In Pythonanywhere server
Just
add Static file url and Directory
URL
/static/
/media/
Directory path
/home/taukir707/myblog/static
/home/taukir707/myblog/media
- How to run a Django celery task every 6am and 6pm daily?
- Django logging โ django.request logger and extra context
- How to stop gunicorn_django in virtualenv?
0๐
This is the best solution. Keep you media folder inside your static folder. And use this code in
Settings.py
# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/3.1/howto/static-files/
if(DEBUG==True):
STATIC_URL = '/static/'
MEDIA_URL = '/static/media/'
STATICFILES_DIRS=[
os.path.join(BASE_DIR,'static')
]
STATIC_ROOT='/home/username/websitedomain/static'
MEDIA_ROOT='/home/username/websitedomain/static/media'
else:
STATIC_URL = '/static/'
MEDIA_URL='static/media/'
STATIC_ROOT=os.path.join(BASE_DIR,'static')
MEDIA_ROOT=os.path.join(BASE_DIR,'static/media/')
- Best way to denormalize data in Django?
- Errno 13 while running docker-compose up
- Django i18n: how to not translate the admin site?
- Django + virtualenv + gunicorn โ No module named django.core.wsgi?
0๐
this helps me a lot
settings.py
STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, "static/")
MEDIA_URL = '/media/'
MEDIA_ROOT = os.path.join(BASE_DIR, "media")
STATICFILES_STORAGE = 'whitenoise.storage.CompressedStaticFilesStorage'
INSTALLED_APPS = [
'django.contrib.staticfiles',
'whitenoise.runserver_nostatic',
]
in url.py import URL and give static and media file URL
from django.views.static import serve
from django.conf.urls import url
- How to serialize queryset in get method in Django Rest Framework?
- Send email to bcc and cc in django
0๐
-
In settings.py:
DEBUG = False
-
In urls.py :
if not settings.DEBUG:
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
$> python manage.py runserver
Work fine in deployment or development!
- Styling django non-field errors on forms
- Variable not found. Declare it as envvar or define a default value
- Django โ template context processors โ breaking my app
- What is the use of cleaned_data in Django
0๐
devserver in secure mode
Step 1
Define a STATIC_ROOT and MEDIA_ROOT path in settings.py.
Code: settings.py
STATIC_URL = '/static/'
MEDIA_URL = '/media/'
if DEBUG:
STATICFILES_DIRS = [os.path.join(BASE_DIR, 'static')]
else:
STATIC_ROOT = os.path.join(BASE_DIR, 'static')
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
Step 2
Code: urls.py
from django.conf.urls import url
from django.conf import settings
from django.views.static import serve
urlpatterns = [
url(r'^media/(?P<path>.*)$', serve,{'document_root': settings.MEDIA_ROOT}),
url(r'^static/(?P<path>.*)$', serve,{'document_root': settings.STATIC_ROOT}),
]
Then run py manage.py runserver
0๐
Need to change Ngingx to add location:
location /media/ {
alias /home/xuser/xproject/media/;
expires 1d;
}