Reputation: 13
I have a django project that works very well and shows all media files uploaded from the admin when debug = True but immediately i turn change to debug = False django cannot find my media folder yet it loads my static folder. as you can see i have set up my MEDIA_ROOT and MEDIA_URL correctly.
My settings.py
# Static files (CSS, JavaScript, Images)
import os
STATIC_URL = '/static/'
STATICFILES_DIRS = [
os.path.join(BASE_DIR, 'static'),
]
STATIC_ROOT = os.path.join(BASE_DIR,'staticfiles')
MEDIA_URL = '/media/'
MEDIA_ROOT = os.path.join(BASE_DIR,'media')
#HTTPS SETTING
SESSION_COOKIE_SECURE = True
CSRF_COOKIE_SECURE = True
SECURE_SSL_REDIRECT = True
My urls.py:
urlpatterns = [
path('', include('appMain.urls',namespace='main')),
path('auth/', include('appAuth.urls',namespace='auth')),
path('blog/', include('blog.urls',namespace='blog')),
]+static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
So i need some help to display my media files when debug= False
Upvotes: 1
Views: 949
Reputation: 21
make this in 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')
and in urls.py import
from django.conf.urls import url
from django.conf import settings
from django.views.static import serve
and paste this in your urlspatterns whith your path
url(r'^media/(?P<path>.*)$', serve,{'document_root': settings.MEDIA_ROOT}),
url(r'^static/(?P<path>.*)$', serve,{'document_root': settings.STATIC_ROOT}),
like this
urlpatterns = [
url(r'^media/(?P<path>.*)$', serve,{'document_root': settings.MEDIA_ROOT}),
url(r'^static/(?P<path>.*)$', serve,{'document_root': settings.STATIC_ROOT}),
path('admin/', admin.site.urls),
]
Upvotes: 2