加载产品图片时出错

时间:2017-08-15 23:44:30

标签: python django

我创建了一个电子商务项目的product_view页面,我正在尝试访问页面上的一些项目及其详细信息。页面加载时,我只是项目图像的名称而不是图像本身。以下是截图:enter image description here

这是我的settings.py

"""
Django settings for shoppingmall project.

Generated by 'django-admin startproject' using Django 1.11.1.

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')

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

# 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 = 'jn^sgbwcco=!w9=h11_m2cvb727#k2w&$jb6w^68xldcr3l$jl'

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

ALLOWED_HOSTS = []

CART_SESSION_ID = 'cart'

# Application definition

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.sites',
    'registration',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'shop',
    'cart',
]



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 = 'shoppingmall.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 = 'shoppingmall.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/

STATICFILES_DIRS = [STATIC_DIR, ]

STATIC_URL = '/static/'

# Media files where django stores media uploaded by users

MEDIA_ROOT = MEDIA_DIR

MEDIA_URL = '/media/'

# DJANGO REGISTRATION REDUX
ACCOUNT_ACTIVATION_DAYS = 7 #Number of days the activation email remains active, afterwards account remains inactive permanently
REGISTRATION_AUTO_LOGIN = True #Account is automatially logged in upon registration

#DJANGO REGISTRATION REDUX
SITE_ID = 1

这是我的urls.py,用于我的项目,其中media_url是

"""shoppingmall 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 django.conf import settings
from django.conf.urls.static import static

urlpatterns = [
    url(r'^admin/', admin.site.urls),
    url(r'^', include('shop.urls', namespace='shop')),
    url(r'^cart/', include('cart.urls', namespace='cart')),
    url(r'^accounts/', include('registration.backends.default.urls')),
]
if settings.DEBUG:
    urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

以下是product_view的模板

{% extends 'shop/base.html' %}
{% load static %}

{% block title_block %}
    Product View
{% endblock %}

{% block body_block %}
    {% for p in product %}
        {{ p.image }}
        <br>
        Product - {{ p.name }}
        <br>
        <!-- {{ p.brand }} -->
        Availability - {{ p.availability }}
        <br>
        <p class="price">Price - ${{ p.price }}</p>
        <form action="{% url 'cart:cart_add' product_id=p.id %}" method="post">
          {{ cart_product_form }}
          {% csrf_token %}
          <input type="submit" value="Add to cart" class="button light">
        </form>
    {% endfor %}
{% endblock %}

1 个答案:

答案 0 :(得分:0)

未经查看settings.pyurls.py ...可能是错误发生在template.html

要显示图片,您需要使用<img>标记,如下所示:

<img src="{{ item.image.url }}">

即使路径不正确,也不应该在页面上显示,因为它是img标记内的参数。好像你忘了把它放在这个标签里......

编辑:既然你用模板代码更新了你的问题,那确实是这样的。