Python – Django doesn’t find CSS files for admin pages using uWSGI

djangopythonstatic-contentuwsgi

Even if I followed the official instructions, when I start a Django test site using uWSGI, CSS files for the admin interface are not loaded. If I open the URL of a CSS file, for example http://localhost:8443/static/admin/css/base.css, I get a 404 error. I searched for the local file and I guess its path is /usr/local/lib/python3.3/dist-packages/django/contrib/admin/static/admin/css/base.css; so I also tried to run uwsgi as root, but nothing changed.

I have no problem using python3 manage.py runserver. If I open http://localhost:8000/static/admin/css/base.css, the file is loaded in the browser, and the style is applied to the admin page.

This is the command I execute in bash:

uwsgi --ini ~/.uwsgi/conf/django.ini --set-placeholder project_name=mysite --set-placeholder port=8443

and this is the content of django.ini:

[uwsgi]
module = %(project_name).wsgi:application
https = :%(port),/usr/local/nginx/conf/server.crt,/usr/local/nginx/conf/server.key,HIGH
strict = true
chdir = /home/marco/django-projects/%(project_name)
env = DJANGO_SETTINGS_MODULE=%(project_name).settings
socket = /home/marco/.uwsgi/%(project_name).socket
pidfile = /home/marco/.uwsgi/%(project_name).pid
daemonize = /home/marco/.uwsgi/%(project_name).log
master = true
enable-threads = true
harakiri = 20
max-requests = 5000
vacuum = true

Best Answer

Official deployment docs (independently by the WSGI server) do not cover serving static files (that is generally managed by the webserver). The right docs are here:

https://docs.djangoproject.com/en/dev/howto/static-files/deployment/

eventually serving static files is pretty easy with uWSGI:

http://uwsgi-docs.readthedocs.org/en/latest/StaticFiles.html

but if you can do it in nginx it is better

Related Topic