繁体   English   中英

找不到 Django style.css

[英]Django style.css is not found

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

它仅用于开发。 不是部署。

您是否运行过python manage.py collectstatic命令?

设置.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并添加正确的文件夹权限。

如何调试?

检查浏览器中的链接,如果您觉得有任何问题,请尝试手动更改路径

删除标签中的空格<link rel="stylesheet" href="{ % static 'css/style.css' % }" >

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM