Tried to work on a project (on LAN)
When you try to run through python manage.py runserver 0.0.0.0:8000 - everything works
But as soon as I try to uWSGI using the uwsgi --http :8005 --module main.wsgi command uwsgi --http :8005 --module main.wsgi to run the project, I get the following errors in the developer's browser console:


The problem is to connect css and js, although through the "normal method" everything works
I used python manage.py collectstatic - and it collected (because I set it in the settings) in the static folder, which had all the static files
What can this problem be connected with and how can it be solved?

  • 2
    Django will not distribute static outside the developer’s environment. You need to publish the static folder in some other way, for example via nginx - andreymal
  • the funny thing is that I always delay the filing of a question on an overflash stack, and when I do, I reach the solution in 10 minutes (tested on the last 3 questions), but anyway - thanks) - nrjshka
  • So share the solution, since it is there. If you already asked a question .. :) Someone useful. - Mikhail Alekseevich
  • @ MikhailAlekseevich andreymal already answered it. The problem is that uWSGI does not see the static and needs to transfer it to it via nginx. - nrjshka
  • I will issue a full answer probably later - andreymal

1 answer 1

Official Django documentation declares ( translation ):

In addition to the configuration, you must configure the distribution of static files.

When developing, if you use django.contrib.staticfiles , this all happens automatically via the runserver , with DEBUG to True .

This presentation is very inefficient and perhaps unsafe . It is intended only for development, and should not be used on a combat server .

Accordingly, Django is not going to distribute static files through uwsgi (although it can be forced to do this, but I will not tell you how, because I don’t need to).

In general, about the distribution of static files on the production, there is also a page in the documentation ( translation ).

In most cases, nginx is put in front of janga (and indeed in any other scripts) (even if there is apache2, nginx is also often placed before it). And in the configuration, nginx prescribe that for links to static files you need to pick them up from such a directory, and transfer everything else to Django. I will not write here a whole manual on configuring nginx (this is easily googled), but a typical config looks like this:

 server { server_name example.com; # listen, ssl_certificate и прочие параметры тут # Статические файлы забирает сам nginx location /static/ { alias /path/to/your/static/; # Можно управлять кэшем браузера с помощью параметра expires } location /media/ { alias /path/to/your/media/; expires 30d; # предлагаем браузеру кэшировать файлы из /media/ месяц } # Всё остальное передаём в Django, на порту 8000 запущен uwsgi location { proxy_pass http://127.0.0.1:8000; include proxy_params; } } 

If you need the IP address of the user (client), then note that the client for Django in this case is nginx with the IP address 127.0.0.1, and you will have to find out the user's real IP bypass .