无法在我的django项目中链接我的css文件

lrl1mhuk  于 2022-12-14  发布在  Go
关注(0)|答案(4)|浏览(196)

index.html

{% load static %}
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <link rel="stylesheet" href="{% static 'style.css' %}">
    <title>Document</title>
</head>
<body>
    <h1 class="heading">This is the Heading</h1>
</body>
</html>

settings.py (my应用程序名称为基础)

from pathlib import Path

BASE_DIR = Path(__file__).resolve().parent.parent
DEBUG = True
ALLOWED_HOSTS = []

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'base'
]

MIDDLEWARE = [
    'django.middleware.security.SecurityMiddleware',
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.common.CommonMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
    'django.middleware.clickjacking.XFrameOptionsMiddleware',
]

ROOT_URLCONF = 'testpro.urls'

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [BASE_DIR, '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',
            ],
        },
    },
]

WSGI_APPLICATION = 'testpro.wsgi.application'

...
STATIC_URL = 'static/'
STATICFILES_DIRS = BASE_DIR, 'static'

文件夹结构是这样的:

project folder
    /static
       /style.css

CSS包含一个简单的颜色变化,它是不工作的。
我试过用不同的方式链接css文件,但似乎都不起作用。我不知道为什么,有时它会突然起作用,然后突然停止工作。

8ulbf1ek

8ulbf1ek1#

而不是这样:

STATIC_URL = 'static/'
STATICFILES_DIRS = BASE_DIR, 'static'

试试这个:

STATIC_URL = '/static/'

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

dojqjjoe2#

不需要在模板数组中指定基目录

DIRS': [BASE_DIR, 'templates'],

就让它空着吧。

DIRS': [],

和所有其他看起来都很好,只是要确保CSS文件。

zdwk9cvp

zdwk9cvp3#

将此添加到

<head>
<link href="{%static 'style.css'%}" rel="stylesheet"> 

</head>

并将其添加到testpro(urls.py)

from testpro import settings

urlpatterns = [
// here goes your urls 
]
//at the bottom add this
urlpatterns += static(settings.STATIC_URL , document_root=settings.STATICFILES_DIRS)

并在www.example.com中settings.py更改

STATICFILES_DIRS= BASE_DIR, 'static' to
STATIC_ROOT = os.path.join(BASE_DIR,'static')
quhf5bfb

quhf5bfb4#

settings.py的这一部分更改为如下所示:

STATIC_URL = 'static/'
STATICFILES_DIRS = (os.path.join(BASE_DIR, '<Your APP name goes here>\static'),)

如果您的静态文件夹中有一个'css'子文件夹,那么您需要将其添加到HTML模板中的相对路径,如下所示:

{% load static %}
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <link rel="stylesheet" href="{% static 'css/style.css' %}">
    <title>Document</title>
</head>
<body>
    <h1 class="heading">This is the Heading</h1>
</body>
</html>

我希望这对你有帮助!

相关问题