Setting up media files in production with nginx

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,

Looking on this configuration, it looks like books is one of your apps, and django_books is your project root directory. Possibly, it’s on django_books that the BASE_DIR configuration is pointing at.

So on this line:

You’re saying that you’re going to have a static folder under django_books. This is the location that all the static files will live, after you run the collectstatic management command.
The same concept applies for the media files.

So you should assert that you done the following steps on the host machine:
1 - Run the collectstatic management command;
2 - Update the nginx alias to the path that is referenced on the STATIC_ROOT / MEDIA_ROOT

Further note.
I think that this line will have no use, or be redundant after you run the collectstatic cmd:

Hi Leandro,

When I run the collecstatic cmd I get this error messages:

´´´
root@django-server:/home/django/django_books# python3 manage.py collectstatic

You have requested to collect static files at the destination

location as specified in your settings:

/home/django/django_books/static

This will overwrite existing files!

Are you sure you want to do this?

Type ‘yes’ to continue, or ‘no’ to cancel: yes

Found another file with the destination path ‘admin/css/dark_mode.css’. It will be ignored since only the first encountered file is collected. If this is not what you want, make sure every static file has a unique path.

Found another file with the destination path ‘admin/css/forms.css’. It will be ignored since only the first encountered file is collected. If this is not what you want, make sure every static file has a unique path.

Found another file with the destination path ‘admin/css/rtl.css’. It will be ignored since only the first encountered file is collected. If this is not what you want, make sure every static file has a unique path.

Found another file with the destination path ‘admin/css/dashboard.css’. It will be ignored since only the first encountered file is collected. If this is not what you want, make sure every static file has a unique path.

Found another file with the destination path ‘admin/css/autocomplete.css’. It will be ignored since only the first encountered file is collected. If this is not what you want, make sure every static file has a unique path.

Found another file with the destination path ‘admin/css/widgets.css’. It will be ignored since only the first encountered file is collected. If this is not what you want, make sure every static file has a unique path.

Found another file with the destination path ‘admin/css/changelists.css’. It will be ignored since only the first encountered file is collected. If this is not what you want, make sure every static file has a unique path.

Found another file with the destination path ‘admin/css/fonts.css’. It will be ignored since only the first encountered file is collected. If this is not what you want, make sure every static file has a unique path.

Found another file with the destination path ‘admin/css/base.css’. It will be ignored since only the first encountered file is collected. If this is not what you want, make sure every static file has a unique path.

Found another file with the destination path ‘admin/css/login.css’. It will be ignored since only the first encountered file is collected. If this is not what you want, make sure every static file has a unique path.

Found another file with the destination path ‘admin/css/nav_sidebar.css’. It will be ignored since only the first encountered file is collected. If this is not what you want, make sure every static file has a unique path.

Found another file with the destination path ‘admin/css/responsive_rtl.css’. It will be ignored since only the first encountered file is collected. If this is not what you want, make sure every static file has a unique path.

Found another file with the destination path ‘admin/css/responsive.css’. It will be ignored since only the first encountered file is collected. If this is not what you want, make sure every static file has a unique path.

Found another file with the destination path ‘admin/css/vendor/select2/LICENSE-SELECT2.md’. It will be ignored since only the first encountered file is collected. If this is not what you want, make sure every static file has a unique path.

Found another file with the destination path ‘admin/css/vendor/select2/select2.min.css’. It will be ignored since only the first encountered file is collected. If this is not what you want, make sure every static file has a unique path.

Found another file with the destination path ‘admin/css/vendor/select2/select2.css’. It will be ignored since only the first encountered file is collected. If this is not what you want, make sure every static file has a unique path.

Found another file with the destination path ‘admin/js/actions.js’. It will be ignored since only the first encountered file is collected. If this is not what you want, make sure every static file has a unique path.

Found another file with the destination path ‘admin/js/inlines.js’. It will be ignored since only the first encountered file is collected. If this is not what you want, make sure every static file has a unique path.

Found another file with the destination path ‘admin/js/calendar.js’. It will be ignored since only the first encountered file is collected. If this is not what you want, make sure every static file has a unique path.

Found another file with the destination path ‘admin/js/core.js’. It will be ignored since only the first encountered file is collected. If this is not what you want, make sure every static file has a unique path.

