Django负载静力学

2024-05-19 18:19:08 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试将静态文件加载到我的html文件中。索引.html延伸基本.html. 但是,文件不会被读取(至少不会显示在屏幕上,但是终端不会说找不到)

在…的头上索引.html以及基本.html,我有一个标签

{% load static %}

我使用中的标记读取.css文件索引.html地址:

{% static 'base/css/style.css' %}

在设置.py,我有:

TEMPLATES = [
{
    'BACKEND': 'django.template.backends.django.DjangoTemplates',
    'DIRS': ['Grace/templates/',
             'HomePage/templates/',
             'Forum/templates/',
             'CustomUser/templates'],
    'APP_DIRS': True,
    'OPTIONS': {
        'context_processors': [
            'django.template.context_processors.debug',
            'django.template.context_processors.request',
            'django.contrib.auth.context_processors.auth',
            'django.contrib.messages.context_processors.messages',
            'django.template.context_processors.csrf',
            ],
        },
    },
]

我的项目结构如下:

|Grace
|--static
|----base
|------css
|--------boostrap.min.css
|--------style.css
|------templates
|--------base.html

|Homepage
|--templates
|----index
|------index.html

提前谢谢!你知道吗


Tags: 文件djangoauthbasestylehtmlcontextstatic
1条回答
网友
1楼 · 发布于 2024-05-19 18:19:08

请尝试执行以下步骤:

假设这是您的目录:

project_folder
├── app_1
├── app_2
├── static
│   ├── css
│   │     ├── bootstrap.css
│   ├── fonts
│   └── js
├── templates

settings.py文件中,您必须指定名为static的静态文件所在文件夹的路径,在我的示例中:

STATIC_URL = '/static/'

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

而不是在任何html文件中,您可以像这样加载boostrap.css文件:

{% load static %}

<!DOCTYPE html>
<html lang="en" xmlns="http://www.w3.org/1999/html">
<head>

    <meta charset="UTF-8">
    <title></title>
    <link href="{% static 'css/bootstrap.min.css' %}" rel="stylesheet">

</head>

<body>

相关问题 更多 >