![](/img/trans.png)
[英]How to reference a static file in Django that isn't inside my static file directory?
[英]Django - Is My Static File in the Wrong Place?
我正在使用 Django 为天气应用程序创建一个项目,我认为我的 Javascript 文件位于错误的位置。 我把它放在 static 文件夹中。 但我收到控制台错误 GET http://127.0.0.1:8000/static/capstone/capstone.js net::ERR_ABORTED 404 (Not Found)
这是我的项目文件的设置方式。 这个对吗?
在 settings.py 我也有:
STATIC_URL = '/static/'
STATICFILES_DIRS = (
os.path.join(BASE_DIR, 'static'),
)
STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles')
创建一个新文件夹capstone
并将您的 capstone.js 移动到它。
cause by default Django uses a static
folder within an app, so in your case, you're visiting http://localhost:8000/static/capstone/capstone.js but the actual link is http://localhost:8000/static /capstone.js
默认情况下,您的BASE_DIR
指向manage.py
所在的同一目录。如果您没有更改它,那么capstone.js
当前位于
os.path.join(BASE_DIR, 'capstone/static')
但是 django 默认情况下会在每个已安装的应用程序中查找static
文件夹,因此在您的情况下STATCFILES_DIRS
是多余的。
确保您已将 static 文件添加到您的urls.py
中。
from django.conf import settings
from django.conf.urls.static import static
urlpatterns = [
# ... the rest of your URLconf goes here ...
] + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
或者
from django.contrib.staticfiles.urls import staticfiles_urlpatterns
urlpatterns = [
# ...urls
]
# static content urls
urlpatterns += staticfiles_urlpatterns()
这是我对您为什么会收到 404 错误的最佳猜测。
注意:如果您已设置 static 网址,请尝试
http://127.0.0.1:8000/static/capstone.js
代替。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.