Found another file with the destination path ‘admin/js/urlify.js’. It will be ignored since only the first encountered file is collected. If this is not what you want, make sure every static file has a unique path.

Found another file with the destination path ‘admin/js/filters.js’. It will be ignored since only the first encountered file is collected. If this is not what you want, make sure every static file has a unique path.

Found another file with the destination path ‘admin/js/jquery.init.js’. It will be ignored since only the first encountered file is collected. If this is not what you want, make sure every static file has a unique path.

Found another file with the destination path ‘admin/js/change_form.js’. It will be ignored since only the first encountered file is collected. If this is not what you want, make sure every static file has a unique path.

Found another file with the destination path ‘admin/js/popup_response.js’. It will be ignored since only the first encountered file is collected. If this is not what you want, make sure every static file has a unique path.

Found another file with the destination path ‘admin/js/autocomplete.js’. It will be ignored since only the first encountered file is collected. If this is not what you want, make sure every static file has a unique path.

Found another file with the destination path ‘admin/js/SelectBox.js’. It will be ignored since only the first encountered file is collected. If this is not what you want, make sure every static file has a unique path.

Found another file with the destination path ‘admin/js/prepopulate_init.js’. It will be ignored since only the first encountered file is collected. If this is not what you want, make sure every static file has a unique path.

Found another file with the destination path ‘admin/js/cancel.js’. It will be ignored since only the first encountered file is collected. If this is not what you want, make sure every static file has a unique path.

Found another file with the destination path ‘admin/js/nav_sidebar.js’. It will be ignored since only the first encountered file is collected. If this is not what you want, make sure every static file has a unique path.

Found another file with the destination path ‘admin/js/SelectFilter2.js’. It will be ignored since only the first encountered file is collected. If this is not what you want, make sure every static file has a unique path.

Found another file with the destination path ‘admin/js/prepopulate.js’. It will be ignored since only the first encountered file is collected. If this is not what you want, make sure every static file has a unique path.

Found another file with the destination path ‘admin/js/collapse.js’. It will be ignored since only the first encountered file is collected. If this is not what you want, make sure every static file has a unique path.

Found another file with the destination path ‘admin/js/vendor/select2/select2.full.min.js’. It will be ignored since only the first encountered file is collected. If this is not what you want, make sure every static file has a unique path.

Found another file with the destination path ‘admin/js/vendor/select2/LICENSE.md’. It will be ignored since only the first encountered file is collected. If this is not what you want, make sure every static file has a unique path.

Found another file with the destination path ‘admin/js/vendor/select2/select2.full.js’. It will be ignored since only the first encountered file is collected. If this is not what you want, make sure every static file has a unique path.

Found another file with the destination path ‘admin/js/vendor/select2/i18n/fi.js’. It will be ignored since only the first encountered file is collected. If this is not what you want, make sure every static file has a unique path.

Found another file with the destination path ‘admin/js/vendor/select2/i18n/hsb.js’. It will be ignored since only the first encountered file is collected. If this is not what you want, make sure every static file has a unique path.

Found another file with the destination path ‘admin/js/vendor/select2/i18n/th.js’. It will be ignored since only the first encountered file is collected. If this is not what you want, make sure every static file has a unique path.

Found another file with the destination path ‘admin/js/vendor/select2/i18n/et.js’. It will be ignored since only the first encountered file is collected. If this is not what you want, make sure every static file has a unique path.

Found another file with the destination path ‘admin/js/vendor/select2/i18n/bg.js’. It will be ignored since only the first encountered file is collected. If this is not what you want, make sure every static file has a unique path.

Found another file with the destination path ‘admin/js/vendor/select2/i18n/gl.js’. It will be ignored since only the first encountered file is collected. If this is not what you want, make sure every static file has a unique path.

Found another file with the destination path ‘admin/js/vendor/select2/i18n/az.js’. It will be ignored since only the first encountered file is collected. If this is not what you want, make sure every static file has a unique path.

Found another file with the destination path ‘admin/js/vendor/select2/i18n/ro.js’. It will be ignored since only the first encountered file is collected. If this is not what you want, make sure every static file has a unique path.

Found another file with the destination path ‘admin/js/vendor/select2/i18n/ja.js’. It will be ignored since only the first encountered file is collected. If this is not what you want, make sure every static file has a unique path.

Found another file with the destination path ‘admin/js/vendor/select2/i18n/sr-Cyrl.js’. It will be ignored since only the first encountered file is collected. If this is not what you want, make sure every static file has a unique path.

