gpt4 book ai didi

python - HTML 不在 django 中提供图像

转载 作者:行者123 更新时间:2023-11-28 17:11:08 25 4
gpt4 key购买 nike

我正在构建一个简单的 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");
}

但是图像没有加载,我不知道渲染它有什么问题!请帮忙!

附言如果我从互联网上提取完美运行的图像

最佳答案

1)您是否尝试过以这种方式链接静态资源?

<img src="{% static 'images/home-background.jpeg' %}" />

Django 应将 static 替换为您的 STATIC_URL,并且该 URL 应变为 /static/images/home-background.jpg

2) 如果您想使用 {% static ... %},您还需要在 html 文件的开头添加此内容>

{% load staticfiles %}

3) 当你使用 {% static 'images/file.jpg' %} 你说你想用 STATIC_ROOT 的值替换 static 。在您的 settings.py 中,您将 STATIC_ROOT 设置为 /static/static-files

我认为您可以通过正确设置 STATIC_ROOT 使其指向/static/来解决您的问题

STATIC_ROOT = os.path.join(os.path.dirname(BASE_DIR), "static")

注意STATIC_ROOT也是存放collectstatic文件的文件夹路径

关于python - HTML 不在 django 中提供图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29197248/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com