Hello,
I’m deploying an app and everything works fine until I change the DEBUG option on the settings.py to False.
My media files stop showing up on my pages. I have been in the process of try and error for a week now, searching and reading forums and I can’t seem to find the correct anwser. I understand that django does not serve media files on a production env but what is the setup then? I’ve tried a lot of different things and I can’t figure it out. Any help would be much appreciated.
Here are my setup on the settings.py, url.py and /etc/nginx/sites-enables/default
settings.py
MEDIA_URL = "/home/django/django_books/books/media/"
MEDIA_ROOT = os.path.join(BASE_DIR, '/home/django/django_books/books/media')
# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/4.1/howto/static-files/
STATIC_ROOT = os.path.join(BASE_DIR, 'static')
STATIC_URL = '/static/'
urls.py
from django.contrib.auth import views as auth_views
from django.urls import path, include
from django.conf import settings
from django.conf.urls.static import static
from users import views as user_views
urlpatterns = [
path('admin/', admin.site.urls),
path('register/', user_views.register, name='register'),
path('profile/', user_views.profile, name='profile'),
path('login/', auth_views.LoginView.as_view(template_name='users/login.html'), name='login'),
path('logout/', auth_views.LogoutView.as_view(template_name='users/logout.html'), name='logout'),
path('password-reset/',
auth_views.PasswordResetView.as_view(template_name='users/password_reset.html'),
name='password_reset'),
path('password-reset/done',
auth_views.PasswordResetDoneView.as_view(template_name='users/password_reset_done.html'),
name='password_reset_done'),
path('password-reset-confirm/<uidb64>/<token>/',
auth_views.PasswordResetConfirmView.as_view(template_name='users/password_reset_confirm.html'),
name='password_reset_confirm'),
path('password-reset-complete/',
auth_views.PasswordResetCompleteView.as_view(template_name='users/password_reset_complete.html'),
name='password_reset_complete'),
path('', include('books.urls')),
] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
/etc/nginx/sites-enables/default
location /media {
alias /home/django/django_books/books/media/;
}
# your Django project's static files - amend as required
location /static {
alias /home/django/django_books/books/static/;
}
# Proxy the static assests for the Django Admin panel
location /home/django/django_books/static/admin {
alias /usr/lib/python3/dist-packages/django/contrib/admin/static/admin/;
Regards,