Django staticfiles图像未显示
Posted
技术标签:
【中文标题】Django staticfiles图像未显示【英文标题】:Django staticfiles images not showing 【发布时间】:2018-05-14 18:35:33 【问题描述】:我想出了如何使用 Django 将我的 CSS 文件链接到我的 html,但我不知道如何让我的图像显示在我的 base.html 文件中。
这些是我的文件,这就是我添加图像的方式,也许我做错了什么? pic with all the files and directories 首先,我创建了一个静态目录,然后创建了两个子目录,CSS 和 Images。我将 css 文件放在 css 目录中,然后将图像放在图像目录所在的位置。 然后我使用这段代码尝试在 css 中添加 uptown.jpg 作为我的背景。
header
background-image:linear-gradient(rgba(0, 0, 0, 0.5),rgba(0, 0, 0,
0.5)),
url(% static 'static/images/uptown.jpg' %);
height: 65vh;
background-size: cover;
background-position: center;
但该图像从未出现在我的网站上。我还尝试按照我通常在没有 Django 标记的 css 文件中的方式链接它,但仍然没有工作。
我在我的 settings.py 中设置了 staticfiles_dir 和所有内容。所以我不确定是什么问题,所以请帮忙。
这是我的网址和 settings.py:
"""ask_uptown_personal_project URL Configuration
The `urlpatterns` list routes URLs to views. For more information please see:
https://docs.djangoproject.com/en/1.11/topics/http/urls/
Examples:
Function views
1. Add an import: from my_app import views
2. Add a URL to urlpatterns: url(r'^$', views.home, name='home')
Class-based views
1. Add an import: from other_app.views import Home
2. Add a URL to urlpatterns: url(r'^$', Home.as_view(), name='home')
Including another URLconf
1. Import the include() function: from django.conf.urls import url, include
2. Add a URL to urlpatterns: url(r'^blog/', include('blog.urls'))
"""
from django.conf.urls import url, include
from django.contrib import admin
from . import views
from django.conf import settings
from django.conf.urls.static import static
urlpatterns = [
url(r"^$", views.HomePage.as_view(), name="home"),
url(r"^test/$", views.TestPage.as_view(), name="test"),
url(r"^thanks/$", views.ThanksPage.as_view(), name="thanks"),
url(r"^admin/", admin.site.urls),
url(r"^accounts/", include("accounts.urls", namespace="accounts")),
url(r"^accounts/", include("django.contrib.auth.urls")),
url(r"^posts/", include("posts.urls", namespace="posts")),
url(r"^groups/",include("groups.urls", namespace="groups")),
]
if settings.DEBUG:
urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
enter code here
设置:
"""
Django settings for ask_uptown_personal_project project.
Generated by 'django-admin startproject' using Django 1.11.4.
For more information on this file, see
https://docs.djangoproject.com/en/1.11/topics/settings/
For the full list of settings and their values, see
https://docs.djangoproject.com/en/1.11/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__)))
TEMPLATE_DIR = os.path.join(BASE_DIR,'templates')
STATIC_DIR = os.path.join(BASE_DIR,'static')
# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/1.11/howto/deployment/checklist/
# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = '=a0n@y2ei!x@mjo5mkco5=im_($*+8mnlx-e8-yjw3qo$uy2mx'
# 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',
'accounts',
'bootstrap3',
'groups',
'posts',
]
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 = 'ask_uptown_personal_project.urls'
TEMPLATES = [
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [TEMPLATE_DIR,],
'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 = 'ask_uptown_personal_project.wsgi.application'
# Database
# https://docs.djangoproject.com/en/1.11/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/1.11/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/1.11/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/1.11/howto/static-files/
STATIC_URL = '/static/'
STATICFILES_DIRS = [
STATIC_DIR,
]
LOGIN_REDIRECT_URL = "test"
LOGOUT_REDIRECT_URL = "thanks"
最好, 谢谢!
【问题讨论】:
试图回答你。如果您在此处提供 urls.py 和 settings.py 代码会有所帮助 我编辑了答案,所以它有两个文件] 我的解决方案对您有用吗? How to refer to static files in my css files?的可能重复 现在检查我添加了它,它已经有模板标签,并且没有工作。现在看代码看看。 【参考方案1】:您需要在urls.py
中添加静态文件的路径,如您在Django documentation 中所见。
from django.conf import settings
from django.conf.urls.static import static
urlpatterns = [
# ... the rest of your URLconf goes here ...
] + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
您还需要在模板文件上加载静态文件。
% load static %
<html>
...
【讨论】:
以上是关于Django staticfiles图像未显示的主要内容,如果未能解决你的问题,请参考以下文章