I am having problems getting images and image links to display. I have a model method ('thumbnail_') that is supposed to display a thumbnail that is the link the the full size image. It is not rendering to the webpage (image.html). What am I doing wrong? Thank you.
我在显示图像和图像链接时遇到问题。我有一个模型方法('thumbnail_'),它应该显示一个缩略图,它是全尺寸图像的链接。它不会呈现给网页(image.html)。我究竟做错了什么?谢谢。
model.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>
2 个解决方案
#1
1
I'm surprised you get anything at all. This line in your view:
我很惊讶你得到任何东西。您在视图中的这一行:
image = Image.objects.values('id', 'title', 'thumbnail_')
is not allowed, because thumbnail_
is not a field, it is a method on the Image
class. values
operates only on fields, returning a queryset containing dicts of those names. Even if you used the actual field, thumbnail
, in the values
call, the template would still not output correctly, as the thumbnail_
method does not exist on the dict that is returned by values
.
不允许,因为thumbnail_不是字段,它是Image类的一个方法。 values仅对字段起作用,返回包含这些名称的dicts的查询集。即使您在值调用中使用了实际字段缩略图,模板仍然无法正确输出,因为在值返回的字典中不存在thumbnail_方法。
The simple solution is to use a standard Image.objects.all()
call here - trying to limit the number of fields returned is over-optimization.
简单的解决方案是在此处使用标准的Image.objects.all()调用 - 尝试限制返回的字段数量是过度优化。
(Also, please try and give your method a nicer name: something like render_thumbnail
would be better than just thumbnail_
.)
(另外,请尝试给你的方法一个更好的名字:像render_thumbnail这样的东西比只有thumbnail_更好。)
#2
0
I can't see any attribute named thumbnail_
:
我看不到任何名为thumbnail_的属性:
image = Image.objects.values('id', 'title', 'thumbnail_')
and
thumbnail = models.ImageField(upload_to="images/", blank=True, null=True)
thumbnail2 = models.ImageField(upload_to="images/", blank=True, null=True)
Check if the image is in correct directory.
检查图像是否在正确的目录中。
#1
1
I'm surprised you get anything at all. This line in your view:
我很惊讶你得到任何东西。您在视图中的这一行:
image = Image.objects.values('id', 'title', 'thumbnail_')
is not allowed, because thumbnail_
is not a field, it is a method on the Image
class. values
operates only on fields, returning a queryset containing dicts of those names. Even if you used the actual field, thumbnail
, in the values
call, the template would still not output correctly, as the thumbnail_
method does not exist on the dict that is returned by values
.
不允许,因为thumbnail_不是字段,它是Image类的一个方法。 values仅对字段起作用,返回包含这些名称的dicts的查询集。即使您在值调用中使用了实际字段缩略图,模板仍然无法正确输出,因为在值返回的字典中不存在thumbnail_方法。
The simple solution is to use a standard Image.objects.all()
call here - trying to limit the number of fields returned is over-optimization.
简单的解决方案是在此处使用标准的Image.objects.all()调用 - 尝试限制返回的字段数量是过度优化。
(Also, please try and give your method a nicer name: something like render_thumbnail
would be better than just thumbnail_
.)
(另外,请尝试给你的方法一个更好的名字:像render_thumbnail这样的东西比只有thumbnail_更好。)
#2
0
I can't see any attribute named thumbnail_
:
我看不到任何名为thumbnail_的属性:
image = Image.objects.values('id', 'title', 'thumbnail_')
and
thumbnail = models.ImageField(upload_to="images/", blank=True, null=True)
thumbnail2 = models.ImageField(upload_to="images/", blank=True, null=True)
Check if the image is in correct directory.
检查图像是否在正确的目录中。