我的图像显示未找到错误,但终端中的链接能够找到该文件

问题描述 投票:0回答:1

我的图片无法加载到页面,反馈显示

未找到:/auctions/static/auctions/images/Artboard_1_copy.png “获取/拍卖/静态/拍卖/图像/Artboard_1_copy.png HTTP/1.1”404 3417

但是当我按住 Ctrl 键并单击反馈中的链接时,我能够在工作区中打开我的图像。

这是我的设置.py

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

# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = '6ps8j!crjgrxt34cqbqn7x&b3y%(fny8k8nh21+qa)%ws3fh!q'

# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True

ALLOWED_HOSTS = []


# Application definition

INSTALLED_APPS = [
    'auctions',
    '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 = 'commerce.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 = 'commerce.wsgi.application'


# Database
# https://docs.djangoproject.com/en/3.0/ref/settings/#databases

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.sqlite3',
        'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
    }
}

AUTH_USER_MODEL = 'auctions.User'

# Password validation
# https://docs.djangoproject.com/en/3.0/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/3.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/3.0/howto/static-files/

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


import mimetypes
mimetypes.add_type("text/css", ".css", True)

我的 html,加载静态包含在文件顶部

<img class="image1" src="{{ listing.image }}">

我的模型.py

image = models.ImageField(upload_to="auctions/static/auctions/images", blank=True)

我的网址.py

urlpatterns = [
    path("", views.index, name="index"),
    path("<int:id>", views.item, name="item"),
    path("listing", views.Listing, name="listing"),
    path("login", views.login_view, name="login"),
    path("logout", views.logout_view, name="logout"),
    path("register", views.register, name="register")
] + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)

我尝试添加 static(settings.STATIC_URL, document_root=settings.STATIC_ROOT) 和 STATICFILES_DIRS = [os.path.join(BASE_DIR, "static"),] 但不起作用。尝试将“.url”添加到 {{listing.image.url}} 中,但效果不佳。

html django image
1个回答
0
投票

我无法发表评论,所以我在这里回答。您应该引用

url
对象的
listing.image
属性而不是对象本身:
<img class="image1" src="{{ listing.image.url }}">

© www.soinside.com 2019 - 2024. All rights reserved.