Found another file with the destination path ‘admin/js/vendor/select2/i18n/el.js’. It will be ignored since only the first encountered file is collected. If this is not what you want, make sure every static file has a unique path.

Found another file with the destination path ‘admin/js/vendor/select2/i18n/pl.js’. It will be ignored since only the first encountered file is collected. If this is not what you want, make sure every static file has a unique path.

Found another file with the destination path ‘admin/js/vendor/select2/i18n/sv.js’. It will be ignored since only the first encountered file is collected. If this is not what you want, make sure every static file has a unique path.

Found another file with the destination path ‘admin/js/vendor/select2/i18n/nl.js’. It will be ignored since only the first encountered file is collected. If this is not what you want, make sure every static file has a unique path.

Found another file with the destination path ‘admin/js/vendor/select2/i18n/lv.js’. It will be ignored since only the first encountered file is collected. If this is not what you want, make sure every static file has a unique path.

Found another file with the destination path ‘admin/js/vendor/select2/i18n/uk.js’. It will be ignored since only the first encountered file is collected. If this is not what you want, make sure every static file has a unique path.

Found another file with the destination path ‘admin/js/vendor/select2/i18n/bn.js’. It will be ignored since only the first encountered file is collected. If this is not what you want, make sure every static file has a unique path.

Found another file with the destination path ‘admin/js/vendor/select2/i18n/is.js’. It will be ignored since only the first encountered file is collected. If this is not what you want, make sure every static file has a unique path.

Found another file with the destination path ‘admin/js/vendor/select2/i18n/fa.js’. It will be ignored since only the first encountered file is collected. If this is not what you want, make sure every static file has a unique path.

Found another file with the destination path ‘admin/js/vendor/select2/i18n/nb.js’. It will be ignored since only the first encountered file is collected. If this is not what you want, make sure every static file has a unique path.

Found another file with the destination path ‘admin/js/vendor/select2/i18n/tr.js’. It will be ignored since only the first encountered file is collected. If this is not what you want, make sure every static file has a unique path.

Found another file with the destination path ‘admin/js/vendor/select2/i18n/ca.js’. It will be ignored since only the first encountered file is collected. If this is not what you want, make sure every static file has a unique path.

Found another file with the destination path ‘admin/js/vendor/select2/i18n/sl.js’. It will be ignored since only the first encountered file is collected. If this is not what you want, make sure every static file has a unique path.

Found another file with the destination path ‘admin/js/vendor/select2/i18n/da.js’. It will be ignored since only the first encountered file is collected. If this is not what you want, make sure every static file has a unique path.

Found another file with the destination path ‘admin/js/vendor/select2/i18n/hr.js’. It will be ignored since only the first encountered file is collected. If this is not what you want, make sure every static file has a unique path.

Found another file with the destination path ‘admin/js/vendor/select2/i18n/fr.js’. It will be ignored since only the first encountered file is collected. If this is not what you want, make sure every static file has a unique path.

Found another file with the destination path ‘admin/js/vendor/select2/i18n/id.js’. It will be ignored since only the first encountered file is collected. If this is not what you want, make sure every static file has a unique path.

Found another file with the destination path ‘admin/js/vendor/select2/i18n/sr.js’. It will be ignored since only the first encountered file is collected. If this is not what you want, make sure every static file has a unique path.

Found another file with the destination path ‘admin/js/vendor/select2/i18n/hu.js’. It will be ignored since only the first encountered file is collected. If this is not what you want, make sure every static file has a unique path.

Found another file with the destination path ‘admin/js/vendor/select2/i18n/it.js’. It will be ignored since only the first encountered file is collected. If this is not what you want, make sure every static file has a unique path.

Found another file with the destination path ‘admin/js/vendor/select2/i18n/zh-CN.js’. It will be ignored since only the first encountered file is collected. If this is not what you want, make sure every static file has a unique path.

Found another file with the destination path ‘admin/js/vendor/select2/i18n/tk.js’. It will be ignored since only the first encountered file is collected. If this is not what you want, make sure every static file has a unique path.

Found another file with the destination path ‘admin/js/vendor/select2/i18n/km.js’. It will be ignored since only the first encountered file is collected. If this is not what you want, make sure every static file has a unique path.

