Django管理员不会加载某些静态文件 [英] Django admin wont load certain static files

查看:91
本文介绍了Django管理员不会加载某些静态文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在django的多个项目中发生了一个奇怪的问题,我正在设法解决该问题.出于某种原因,管理区域的所有静态文件都正确加载,包括js,css和图像,但侧面导航栏的2个文件(以及其他所有文件都在我的静态目录中)不会加载.这些文件是nav_sidebar.css和nav_sidebar.js.

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.

我已经找到解决方法,并将这些内联添加到admins base.html模板中,并删除了指向这些文件的链接.这行得通,但设法加载所有其他静态资产却不加载这些特定文件有点荒谬.我已经正确设置了我的静态根目录和目录,让nginx指向了正确的静态目录,并完成了静态收集并重新启动了服务器.我可能会想到的所有方法,但是都行不通.

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.

考虑到这是在3个直接项目中发生的,我认为这是某种错误,而不是我的错误.

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

推荐答案

所以我最终可以回答自己的问题,对于遇到此问题的其他任何人,这都是我采取的步骤.

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.

对于管理静态文件,在运行收集静态文件之前,它们直接从python(versionNumber)/site-packages/django/contrib/admin/static所在的位置获取.除非您运行收集静态文件,或者手动将它们复制并粘贴到静态文件目录中,否则这些管理文件将不存在.

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.

现在,我确定这是所有django用户的基础知识,我什至做到了,并且错误仍然存​​在.我发现的是,我将自己的静态网址设置为:

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')

由于某种原因,django仍在从django源所在的同一目录中采购管理文件.因此,我只是将url路径和静态根目录更改为:

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')

然后再次运行manage.py collectstatic并修复了我的管理区域,现在一切正常.

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

这篇关于Django管理员不会加载某些静态文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