我想向 Django 添加一个 css 文件,但找不到静态文件夹。
这是我的 settings.py 文件:
"""
Django settings for opengarden project.
Generated by 'django-admin startproject' using Django 2.2.3.
For more information on this file, see
https://docs.djangoproject.com/en/2.2/topics/settings/
For the full list of settings and their values, see
https://docs.djangoproject.com/en/2.2/ref/settings/
"""
import os
# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/2.2/howto/deployment/checklist/
# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = '__________'
# 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',
]
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 = 'opengarden.urls'
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [os.path.join(BASE_DIR, 'wx', '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 = 'opengarden.wsgi.application'
# Database
# https://docs.djangoproject.com/en/2.2/ref/settings/#databases
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
}
}
# Password validation
# https://docs.djangoproject.com/en/2.2/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/2.2/topics/i18n/
LANGUAGE_CODE = 'en-us'
TIME_ZONE = 'America/Denver'
USE_I18N = True
USE_L10N = True
USE_TZ = True
# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/2.2/howto/static-files/
STATIC_URL = '/static/'
STATIC_ROOT = ''
Run Code Online (Sandbox Code Playgroud)
我的基本 html 文件:
<!DOCTYPE html>
<html lang="en">
<!DOCTYPE html>
<html lang="en">
<head>
{% block title %}<title>Garden Dash</title>{% endblock %}
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
{% load static %}
<link rel='stylesheet' href="{% static 'wx/css/styles.css' %}">
</head>
<body>
<div class="container-fluid">
<div class="nav">
<ul>
<li>Plants</li>
<li>Weather</li>
<li>Soil</li>
<li>Yield</li>
</ul>
</div>
<div class="row">
<div class="col-sm-10">{% block content %}{% endblock %}</div>
</div>
</div>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)
我的文件夹结构如下:
Gardendash > wx > 静态 > wx > css > styles.css
我的样式表很简单,
html {
background-color: black;
}
Run Code Online (Sandbox Code Playgroud)
我搜索了堆栈溢出并找到了几个解决方案,但它们没有解决问题。我还发现一个说输入以下内容来查找我的静态位置
python manage.py findstatic --verbosity 2 css/style.css
我收到的结果是
找不到“css/style.css”的匹配文件。
查看以下位置:
/home/my_name/.virtualenvs/django/lib/python3.6/site-packages/django/contrib/admin/static
关于我做错了什么有什么建议吗?我也尝试将 settings.py 中的根 url 调整为文件夹目录,但没有成功。
感谢大家抽出宝贵的时间。
您提到您的项目结构是这样的:
project_folder > app > static > app_name > css > styles.css
Run Code Online (Sandbox Code Playgroud)
由于styles.css位于应用程序的静态目录内,因此您不能简单地以/static/css/styles.css. 这是错误的道路。
正确的路径是这样的:/static/app_name/css/styles.css。
因此,在您的模板中,您可以像这样访问您的文件:
{% static 'app_name/css/styles.css' %}
Run Code Online (Sandbox Code Playgroud)
另外,请确保该应用程序已在 中列出INSTALLED_APPS。
另外,我要澄清几点:
STATIC_ROOT:这是运行命令时django 将保存所有静态文件的目录名称manage.py collectstatic。这仅在生产中需要,否则该目录将并且应该为空。该collectstatic命令复制应用程序和项目中的所有静态文件。STATIC_URL:这是 django 用于生成静态文件url 路径的url 前缀。如果STATIC_URL设置为/static/,django 将转换{% static 'file.jpg' %}为/static/file.jpg.STATICFILES_DIRS:这是一个包含保存项目特定静态文件的目录路径的列表。这只是开发所需要的。当您运行collectstatic命令时,Django 将从该列表中列出的所有路径复制文件并将它们放置在STATIC_ROOT.django.contrib.staticfiles您的INSTALLED_APPS.| 归档时间: |
|
| 查看次数: |
7444 次 |
| 最近记录: |