Found another file with the destination path ‘admin/js/vendor/select2/i18n/sk.js’. It will be ignored since only the first encountered file is collected. If this is not what you want, make sure every static file has a unique path.

Found another file with the destination path ‘admin/js/vendor/select2/i18n/cs.js’. It will be ignored since only the first encountered file is collected. If this is not what you want, make sure every static file has a unique path.

Found another file with the destination path ‘admin/js/vendor/select2/i18n/de.js’. It will be ignored since only the first encountered file is collected. If this is not what you want, make sure every static file has a unique path.

Found another file with the destination path ‘admin/js/vendor/select2/i18n/zh-TW.js’. It will be ignored since only the first encountered file is collected. If this is not what you want, make sure every static file has a unique path.

Found another file with the destination path ‘admin/js/vendor/select2/i18n/dsb.js’. It will be ignored since only the first encountered file is collected. If this is not what you want, make sure every static file has a unique path.

Found another file with the destination path ‘admin/js/vendor/select2/i18n/pt-BR.js’. It will be ignored since only the first encountered file is collected. If this is not what you want, make sure every static file has a unique path.

Found another file with the destination path ‘admin/js/vendor/select2/i18n/bs.js’. It will be ignored since only the first encountered file is collected. If this is not what you want, make sure every static file has a unique path.

Found another file with the destination path ‘admin/js/vendor/select2/i18n/mk.js’. It will be ignored since only the first encountered file is collected. If this is not what you want, make sure every static file has a unique path.

Found another file with the destination path ‘admin/js/vendor/select2/i18n/ko.js’. It will be ignored since only the first encountered file is collected. If this is not what you want, make sure every static file has a unique path.

Found another file with the destination path ‘admin/js/vendor/select2/i18n/ar.js’. It will be ignored since only the first encountered file is collected. If this is not what you want, make sure every static file has a unique path.

Found another file with the destination path ‘admin/js/vendor/select2/i18n/ps.js’. It will be ignored since only the first encountered file is collected. If this is not what you want, make sure every static file has a unique path.

Found another file with the destination path ‘admin/js/vendor/select2/i18n/hy.js’. It will be ignored since only the first encountered file is collected. If this is not what you want, make sure every static file has a unique path.

Found another file with the destination path ‘admin/js/vendor/select2/i18n/es.js’. It will be ignored since only the first encountered file is collected. If this is not what you want, make sure every static file has a unique path.

Found another file with the destination path ‘admin/js/vendor/select2/i18n/ka.js’. It will be ignored since only the first encountered file is collected. If this is not what you want, make sure every static file has a unique path.

Found another file with the destination path ‘admin/js/vendor/select2/i18n/he.js’. It will be ignored since only the first encountered file is collected. If this is not what you want, make sure every static file has a unique path.

Found another file with the destination path ‘admin/js/vendor/select2/i18n/af.js’. It will be ignored since only the first encountered file is collected. If this is not what you want, make sure every static file has a unique path.

Found another file with the destination path ‘admin/js/vendor/select2/i18n/sq.js’. It will be ignored since only the first encountered file is collected. If this is not what you want, make sure every static file has a unique path.

Found another file with the destination path ‘admin/js/vendor/select2/i18n/en.js’. It will be ignored since only the first encountered file is collected. If this is not what you want, make sure every static file has a unique path.

Found another file with the destination path ‘admin/js/vendor/select2/i18n/hi.js’. It will be ignored since only the first encountered file is collected. If this is not what you want, make sure every static file has a unique path.

Found another file with the destination path ‘admin/js/vendor/select2/i18n/pt.js’. It will be ignored since only the first encountered file is collected. If this is not what you want, make sure every static file has a unique path.

Found another file with the destination path ‘admin/js/vendor/select2/i18n/ne.js’. It will be ignored since only the first encountered file is collected. If this is not what you want, make sure every static file has a unique path.

Found another file with the destination path ‘admin/js/vendor/select2/i18n/ms.js’. It will be ignored since only the first encountered file is collected. If this is not what you want, make sure every static file has a unique path.

Found another file with the destination path ‘admin/js/vendor/select2/i18n/lt.js’. It will be ignored since only the first encountered file is collected. If this is not what you want, make sure every static file has a unique path.

Found another file with the destination path ‘admin/js/vendor/select2/i18n/ru.js’. It will be ignored since only the first encountered file is collected. If this is not what you want, make sure every static file has a unique path.

