Django:如何在django项目中设置静态文件?

问题描述 投票:0回答:1

我在Django中创建了一个简单的项目,但静态文件(CSS)无法使用。

设置.py

STATIC_URL = '/static/'
STATICFILES_DIRS = [
    os.path.join(BASE_DIR, "static"),
]

urls.py

urlpatterns = [
    path('admin/', admin.site.urls),
    path('',views.portfolio),
]+ static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)

HTML文件

{% load static %}
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <link rel="stylesheet" href="{% static 'portfolio.css' %}">
</head>
<body>
    <h1>hello world</h1>
</body>
</html>

项目目录图

picture of the project directory.

blog是应用程序,my_site是一个项目。

python django static-files
1个回答
1
投票

尝试添加

os.path.join(BASE_DIR, "my_site/static")

至STATICFILES_DIRS。

STATICFILES_DIRS = [
    os.path.join(BASE_DIR, "static"),
    os.path.join(BASE_DIR, "my_site/static")
]

摆放 静态 文件夹中的 my_site 夹子

© www.soinside.com 2019 - 2024. All rights reserved.