我正在构建一个简单的Django网站但是我的html文件中的图像没有加载,对于插入到html页面或背景图像中的任何图像都是一样的。
我的settings.py文件如下:
import os
BASE_DIR = os.path.dirname(os.path.dirname(__file__))
# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/1.7/howto/deployment/checklist/
# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True
TEMPLATE_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',
'myapp',
)
MIDDLEWARE_CLASSES = (
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.auth.middleware.SessionAuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
)
ROOT_URLCONF = 'Site.urls'
WSGI_APPLICATION = 'Site.wsgi.application'
# Database
# https://docs.djangoproject.com/en/1.7/ref/settings/#databases
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'SiteDatabase',
'USER': 'siteadmin',
'PASSWORD': 'siteadmin',
}
}
# Internationalization
# https://docs.djangoproject.com/en/1.7/topics/i18n/
LANGUAGE_CODE = 'en-us'
TIME_ZONE = 'UTC+05:30'
USE_I18N = True
USE_L10N = True
USE_TZ = True
# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/1.7/howto/static-files/
STATIC_URL = '/static/'
#TEMPLATES
TEMPLATE_DIRS = (
os.path.join(os.path.dirname(BASE_DIR), "static", "templates"),
)
if DEBUG:
MEDIA_URL = '/media/'
STATIC_ROOT = os.path.join(os.path.dirname(BASE_DIR), "static", "static-files")
MEDIA_ROOT = os.path.join(os.path.dirname(BASE_DIR), "static", "media")
STATICFILES_DIRS = (
os.path.join(os.path.dirname(BASE_DIR), "static", "static"),
)
我还将我的urls.py文件更新为:
from django.conf.urls import patterns, include, url
from django.contrib import admin
from django.conf import settings
from django.conf.urls.static import static
urlpatterns = patterns('',
# Examples:
url(r'^$', 'qaengine.views.home', name='home'),
# url(r'^blog/', include('blog.urls')),
url(r'^captcha/', include('captcha.urls')),
url(r'^admin/', include(admin.site.urls)),
)
if settings.DEBUG:
urlpatterns += static(settings.STATIC_URL,
document_root = settings.STATIC_ROOT)
urlpatterns += static(settings.MEDIA_URL,
document_root = settings.MEDIA_ROOT)
以下是我的静态目录:
/static/
/static/images/home-background.jpeg
/static/media/
/static/static/home.css
/static/static-files/ (This is where all files from collectstatic are stored)
/static/templates/home.html
以下是我的html和css文件:
HTML:
<html>
<head>
<link type="text/css" rel="stylesheet" href = "../static/home.css" />
</head>
<body>
<img src="../images/home-background.jpeg"/>
<form method='POST' action=''>
{% csrf_token %}
{{ form.as_p }}
<input type='submit'>
</form>
</body>
</html>
CSS:
body {
background-image: url("../images/home-background.jpeg");
}
但图像没有加载,我不知道渲染它的问题是什么!请帮忙!
P.S。如果我从互联网上提取完美的图像
答案 0 :(得分:1)
1)您是否尝试过以这种方式链接静态资源?
<img src="{% static 'images/home-background.jpeg' %}" />
Django应该用您的STATIC_URL替换static
,并且网址应该变为/static/images/home-background.jpg
2)如果您想使用{% static ... %}
{% load staticfiles %}
3)当您使用{% static 'images/file.jpg' %}
时,您说要将静态替换为STATIC_ROOT
的值。
在您的settings.py中,您将STATIC_ROOT
设置为/static/static-files
。
我认为您可以正确解决问题设置STATIC_ROOT,使其指向/ static /
STATIC_ROOT = os.path.join(os.path.dirname(BASE_DIR), "static")
请注意,STATIC_ROOT也是存储来自collectstatic的文件的文件夹的路径
答案 1 :(得分:0)
您需要在网址中指定静态路径:
body {
background-image: url("/static/images/home-background.jpeg");
}
或者更好,
body {
background-image: url("{% static "/static/images/home-background.jpeg" %}");
}