簡體   English   中英

該應用程序未在file.html中顯示圖片-Django,Python

[英]the app does not display pictures in file.html - Django, Python

我在django-app中將照片插入到我的file.html中時遇到問題。 我試圖進行許多更改,但是圖片仍然沒有顯示。 我將不勝感激。

我的html文件

{% extends 'base.html' %}

{% block title %}
<h2>Home templates</h2>
{% endblock %}

{% block content %}
<div class="container">
{% for frond_photo in frond_photo_list %}
{% if frond_photo.image %}
<img src="{{frond_photo.image.url}}" class="img-responsive" />
{% endif %}
{% endfor %}
{% endblock %}

views.py

def frond_photo_list(request):
    queryset = Frond_Photo.objects.all()
    context = {
        "photos": queryset,
    }
    return render (request, "reviews/home.html", context)

Models.py

class Frond_Photo(models.Model):
    title = models.CharField(max_length=100)
    text = models.CharField(max_length=200)
    image = models.FileField(null=True, blank=True)

urls.py(應用程序)

...
url(r'^home/', views.frond_photo_list, name='home'),
...

admin.py

class Frond_PhotoAdmin(admin.ModelAdmin):
    model = Frond_Photo
admin.site.register(Frond_Photo)

當我的文件如下所示時(一切正常)。 顯示圖片,我可以根據需要進行調整。 我想我不得不忘記一些簡單的事情...

models.py

class Wine(models.Model):
    name = models.CharField(max_length=200)
    image = models.FileField(null=True, blank=True)


    def average_rating(self):
        all_ratings = [list(map(lambda x: x.rating, self.review_set.all()))]
        return np.mean(all_ratings)

    def __unicode__(self):
        return self.name

views.py

def wine_list(request):
    wine_list = Wine.objects.order_by('-name')
    context = {'wine_list':wine_list}
    return render(request, 'reviews/wine_list.html', context)

html的

...
{% if wine_list %}
{% for wine in wine_list %}
{% if wine.image %}
<img class="thumbnail"  src="{{wine.image.url}}" alt="Card image cap"  style="width:100%" width="100" height="250" >
{% endif %}
...

編輯Stettings.py

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',
    'bootstrap3',
    'reviews',
    'registration'
]

ACCOUNT_ACTIVATION_DAYS = 7 # One-week activation window
REGISTRATION_AUTO_LOGIN = True # Automatically log the user in.



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

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [os.path.join(BASE_DIR, 'templates')],
        '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 = 'winerama.wsgi.application'

LOGIN_REDIRECT_URL = '/reviews/review/user'


# 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

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/2.0/topics/i18n/

LANGUAGE_CODE = 'en-us'

TIME_ZONE = 'UTC'

USE_I18N = True

USE_L10N = True

USE_TZ = False


# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/2.0/howto/static-files/

STATIC_URL = '/static/'

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

在您的上下文中,將queryset設置為名稱photos但在模板中嘗試遍歷frond_photo_list 如果將模板中的for循環行更改為:

{% for frond_photo in photos %}

您應該得到想要的結果。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM