Django 找不到 JS 静态脚本



我有一个基本的Django应用程序,我正在尝试添加HTML到其中。

我遇到的问题是,由于某种原因,我的降价没有在我的static文件夹中拾取scripts

我对Django相对较新,所以我包括了我认为是相对的东西。

项目根目录中的静态文件夹结构。

-- static
---- css
------ some_css_files
---- images
---- scripts
------ some_scripts

HTML 中的引用脚本

<!DOCTYPE html>
<html dir="ltr" lang="en-US">
<head>
{% load static %}
...
...
<script type="text/javascript" src="{% static 'scripts/jquery.js' %}"></script>
<script type="text/javascript" src="{% static 'scripts/plugins.js' %}"></script>
<script type="text/javascript" src="{% static 'scripts/functions.js' %}"></script>

从Chrome开发工具中,路径看起来是正确的,在根目录中查找static文件夹,但在所有资源上仍然是404。

0.0.0.0/:13 GET http://0.0.0.0:8000/static/css/bootstrap.css 404 (Not Found)
0.0.0.0/:14 GET http://0.0.0.0:8000/static/css/style.css net::ERR_ABORTED 404 (Not Found)
0.0.0.0/:15 GET http://0.0.0.0:8000/static/css/swiper.css net::ERR_ABORTED 404 (Not Found)
0.0.0.0/:16 GET http://0.0.0.0:8000/static/css/dark.css net::ERR_ABORTED 404 (Not Found)
0.0.0.0/:17 GET http://0.0.0.0:8000/static/css/font-icons.css net::ERR_ABORTED 404 (Not Found)
0.0.0.0/:18 GET http://0.0.0.0:8000/static/css/animate.css net::ERR_ABORTED 404 (Not Found)
0.0.0.0/:19 GET http://0.0.0.0:8000/static/css/magnific-popup.css net::ERR_ABORTED 404 (Not Found)
0.0.0.0/:21 GET http://0.0.0.0:8000/static/app/content/responsive.css net::ERR_ABORTED 404 (Not Found)
0.0.0.0/:221 GET http://0.0.0.0:8000/static/scripts/jquery.js net::ERR_ABORTED 404 (Not Found)
0.0.0.0/:222 GET http://0.0.0.0:8000/static/scripts/plugins.js net::ERR_ABORTED 404 (Not Found)
0.0.0.0/:223 GET http://0.0.0.0:8000/static/scripts/functions.js net::ERR_ABORTED 404 (Not Found)

在我的settings.py中,我有以下内容用于静态文件:

STATIC_URL = '/static/'

我配置错误了什么,或者我需要更改哪些内容才能正常获取资源?

更新:

根网址模式

urlpatterns = [
path('admin/', admin.site.urls),
path('', RedirectView.as_view(url='home/', permanent=True)),
path('home/', include('home.urls')),
]

您可以看到所有静态文件都没有加载,包括 css

对于开发,您应该添加其他静态 url模式,如文档中所述

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)

或者如果您使用默认django.contrib.staticfiles

DEBUG=True

像这样更改引用脚本

<script type="text/javascript" src="/static/scripts/jquery.js"></script>
<script type="text/javascript" src="/static/scripts/plugins.js"></script>
<script type="text/javascript" src="/static/scripts/functions.js"></script>

最新更新