Found another file with the destination path ‘admin/js/vendor/select2/i18n/eu.js’. It will be ignored since only the first encountered file is collected. If this is not what you want, make sure every static file has a unique path.

Found another file with the destination path ‘admin/js/vendor/select2/i18n/vi.js’. It will be ignored since only the first encountered file is collected. If this is not what you want, make sure every static file has a unique path.

Found another file with the destination path ‘admin/js/vendor/xregexp/xregexp.js’. It will be ignored since only the first encountered file is collected. If this is not what you want, make sure every static file has a unique path.

Found another file with the destination path ‘admin/js/vendor/xregexp/LICENSE.txt’. It will be ignored since only the first encountered file is collected. If this is not what you want, make sure every static file has a unique path.

Found another file with the destination path ‘admin/js/vendor/xregexp/xregexp.min.js’. It will be ignored since only the first encountered file is collected. If this is not what you want, make sure every static file has a unique path.

Found another file with the destination path ‘admin/js/vendor/jquery/jquery.min.js’. It will be ignored since only the first encountered file is collected. If this is not what you want, make sure every static file has a unique path.

Found another file with the destination path ‘admin/js/vendor/jquery/jquery.js’. It will be ignored since only the first encountered file is collected. If this is not what you want, make sure every static file has a unique path.

Found another file with the destination path ‘admin/js/vendor/jquery/LICENSE.txt’. It will be ignored since only the first encountered file is collected. If this is not what you want, make sure every static file has a unique path.

Found another file with the destination path ‘admin/js/admin/DateTimeShortcuts.js’. It will be ignored since only the first encountered file is collected. If this is not what you want, make sure every static file has a unique path.

Found another file with the destination path ‘admin/js/admin/RelatedObjectLookups.js’. It will be ignored since only the first encountered file is collected. If this is not what you want, make sure every static file has a unique path.

Found another file with the destination path ‘admin/img/icon-no.svg’. It will be ignored since only the first encountered file is collected. If this is not what you want, make sure every static file has a unique path.

Found another file with the destination path ‘admin/img/icon-yes.svg’. It will be ignored since only the first encountered file is collected. If this is not what you want, make sure every static file has a unique path.

Found another file with the destination path ‘admin/img/icon-deletelink.svg’. It will be ignored since only the first encountered file is collected. If this is not what you want, make sure every static file has a unique path.

Found another file with the destination path ‘admin/img/icon-unknown-alt.svg’. It will be ignored since only the first encountered file is collected. If this is not what you want, make sure every static file has a unique path.

Found another file with the destination path ‘admin/img/icon-addlink.svg’. It will be ignored since only the first encountered file is collected. If this is not what you want, make sure every static file has a unique path.

Found another file with the destination path ‘admin/img/tooltag-add.svg’. It will be ignored since only the first encountered file is collected. If this is not what you want, make sure every static file has a unique path.

Found another file with the destination path ‘admin/img/icon-calendar.svg’. It will be ignored since only the first encountered file is collected. If this is not what you want, make sure every static file has a unique path.

Found another file with the destination path ‘admin/img/tooltag-arrowright.svg’. It will be ignored since only the first encountered file is collected. If this is not what you want, make sure every static file has a unique path.

Found another file with the destination path ‘admin/img/icon-unknown.svg’. It will be ignored since only the first encountered file is collected. If this is not what you want, make sure every static file has a unique path.

Found another file with the destination path ‘admin/img/icon-alert.svg’. It will be ignored since only the first encountered file is collected. If this is not what you want, make sure every static file has a unique path.

Found another file with the destination path ‘admin/img/README.txt’. It will be ignored since only the first encountered file is collected. If this is not what you want, make sure every static file has a unique path.

Found another file with the destination path ‘admin/img/inline-delete.svg’. It will be ignored since only the first encountered file is collected. If this is not what you want, make sure every static file has a unique path.

Found another file with the destination path ‘admin/img/LICENSE’. It will be ignored since only the first encountered file is collected. If this is not what you want, make sure every static file has a unique path.

Found another file with the destination path ‘admin/img/icon-clock.svg’. It will be ignored since only the first encountered file is collected. If this is not what you want, make sure every static file has a unique path.

Found another file with the destination path ‘admin/img/selector-icons.svg’. It will be ignored since only the first encountered file is collected. If this is not what you want, make sure every static file has a unique path.

