gpt4 book ai didi

javascript - 即使将 STATICFILES_DIR 设置为正确的路径,Django 静态文件也不会加载

转载 作者:行者123 更新时间:2023-11-28 00:34:11 35 4
gpt4 key购买 nike

我在 Stack overflow 和其他网站上看过很多类似的问题,但我似乎无法弄清楚为什么我的静态文件没有加载。

我已经根据在线教程创建了我的基本模板,它应该像魔术一样工作。但是我的静态文件没有随 View 一起呈现。当我转到 chrome 上的开发人员控制台时,我发现找不到 css 和 js 文件。确切的错误是:

> 127.0.0.1/:8 GET http://127.0.0.1:8000/static/css/bootstrap.min.css net::ERR_ABORTED 404 (Not Found)
> 127.0.0.1/:117 GET http://127.0.0.1:8000/static/js/jquery-3.3.1.min.js net::ERR_ABORTED 404 (Not Found)
> 127.0.0.1/:118 GET http://127.0.0.1:8000/static/js/popper.min.js net::ERR_ABORTED 404 (Not Found)
> 127.0.0.1/:119 GET http://127.0.0.1:8000/static/js/bootstrap.min.js net::ERR_ABORTED 404 (Not Found)

这是我的基本模板:

{% load static from staticfiles %}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>{% block title %}Django Forms{% endblock %}</title>

<link rel="stylesheet" href="{% static 'css/bootstrap.min.css' %}">
{% block stylesheets %}
{% endblock stylesheets %}

</head>
<body>
{% block body %}
<nav class="navbar navbar-expand-sm navbar-dark bg-dark" >
<div class="container" >
<a class="navbar-brand" href="{% url 'home' %}">Blog App</a>
<button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#mainMenu" aria-controls="mainMenu" aria-expanded="false" aria-label="Toggle navigation" >
<span class="navbar-toggler-icon" ></span>
</button>
<div class="collapse navbar-collapse" id="mainMenu" >
<ul class="navbar-nav ml-auto" >

<li class="nav-item dropdown" >
<a class="nav-link dropdown-toggle" href="#" id="userMenu" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
{{user.username}}
</a>
<div class="dropdown-menu dropdown-menu-right"aria-labelledby="userMenu">
<a class="dropdown-item" href="#">My account</a>
<a class="dropdown-item" href="#" >Change password</a>
<div class="dropdown-divider"></div>
<a class="dropdown-item" href="{% url 'logout' %}">Log out</a>
</div>
</li>
</ul>

</div>
</div>
</nav>

<div class="container" >
<ol class = "breadcrumb my-4">
{% block breadcrumb %}
{% endblock %}
</ol>

{% block content %}
{% endblock %}
</div>

{% endblock body %}

<script type="text/javascript" src="{% static 'js/jquery-3.3.1.min.js' %}" ></script>
<script type="text/javascript" src="{% static 'js/popper.min.js' %}" ></script>
<script type="text/javascript" src="{% static 'js/bootstrap.min.js' %}" ></script>
</body>
</html>

这是我的 Settings.py

BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))

INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',

'widget_tweaks',

'accounts',
'blog',


]
STATIC_ROOT = os.path.join(BASE_DIR, 'static_root')

STATICFILES_DIR = (
os.path.join(BASE_DIR, 'static'),
# os.path.join(BASE_DIR,'myformapp', 'static','js')
)

STATIC_URL = '/static/'

STATICFILES_FINDERS = (
'django.contrib.staticfiles.finders.FileSystemFinder',
'django.contrib.staticfiles.finders.AppDirectoriesFinder',
)

我的文件夹结构:

Formproject
|___________myformapp
|___________accounts
|___________blog
|___________myformapp
|__________templates
|__________includes
|__________base.html
|__________other_templates.html
|_____________init__.py
|___________settings.py
|___________urls.py
____________static
____________static_root

最佳答案

您的 templatesstatic 文件夹不在同一根目录下,这可能是问题所在。无论如何,尝试将其添加到元素的 settings.py 中:

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

然后,在 HTML 中使用您的 CSS 文件:

{% load static %}
<link rel="stylesheet" type="text/css" href="{% static 'css/style.css'%}">

这应该可以正常工作。

关于javascript - 即使将 STATICFILES_DIR 设置为正确的路径,Django 静态文件也不会加载,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54264600/

35 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com