python - 找不到 Django style.css
问题描述
Error :Not Found: /{ % static 'css/style.css' % }
"GET /%7B%20%%20static%20'css/style.css'%20%%20%7D HTTP/1.1" 404 2378
当我想将我的style.css链接到 Django 项目时,它会引发错误。
html:
<!DOCTYPE html>
{% load static %}
<html>
<head>
<meta charset="utf-8">
<title>Have a Good Day</title>
<link rel="stylesheet" href="{ % static 'css/style.css' % }" >
</head>
<body>
<h1 class="ok">Great it works</h1>
</body>
</html>
设置.py:
STATIC_URL = '/static/'
STATICFILES_DIRS = [
os.path.join(BASE_DIR, 'static'),
]
笔记 :DEBUG=True
文件夹结构:
newproject (Folder name)
- app1(This is my app name)
-migrations
-static
-css
-style.css
-templates
-init__.py
-admin.py
-apps.py
-models.py
-tests.py
-urls.py
-views.py
它只是为了发展。不是部署。
解决方案
设置.py
STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, 'static')
网址.py
from django.urls import path, include
from django.conf import settings
from django.conf.urls.static import static
urlpatterns = [....]
urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
清单 在生产中,您必须运行python manage.py collectstatic
并添加正确的文件夹权限。
如何调试?
检查浏览器中的链接,如果您觉得有任何问题,请尝试手动更改路径
推荐阅读
- python - Python 3,Terrabyte 文本文件的负 seek() 问题,
- javascript - 你如何让赛普拉斯选择列表中的某个图标?
- assembly - 为什么我不能将 64 位值移动到 64 位寄存器?
- docker - 如何在包括 localhost 机器的 docker 容器之间进行通信
- ios - 找不到支持键盘 iPhone-PortraitTruffle-DecimalPad 的 type 8 的键盘;
- html - 为什么空白:pre; 打破表格内的表格html?
- variables - 如何将地图的块地图转换为 Terraform 中的变量
- mysql - 简单插入记录时截断不正确的 DOUBLE 值
- php - 无需使用表单/POST 即可将信息传递给支付提供商的安全替代方式
- r - 文本分类,rnn包R