该应用程序不会在 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 %}

视图.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] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

为什么迁移不起作用,它也不会在 django 应用程序中创建迁移目录

来自分类Dev

应用程序不会在recyclerview火力显示数据

来自分类Dev

点击应用程序图标不会在应用程序中显示通知

来自分类Dev

点击应用程序图标不会在应用程序中显示通知

来自分类Dev

图片不会在wordpress中显示

来自分类Dev

启动firefox时,它不会在应用程序中显示,但会在进程中显示

来自分类Dev

Android.com的基本应用程序不会在应用程序中显示DisplayMessageActivity

来自分类Dev

Django:图片/绝对URL不在html中显示

来自分类Dev

为什么静态文件不会在Django Web应用程序中加载?

来自分类Dev

即使扩展了应用程序图标,共享扩展程序的应用程序图标也不会在设备中更新

来自分类Dev

如何添加 Fragment 以便我的应用程序不会在屏幕上显示为空白

来自分类Dev

循环不会在python程序中执行

来自分类Dev

HTML不会在本地显示照片

来自分类Dev

在 Heroku 中更改 ENV 变量不会在 Phoenix 应用程序中更改它们

来自分类Dev

rCharts不会在闪亮的应用程序中渲染图

来自分类Dev

ffmpeg不会在我的项目中构建,在示例应用程序中可以正常工作

来自分类Dev

字体不会在Production Rails应用程序中呈现

来自分类Dev

类不会在GWT应用程序中序列化

来自分类Dev

单击Firebase动态链接不会在AppDelegate中调用应用程序(_:继续:restoreHandler :)方法

来自分类Dev

Kafka:从主机发布的事件不会在Docker中运行的应用程序使用

来自分类Dev

npm install不会在react应用程序中修改package.json

来自分类Dev

TextField将不会在Cocoa应用程序中更新

来自分类Dev

webstorm不会在nodejs应用程序中的断点处停止

来自分类Dev

类不会在GWT应用程序中序列化

来自分类Dev

应用程序不会在随机访问文件中搜索零

来自分类Dev

JSF不会在setter和getter中获取数据(CRUD应用程序)

来自分类Dev

瑞克任务不会在我的sinatra应用程序中运行

来自分类Dev

Turbolinks 加载事件永远不会在全新的 Rails 5.1 应用程序中触发

来自分类Dev

`xdg-open` 不会在首选应用程序中打开文件

Related 相关文章

  1. 1

    为什么迁移不起作用,它也不会在 django 应用程序中创建迁移目录

  2. 2

    应用程序不会在recyclerview火力显示数据

  3. 3

    点击应用程序图标不会在应用程序中显示通知

  4. 4

    点击应用程序图标不会在应用程序中显示通知

  5. 5

    图片不会在wordpress中显示

  6. 6

    启动firefox时,它不会在应用程序中显示,但会在进程中显示

  7. 7

    Android.com的基本应用程序不会在应用程序中显示DisplayMessageActivity

  8. 8

    Django:图片/绝对URL不在html中显示

  9. 9

    为什么静态文件不会在Django Web应用程序中加载?

  10. 10

    即使扩展了应用程序图标,共享扩展程序的应用程序图标也不会在设备中更新

  11. 11

    如何添加 Fragment 以便我的应用程序不会在屏幕上显示为空白

  12. 12

    循环不会在python程序中执行

  13. 13

    HTML不会在本地显示照片

  14. 14

    在 Heroku 中更改 ENV 变量不会在 Phoenix 应用程序中更改它们

  15. 15

    rCharts不会在闪亮的应用程序中渲染图

  16. 16

    ffmpeg不会在我的项目中构建,在示例应用程序中可以正常工作

  17. 17

    字体不会在Production Rails应用程序中呈现

  18. 18

    类不会在GWT应用程序中序列化

  19. 19

    单击Firebase动态链接不会在AppDelegate中调用应用程序(_:继续:restoreHandler :)方法

  20. 20

    Kafka:从主机发布的事件不会在Docker中运行的应用程序使用

  21. 21

    npm install不会在react应用程序中修改package.json

  22. 22

    TextField将不会在Cocoa应用程序中更新

  23. 23

    webstorm不会在nodejs应用程序中的断点处停止

  24. 24

    类不会在GWT应用程序中序列化

  25. 25

    应用程序不会在随机访问文件中搜索零

  26. 26

    JSF不会在setter和getter中获取数据(CRUD应用程序)

  27. 27

    瑞克任务不会在我的sinatra应用程序中运行

  28. 28

    Turbolinks 加载事件永远不会在全新的 Rails 5.1 应用程序中触发

  29. 29

    `xdg-open` 不会在首选应用程序中打开文件

热门标签

归档