我在 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 %}
视图.py
def frond_photo_list(request):
queryset = Frond_Photo.objects.all()
context = {
"photos": queryset,
}
return render (request, "reviews/home.html", context)
模型.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'),
...
管理文件
class Frond_PhotoAdmin(admin.ModelAdmin):
model = Frond_Photo
admin.site.register(Frond_Photo)
当我的文件看起来像这样(如下)时,一切正常。图片会显示出来,我可以根据自己的需要调整它们。我想我不得不忘记一些简单的事情......
模型.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
视图.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')
在您的上下文中,您将查询集设置为名称,photos
但在您尝试循环的模板中frond_photo_list
。如果您将模板中的 for 循环行更改为:
{% for frond_photo in photos %}
你应该得到你想要的结果。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句