Django中图片显示问题

0 0 python django
写bug小shuai
写bug小shuai

声望值:336 0人

2019-02-11 14:17:30 提问

关注 0关注

收藏 0收藏, 169浏览

现有如下模型:

    class Plant(models.Model):
        category = models.ForeignKey(Category, related_name='plants', verbose_name="所属属")
        name = models.CharField(max_length=200, verbose_name="种名")
        alias = models.CharField(null=True, blank=True, max_length=200, verbose_name="别名")
        latin_name = models.CharField(null=True, blank=True, max_length=200, verbose_name="拉丁名")
        pinyin = models.CharField(max_length=100, verbose_name='拼音')
        slug = models.SlugField(max_length=200, unique=True)
        created = models.DateTimeField(auto_now_add=True, verbose_name="创建时间")
        updated = models.DateTimeField(auto_now=True, verbose_name="更新时间")
        body = models.TextField(verbose_name="描述")
    
        class Meta:
            ordering = ['name']
            verbose_name = '种'
            verbose_name_plural = verbose_name
    
        def get_absolute_url(self):
            return reverse('plants:plant_detail', args=[self.slug])
    
        def __str__(self):
            return self.name
class Image(models.Model):
    plant = models.ForeignKey(Plant, related_name='images', verbose_name='所属植物')
    image = models.ImageField(upload_to="plant_image/%Y/%m",verbose_name="图片")
    created = models.DateField(auto_now_add=True)

    class Meta:
        ordering = ['created']
        verbose_name = '植物图片'
        verbose_name_plural = verbose_name

假如已经上传好某plant的多张图片,
想要在a模板中显示一张图片,在b模板中显示全部图片,该如何使用模板变量?

请先 登录 后评论

0个回答

注册新账号

悬赏追问
10
  • 10
  • 20
  • 50
  • 100
  • 200
  • 输入数值
发布追问