Tyler Durden
Tyler Durden

Reputation: 59

Django admin wont load certain static files

I have a strange issue that's occurred on several django projects and I'm trying to figure out a fix for it. For some reason, all the static files for the admin area load properly including the js, css, and images but 2 files for the side nav bar (which are in my static directory along with everything else) wont load. The files are the nav_sidebar.css and nav_sidebar.js.

I've figured out a work around and added these inline in the admins base.html template and deleted the links to these files. This works but it's kind of ridiculous that it manages to load all other static assets fine but not these particular files. I have my static root and directories set up properly, have nginx pointing to the correct static directory, and have done collect static and restarted the server. Everything I could possibly think of but it doesn't work.

Considering this has happened on 3 straight projects, I think this is some kind of bug rather than an error on my end.

Upvotes: 2

Views: 1843

Answers (2)

Tyler Durden
Tyler Durden

Reputation: 59

So I was able to answer my own question in the end and for anyone else that comes across this problem here are the steps I took.

For the admin static files, before you run collect static are being sourced directly from the wherever your python(versionNumber)/site-packages/django/contrib/admin/static is located. Unless you run collect static, or manually copy and paste them into your static files directory, these admin files wont be there.

Now I'm sure this is basic knowledge for any django user and I even did this and the error still persisted. What I found, is that I set my static urls up like:

STATIC_URL = '/static/'

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

And for some reason, django was still sourcing the admin files from that same directory the django source is located. So I simply changed the url path and static root to:

STATIC_URL = '/staticfiles/'

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

And ran manage.py collectstatic again and that fixed my admin area and everything is working properly now.

Upvotes: 2

ha-neul
ha-neul

Reputation: 3248

These two files are introduced in django 3.1 for nav_sidebar feature. In admin/base.html, it says:

{% if not is_popup and is_nav_sidebar_enabled %}
  <link rel="stylesheet" type="text/css" href="{% static "admin/css/nav_sidebar.css" %}">
  <script src="{% static 'admin/js/nav_sidebar.js' %}" defer></script>
{% endif %}

is_nav_sidebar_enabled by default is enabled. Did you put something in the root urls.py to disable that?

something like: admin.site.enable_nav_sidebar = False in your root urls.py?

The doc about the new nav_sidebar feature is here

Upvotes: 1

Related Questions