【问题标题】:Django Template ImagesDjango 模板图像
【发布时间】:2010-07-16 09:31:46
【问题描述】:

采取这样的简单观点:

def my_gallery(request):
    images= ?
    t = Template("<html><body>Here my images from XY {{ images }}.</body></html>")
    html = t.render(Context({'images': ? }))
    return HttpResponse(html)

我必须如何定义变量图像/我必须在上下文中填写什么 让 Django 显示我:

1 张图片 超过 1 张图片 1 个声音文件 超过 1 个声音文件

在 my_gallery 网站上

谢谢!

【问题讨论】:

  • 不清楚您在这里要做什么。您是否希望视图呈现字符串“1 image more than 1 image ...”?
  • 我试图在一个 url 上显示一个或多个图像(= jpeg、gif 等)和/或声音文件

标签: django image templates


【解决方案1】:

我猜你想知道什么..

#/appname/model.py
from django.db import models

class MyImages(models.Model):
    images = models.ImageField(upload_to='/upload_path/')


#/appname/views.py
from appname.models import MyImages
from django.template.context import RequestContext
from django.shortcuts import render_to_response

def my_gallery(request, template='gallery.html'):

    obj_images= MyImages.objects.all()

    kwvars = {
        'images': obj_images,
    }

    return render_to_response(template, kwvars, RequestContext(request))


 #gallery.html
 ...
 {% for image in images %}
     {{ images.url }}
 {% endfor %}

【讨论】:

    猜你喜欢
    • 2014-08-27
    • 2016-12-13
    • 2017-11-19
    • 2017-10-06
    • 2017-04-26
    • 2013-02-16
    • 2023-03-02
    • 2013-12-23
    相关资源
    最近更新 更多