【问题标题】:Django - displaying images and image linksDjango - 显示图像和图像链接
【发布时间】:2012-01-29 22:11:02
【问题描述】:

我在获取要显示的图像和图像链接时遇到问题。我有一个模型方法('thumbnail_'),它应该显示一个缩略图,它是全尺寸图像的链接。它不会呈现到网页(image.html)。我究竟做错了什么?谢谢。

模型.py

class Image(models.Model):
    title = models.CharField(max_length=60, blank=True, null=True)
    image = models.ImageField(upload_to="images/", blank=True, null=True)
    thumbnail = models.ImageField(upload_to="images/", blank=True, null=True)
    thumbnail2 = models.ImageField(upload_to="images/", blank=True, null=True)
    #tags = models.ManyToManyField(Tag, blank=True)
    #albums = models.ManyToManyField(Album, blank=True)
    created = models.DateTimeField(auto_now_add=True)
    #rating = models.IntegerField(default=50)
    width = models.IntegerField(blank=True, null=True)
    height = models.IntegerField(blank=True, null=True)
    listings = models.ForeignKey(Listings)

def save(self, *args, **kwargs):
    # Save image dimensions
    super(Image, self).save(*args, **kwargs)
    im = PImage.open(pjoin(MEDIA_ROOT, self.image.name))
    self.width, self.height = im.size

    # large thumbnail
    fn, ext = os.path.splitext(self.image.name)
    im.thumbnail((256,256), PImage.ANTIALIAS)
    thumb_fn = fn + "-thumb2" + ext
    tf2 = NamedTemporaryFile()
    im.save(tf2.name, "JPEG")
    self.thumbnail2.save(thumb_fn, File(open(tf2.name)), save=False)
    tf2.close()

    # small thumbnail
    im.thumbnail((60,60), PImage.ANTIALIAS)
    thumb_fn = fn + "-thumb" + ext
    tf = NamedTemporaryFile()
    im.save(tf.name, "JPEG")
    self.thumbnail.save(thumb_fn, File(open(tf.name)), save=False)
    tf.close()

    super(Image, self).save(*args, **kwargs)


def size(self):
    # Image size #
    return "%s x %s" % (self.width, self.height)

def thumbnail_(self):
    return """<a href = "/media/%s"><img border="0" alt="" src="/media/%s" /></a>""" % (
                                                        (self.image.name, self.thumbnail.name))
thumbnail_.allow_tags = True

def __unicode__(self):
    return self.image.name

view.py

def image(request):
    image = Image.objects.values('id', 'title', 'thumbnail_')
    context = {
        'image' : image,
    }
    return render_to_response('bsmain/image.html', context)

image.html

<TABLE id="some_id">    
<TBODY>
    {% load humanize %}
    {% for row in image %}
    <tr>
        <td>{{ row.id }}</td>
        <td>{{ row.title}}</td>
        <td>{{ row.thumbnail_}}</td>            
    </tr>
    {% endfor %}
</TBODY>

【问题讨论】:

    标签: python django django-templates


    【解决方案1】:

    我很惊讶你能得到任何东西。您认为这一行:

    image = Image.objects.values('id', 'title', 'thumbnail_')
    

    是不允许的,因为thumbnail_ 不是一个字段,它是Image 类上的一个方法。 values 仅对字段进行操作,返回包含这些名称的 dicts 的查询集。即使您在values 调用中使用了实际字段thumbnail,模板仍然无法正确输出,因为values 返回的dict 上不存在thumbnail_ 方法。

    简单的解决方案是在此处使用标准的Image.objects.all() 调用 - 尝试限制返回的字段数量是过度优化。

    (另外,请尝试为您的方法起一个更好的名称:render_thumbnail 之类的名称比 thumbnail_ 更好。)

    【讨论】:

    • 我将方法更改为 render_thumbnail,并使用了 Image.objects.all()。我还注释掉了---- thumbnail_.allow_tags = True 和 from django.utils.safestring import mark_safe,然后改变我的方法来反映 "mark_safe" return mark_safe(""" """ % ((self.image.name, self.thumbnail.name)))
    • 我现在得到的标签看起来应该可以工作,但它们没有呈现缩略图或链接。
    【解决方案2】:

    我看不到任何名为 thumbnail_ 的属性:

    image = Image.objects.values('id', 'title', 'thumbnail_')
    

    和

    thumbnail = models.ImageField(upload_to="images/", blank=True, null=True)
    thumbnail2 = models.ImageField(upload_to="images/", blank=True, null=True)
    

    检查图片是否在正确的目录中。

    【讨论】:

      猜你喜欢
      • 2016-02-03
      • 2021-08-03
      • 2018-03-17
      • 2011-03-17
      • 1970-01-01
      • 2012-04-14
      • 1970-01-01
      • 2018-06-17
      • 1970-01-01
      相关资源
      最近更新 更多