如果文件显示,则显示django模板中的默认值

时间:2011-07-27 14:52:02

标签: django-templates

我必须显示大约5000张图像,我的models.py有一个字段imagename,其中包含文件夹中图像的名称。但问题是:对应所有图像名称,图像不存在,在这种情况下我想要一个默认图像说def.jpeg显示,我该怎么做?  说变量obj.image有图像名称,如果不存在那么我想显示default.jpeg

简而言之:

{% if obj.image %} // don't get confused, every obj.image has a name, but not necessarily a file corresponding to it
 <img src="{{ STATIC_URL }}images/{{dish.image}}" width=150px />

{% else %}
  <img src="somedefaultimage.jpg" />

Models.py:

class ImageApp(models.Model):
    class Meta:
        db_table = "basetable"
        ordering = ['name']
    def __unicode__(self):
        return self.name

    #imported_pk = models.IntegerField()
   # category_name = models.CharField(max_length=100)
    category = models.ForeignKey(Category,null=True,blank=True)
    something = models.CharField(max_length = 100, null=True, blank=True)
    name =  models.CharField(max_length = 100)
    instructions = models.TextField()
    image = models.CharField(max_length=100, null=True, blank=True)

0 个答案:

没有答案