gpt4 book ai didi

python - Django - 加载静态数据

转载 作者:行者123 更新时间:2023-12-01 02:25:28 29 4
gpt4 key购买 nike

我正在尝试将静态文件加载到我的 html 文件中。 index.html 扩展了base.html。但是,文件没有被读取(至少没有显示在屏幕上,但是,终端没有说未找到)

在index.html和base.html的头部,我有一个标签

{% load static %}

我通过在index.html中使用标签来读取.css文件:

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

在setting.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

提前致谢!

最佳答案

尝试按照以下步骤操作:

假设这是您的目录:

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>

关于python - Django - 加载静态数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47457279/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com