【发布时间】:2012-04-03 18:49:44
【问题描述】:
我无法在模板上显示图片(或图片链接)。除了在自定义模型方法中定义的图像“render_thumbnail”之外,其他一切都在模板上工作。我究竟做错了什么? -btw 当我只使用图像表时,render_thumbnail 可以在另一个模板上工作,并且正在使用 -images.render_thumbnail 显示。谢谢。
模型.py
class Listings(models.Model):
createdate = models.DateTimeField(auto_now_add=True)
expirydate = models.DateTimeField(null=True, blank=True)
price = models.IntegerField(null=True, blank=True)
broker_y_n = models.CharField(max_length=1, blank=True, choices=broker, default='n')
listing_type = models.ForeignKey(ListingType)
listing_status = models.ForeignKey(ListingStatus, default=3)
customer = models.ForeignKey(Customer)
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 render_thumbnail(self):
return mark_safe("""<a href = "/media/%s"><img border="0" alt="" src="/media/%s" /></a>""" % ((self.image.name, self.thumbnail.name)))
#render_thumbnail.allow_tags = True
def render_thumbnail2(self):
return mark_safe("""<a href = "/media/%s"><img border="0" alt="" src="/media/%s" /></a>""" % ((self.image.name, self.thumbnail2.name)))
#render_thumbnail.allow_tags = True
def __unicode__(self):
return self.image.name
查看.py
def details_customer(request, user_id):
customer = get_object_or_404(Customer, user=user_id)
cusnum=customer.id
image = Image.objects.all()
listings = Listings.objects.filter(customer=cusnum).values(
'id',
'price',
'listing_type__desc',
'listing_status',
'listing_status__desc',
'canoekayak__builder',
'image__title',
'image__thumbnail',
'image__render_thumbnail',
)
context = Context({
'title': 'Details',
'customer': customer,
'image' : image,
'listings' : listings,
})
return render_to_response('bsmain/details.html', context)
模板表
<TABLE id="some_id">
<TBODY>
{% load humanize %}
{% for row in listings %}
<tr>
<td>{{ row.id }}</td>
<td align="right">{{row.price|intcomma}}</td>
<td>{{ row.listing_type__desc}}</td>
<td>{{ row.listing_status}}</td>
<td>{{ row.listing_status__desc}}</td>
<td>{{ row.canoekayak__builder}}</td>
<td>{{ row.image__title}}</td>
<td>{{ row.image__thumbnail}}</td
<td>{{ row.image__render_thumbnail}}</td
</tr>
{% endfor %}
</TBODY>
【问题讨论】:
-
你得到什么而不是图像?我怀疑你的
<img../>标签被无意中转义了。 -
1.直接使用查询集,不附加
values2. 不要像'/media/'那样对src进行硬编码,尝试unicode(image.thumbnail)和image.thumbnail.url -
嘿 9000,我得到一个空白表格单元格,什么都没有,nada。
-
尝试从 Python shell 调用你的
render_thumbnail()。 -
有些东西你没有在这里展示给我们。当您尝试包含
image__render_thumbnail时,Listing.objects.values调用将失败并显示FieldError,因为render_thumbnail是一种方法,而不是一个字段。
标签: python html django django-templates