I have problem with inserting photos to my file.html in django-app. I tried to make many changes, but the picture is still not displayed. I will be grateful for any help.
My html file
{% 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 (app)
...
url(r'^home/', views.frond_photo_list, name='home'),
...
admin.py
class Frond_PhotoAdmin(admin.ModelAdmin):
model = Frond_Photo
admin.site.register(Frond_Photo)
When my file looks like this (below) Everything is working properly. The pictures are displayed and I can adapt them to my needs. I think I had to forget something simple ...
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 %}
...
EDIT 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')
答案 0 :(得分:1)
在您的上下文中,将查询集设置为名称photos
,但是在模板中,您尝试遍历frond_photo_list
。
如果您将模板中的for循环行更改为:
{% for frond_photo in photos %}
您应该得到想要的结果。