我在Stack Overflow和其他网站上看过很多类似的问题,但是我似乎无法弄清楚为什么我的静态文件没有加载。
我已经基于在线教程创建了基本模板,它应该像魔术一样工作。但是我的静态文件没有与视图一起渲染。当我转到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
答案 0 :(得分:0)
您的 templates 和 static 文件夹不在同一根目录下,这可能是问题所在。无论如何,请尝试将其添加到项目的 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'%}">
这应该很好。
答案 1 :(得分:0)
我发现这是我的基本错误。
在模板上,我按如下方式完成了<link>
标签
<link rel = "stylesheets" src="file source here">
应该已经
<link rel = "stylesheets" href="file source here">
这样的新手错误。