2012-04-01 77 views
0

我無法獲取要在模板上顯示的圖像(或圖像鏈接)。其他所有內容都在模板上工作,但圖像「render_thumbnail」在自定義模型方法中定義。我究竟做錯了什麼? -btw當我僅使用Images表格工作時,render_thumbnail在另一個模板上工作,並使用 - images.render_thumbnail進行顯示。謝謝。django模板 - 在表格中顯示圖像

Models.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 

View.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> 

+1

你得到什麼而不是圖像?我懷疑你的''標籤被無意中逃脫了。 – 9000 2012-04-01 01:03:55

+1

1.直接使用queryset不帶附加'values' 2.不要硬編碼src就像'/ media /',試試unicode(image.thumbnail)和image.thumbnail.url – okm 2012-04-01 01:05:40

+0

hey 9000,我得到一個空白表格單元格,就像沒有任何事情一樣,納達。 – BillB1951 2012-04-01 01:35:52

回答

2

如果你想使你的代碼更簡單,我想建議使用ap折012 django-tables2。這種方法可以解決您關於生成表的所有問題。

由於文檔賽斯:

Django的tables2簡化轉彎組數據轉換成HTML 表的任務。它具有對分頁和排序的本機支持。它爲 HTML表格提供了django.forms爲HTML表單所做的工作。例如

它的特點包括:

  • 任何迭代可以是一個數據源,但包括用於Django的查詢集的特殊支持。
  • 內置用戶界面不依賴於JavaScript。
  • 支持基於Django模型的自動錶格生成。
  • 通過子類別支持自定義列功能。
  • 分頁。
  • 基於列的表格排序。
  • 模板標籤,可以將簡單的渲染轉換爲HTML。
  • 在Django 1.3中使用的通用視圖mixin。

創建表格很簡單,只要:

import django_tables2 as tables 

class SimpleTable(tables.Table): 
    class Meta: 
     model = Simple 

這然後將在視圖中使用:

def simple_list(request): 
    queryset = Simple.objects.all() 
    table = SimpleTable(queryset) 
    return render_to_response("simple_list.html", {"table": table}, 
           context_instance=RequestContext(request)) 

最後在模板:

{% load django_tables2 %} 
{% render_table table %} 

這個例子顯示了 最簡單的情況之一,但是d jango-tables2可以做更多!查看 瞭解更多詳情,請點擊documentation

也可以使用字典代替queryset。

處理表格單元格顯示圖像,你可以使用自定義表列HtmlColumn

from django.utils.safestring import mark_safe 
import django_tables2 as tables 

from pytils.numeral import get_plural 
class HtmlColumn(tables.Column): 
    def render(self, value): 
     return mark_safe(value) 

或以同樣的方式創造新的ImageColumn和只傳送SRC屬性,而不是整個IMG標籤。

+0

Rasmus ..感謝您告訴我關於django-tables2。我會看看。我一直在考慮數據表,但還沒有做出任何決定。 – BillB1951 2012-04-05 01:08:26

+0

如果你的意思是jQuery lib http://datatables.net/,最好的決定是使用它們兩個。 Django-table2將讓你組織服務器端部分,數據表的客戶端部分。 – ramusus 2012-04-08 12:27:56