Ivan
Ivan

Reputation: 95

static files for django admin can't be found while running asgi server

Here is my settings.py

# ...other settings

STATIC_ROOT = os.path.join(BASE_DIR, "static")
STATIC_URL = 'static/'

# ...other settings

Then I run

python manage.py collectstatic

uvicorn Django_Channels.asgi:application --port 8000 --workers 4 --log-level debug --reload

this collects static (after that I do have static folder at the root folder), starts the asgi server, but when I go to the admin page it looks like without any CSS or JS files. Just a bunch of unorganised text.

Here are the logs from terminal:

Here what logs look likeINFO:     
127.0.0.1:51770 - "GET /static/admin/css/dashboard.css HTTP/1.1" 404 Not Found
Not Found: /static/admin/css/responsive.css
INFO:     127.0.0.1:51770 - "GET /static/admin/css/responsive.css HTTP/1.1" 404 Not Found
Not Found: /static/admin/js/theme.js
INFO:     127.0.0.1:51769 - "GET /static/admin/js/theme.js HTTP/1.1" 404 Not Found
Not Found: /static/admin/js/nav_sidebar.js
INFO:     127.0.0.1:51770 - "GET /static/admin/js/nav_sidebar.js HTTP/1.1" 404 Not Found

I am always having a bad time with staticfiles, could please somebody tell me what am I doing wrong?

Upvotes: 0

Views: 796

Answers (2)

Facundo Veronelli
Facundo Veronelli

Reputation: 104

I encountered the same problem with my project. Here’s how you can resolve it:

First, add the STATIC_ROOT setting for static files in your settings.py:


STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles')

After that, run the following command to collect the static files:


python manage.py collectstatic

Finally, add a URL path to urlpatterns:


urlpatterns = [
path("admin/", admin.site.urls),
# your other paths here
] + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)

Upvotes: 2

Jan Papež - honyczek
Jan Papež - honyczek

Reputation: 161

As you can see in related questions and in the django docs, static files can be served when you set DEBUG=True (for your development purposes only). Otherwise you have to include the static files path into your web server configuration.

Serving the files

In addition to these configuration steps, you’ll also need to actually serve the static files.

During development, if you use django.contrib.staticfiles, this will be done automatically by runserver when DEBUG is set to True (see django.contrib.staticfiles.views.serve()).

This method is grossly inefficient and probably insecure, so it is unsuitable for production.

See How to deploy static files for proper strategies to serve static files in production environments.

Upvotes: 2

Related Questions