Found another file with the destination path ‘admin/img/sorting-icons.svg’. It will be ignored since only the first encountered file is collected. If this is not what you want, make sure every static file has a unique path.

Found another file with the destination path ‘admin/img/search.svg’. It will be ignored since only the first encountered file is collected. If this is not what you want, make sure every static file has a unique path.

Found another file with the destination path ‘admin/img/icon-changelink.svg’. It will be ignored since only the first encountered file is collected. If this is not what you want, make sure every static file has a unique path.

Found another file with the destination path ‘admin/img/calendar-icons.svg’. It will be ignored since only the first encountered file is collected. If this is not what you want, make sure every static file has a unique path.

Found another file with the destination path ‘admin/img/icon-viewlink.svg’. It will be ignored since only the first encountered file is collected. If this is not what you want, make sure every static file has a unique path.

Found another file with the destination path ‘admin/img/gis/move_vertex_on.svg’. It will be ignored since only the first encountered file is collected. If this is not what you want, make sure every static file has a unique path.

Found another file with the destination path ‘admin/img/gis/move_vertex_off.svg’. It will be ignored since only the first encountered file is collected. If this is not what you want, make sure every static file has a unique path.

Found another file with the destination path ‘admin/fonts/Roboto-Light-webfont.woff’. It will be ignored since only the first encountered file is collected. If this is not what you want, make sure every static file has a unique path.

Found another file with the destination path ‘admin/fonts/Roboto-Bold-webfont.woff’. It will be ignored since only the first encountered file is collected. If this is not what you want, make sure every static file has a unique path.

Found another file with the destination path ‘admin/fonts/README.txt’. It will be ignored since only the first encountered file is collected. If this is not what you want, make sure every static file has a unique path.

Found another file with the destination path ‘admin/fonts/LICENSE.txt’. It will be ignored since only the first encountered file is collected. If this is not what you want, make sure every static file has a unique path.

Found another file with the destination path ‘admin/fonts/Roboto-Regular-webfont.woff’. It will be ignored since only the first encountered file is collected. If this is not what you want, make sure every static file has a unique path.

0 static files copied to ‘/home/django/django_books/static’, 132 unmodified.

These are only warnings, telling that there are no changes to the files that were copied to the directory.
If you have any static files under your apps i recommend that you try to find them in that directory.

Have you also changed the nginx configuration?
If so, don’t forget to restart nginx. ( service nginx restart )

My media files under app directory ‘books’ are not copied to the static folder after running the collectstatic cmd

I keep getting no images at all.

Where should the media files live in production?

The media files, are not collected during the collectstatic cmd.

The MEDIA_ROOT setting only describes where django will save the media files.
You probably, want to copy all the files that are in the directory: django_books/books/media/ to django_books/media/

After that, change these settings on your settings.py:

To:

MEDIA_URL = "media/"
MEDIA_ROOT = os.path.join(BASE_DIR, "media")

This will tell django to save the media files on django_books/media

Later, point your nginx configuration to this folder

To:

     location /media  {
        alias /home/django/django_books/media/;
    }

I’m going to jump in here with some (what I think) are extremely important points.

  • Media files and static files are two completely different things. While the docs conflate those two topics in some areas, they really should be thought of as being independent topics.

    • Media files are not handled by the collectstatic command. If you’ve got media files in one environment that needs to be moved to a different environment, it’s up to you to move them.
  • Neither static nor media files should reside within your project’s directory tree. It makes handling the permissions for them in a production environment a lot cleaner.

    • The easiest way to make static work is to make it a directory under ‘/var/www/html’ in most environments. (With the corresponding settings changes as well.)

    • Media files should be in yet a different location. That’s more a question of how you want to manage your system. For example, in one project, the directory for media files is a separate file system - an external storage device. (We aren’t consistent - spot-checking a couple of my projects shows I’ve done something different every time.)

2 Likes

I guess with this changes I have also to update my html templates to use the new location, right?

Nope - that’s the beauty of the MEDIA_URL and STATIC_URL settings combined with nginx. Those settings separate the URL from the reference to physical files. Your app works with the “logical” url, and your nginx configuration works with the physical file locations.

(That’s also assuming you’re properly using the {% static %} setting for rendering URLs in your templates.)

1 Like

That’s a good explanation Ken. I’ve got it, thank you.