【问题标题】:I am trying to add an image in html using django framework, image does not appear我正在尝试使用 django 框架在 html 中添加图像,图像没有出现
【发布时间】:2020-08-18 05:07:23
【问题描述】:
{% extends "basic_app/base.html" %}
{% load staticfiles %}
{% block body_block %}
<div class="card text-center">
  <div class="card-body" style="background-color:#F22A00">
    <h5 class="card-title" style="color:black;text-align:center;">TSEC CODESTORM</h5>
    <p class="card-text" style="color:black;text-align:center;">A campus chapter of codecheff</p>
  </div></div>
    <img src="{% static "basic_app/images/hckathon.jpg" %}" alt="Uh Oh, didn't show!">
{% endblock %}

这是我在扩展索引文件中使用的代码,但是在尝试不同的浏览器后,图像不可见。 Here is a screenshot of my webpage

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.0/howto/deployment/checklist/

# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = 'j&wblwbdq(i*x+r&l0y96-wv%c5bjopqbgix(0s*i#o32%5r*b'

# 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',
    'basic_app.apps.BasicAppConfig',
]

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

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [],
        '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 = 'learning_users.wsgi.application'


# Database
# https://docs.djangoproject.com/en/2.0/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.0/ref/settings/#auth-password-validators

PASSWORD_HASHERS = [
    'django.contrib.auth.hashers.Argon2PasswordHasher',
    'django.contrib.auth.hashers.BCryptSHA256PasswordHasher',
    'django.contrib.auth.hashers.BCryptPasswordHasher',
    'django.contrib.auth.hashers.PBKDF2PasswordHasher',
    'django.contrib.auth.hashers.PBKDF2SHA1PasswordHasher',
]

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


# Internationalization
# https://docs.djangoproject.com/en/2.0/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/2.0/howto/static-files/

STATIC_URL = '/static/'
STATICFILES_DIRS = [
os.path.join(BASE_DIR, 'the_directory/static/')]

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

# MEDIA INFORMATION:

MEDIA_URL = '/media/'
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')

LOGIN_URL = '/basic_app/user_login/'

这是我编辑并添加的 seetings.py 文件,请看一下。 请让我知道我哪里出错了。 我按照你们中的一个人提到的那样进行了编辑,但仍然没有结果。 很久以来一直被困在这个问题上,希望有人帮忙。

【问题讨论】:

  • 你确定你的文件名是 hckathon.jpg,看起来很奇怪,像错字,告诉我们你的设置是否包含静态 URL -> docs.djangoproject.com/en/3.0/howto/static-files/…
  • 不,图片名称是正确的,没问题
  • 尝试使用{% load static %} 而不是{% load staticfiles %}

标签: html css django web


【解决方案1】:

通常当我遇到这样的问题时,那是因为我的路径有问题。在设置文件中你有类似的东西

STATICFILES_DIRS = [
os.path.join(BASE_DIR, 'the_directory/static/')]

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

那么你可以有一个像

这样的行
<img src="{% static 'hckathon.jpg' %}" alt="Uh Oh, didn't show!">

the_directory 的静态文件夹位于标记为 static 的文件夹中,该文件夹与您的设置文件位于同一目录中。

【讨论】:

  • 您需要使用自己的目录名称而不是_directory,我回答中的最后一句话不是那么清楚。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-09-16
  • 2014-11-09
  • 1970-01-01
  • 1970-01-01
  • 2018-11-06
  • 2018-04-01
  • 1970-01-01
相关资源
最近更新 更多