【问题标题】:CSS file not affecting page in DjangoCSS文件不影响Django中的页面
【发布时间】:2021-04-04 16:34:24
【问题描述】:

我在 html 和 css 中创建了一个导航栏,并希望将其实现到页面中。 HTML 工作正常,但 CSS 根本不影响 HTML。我已经正确设置了我的静态文件,并使用了大量设置来尝试让它工作。任何帮助都会很棒。我对 HTML 和 CSS 很陌生,所以如果我犯了任何错误,请突出显示它们。谢谢

settings.py

"""
Django settings for completestudent project.

Generated by 'django-admin startproject' using Django 3.1.4.

For more information on this file, see
https://docs.djangoproject.com/en/3.1/topics/settings/

For the full list of settings and their values, see
https://docs.djangoproject.com/en/3.1/ref/settings/
"""

from pathlib import Path
import os

# Build paths inside the project like this: BASE_DIR / 'subdir'.
BASE_DIR = Path(__file__).resolve().parent.parent


# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/3.1/howto/deployment/checklist/

# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = 'siv&qzku5zi8b!2d=%0@z2i34eje)$-t#ezbdot1-e9^zahgg@'

# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True

ALLOWED_HOSTS = []


# Application definition

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


#own apps
'expendetures',
'pages'

]

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 = 'completestudent.urls'

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [os.path.join(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 = 'completestudent.wsgi.application'


# Database
# https://docs.djangoproject.com/en/3.1/ref/settings/#databases

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.sqlite3',
        'NAME': BASE_DIR / 'db.sqlite3',
    }
}


# Password validation
# https://docs.djangoproject.com/en/3.1/ref/settings/#auth-password-validators

AUTH_PASSWORD_VALIDATORS = [
    {
        'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator',
    },
    {
        'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator',
    },
    {
        'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator',
    },
    {
        'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator',
    },
]


# Internationalization
# https://docs.djangoproject.com/en/3.1/topics/i18n/

LANGUAGE_CODE = 'en-us'

TIME_ZONE = 'UTC'

USE_I18N = True

USE_L10N = True

USE_TZ = True


# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/3.1/howto/static-files/

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

STATIC_URL = '/static/'

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

navbar.html

{% load static %}
<!DOCTYPE html>
<html>
<head>
<link rel = ”stylesheet” type = "text/css" href =  "{% static 'css/navbar.css' %}">
</head>
<div class="topnav">
<a class="active" href="{% url 'home' %}">Home</a>
<a href="{% url 'dashboard' %}">Dashboard</a>
<a href="{% url 'contact' %}">Contact</a>
<a href="{% url 'about' %}">About</a>
</div>
</html>

navbar.css

.topnav{
background-color: #8a2be2;
overflow: hidden;
}


.topnav a {
    float: left;
    color: #ffffff;
    text-align: center;
    padding: 14px 16px;
    text-decoration: none;
    font-size: 17px;
}

.topnav a:hover{
    background-color: #ffffff;
    color: #8a2be2;
}

.topnav a.active {
    background-color: #ffffff;
    color: #8a2be2;
}

【问题讨论】:

  • 你是在服务器上运行它还是只在本地主机上运行?
  • @AlexanderFreyr 我在本地主机上运行它
  • 如果我理解正确,css文件在static/css/navbar.css下?
  • @AlexanderFreyr 是的,它在那个文件夹中,但出于某种原因,Django 将静态文件复制到 static/css 目录和静态目录中
  • 您是否尝试过在浏览器中检查开发者控制台以查看加载文件时是否有任何错误?

标签: html css python-3.x django django-3.0


【解决方案1】:

尝试从您的 settings.py 中删除 STATIC_ROOT 并只使用 STATICFILES_DIRS,所以它看起来有点像这样。

# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/3.1/howto/static-files/

STATIC_URL = '/static/'

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

STATIC_ROOT的文档规定,为了方便制作,应该是一个空文件夹。因此,除非您要部署到服务器,否则您不需要使用它。

【讨论】:

  • 我已经尝试了你所说的 settings.py 但现在我在终端中使用 collectstatic 时出现错误说:“raise ImproperlyConfigured("You're using the staticfiles app" django.core. exceptions.ImproperlyConfigured:您正在使用 staticfiles 应用程序而没有将 STATIC_ROOT 设置设置为文件系统路径"
  • 如果您尝试在没有manage.py collectstatic 的情况下启动 Django,您的问题是否仍然存在?
  • 是的。我遇到的问题是我有一个名为“pages”的应用程序,并且在应用程序目录中我有静态文件。但是,当我确实使用 collectstatic 时,它会创建静态文件夹,但如果有意义的话,它不会将其放入“页面”文件夹中
  • 我也将 CSS 代码放入 HTML 文件中,它可以工作,但我宁愿有一个外部 CSS 代码,所以 django 如何访问静态文件是个问题
【解决方案2】:

导航的目录应该是

/path/to/myproject/myapp/static/css/navbar.css

如果你运行过python manage.py collectstatic,目录应该是

/path/to/myproject/static/myapp/css/navbar.css

【讨论】:

  • 我已经检查了路径,它们与您所说的相同
  • 第二个应该是/path/to/myproject/static/myapp/css/navbar.css
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-02-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-03-22
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多