试图加载模板图像上传到ImageField

时间:2017-08-04 19:49:38

标签: html django django-models django-templates django-urls

使用代码下面所有渲染图像的alt选项... 如果我使用图像的实际文件路径对下面的HTML语句进行硬编码,则代码可以正常工作:src“media / media / image.jpg。 这是我的html声明:

<img class="first-slide" src="media/{{Home.slide_1.photo.url}}" alt="First slide">

这是我的Models.py:

class Photo(models.Model):
    photo = models.ImageField(upload_to = 'media/', default = 'media/None/no-img.jpg')
    photo_caption = models.CharField(max_length=25)
    photo_description = models.CharField(max_length=50)
    master_album = models.ManyToManyField('Album', related_name="album")
    created_at = models.DateTimeField(auto_now_add=True)
    updated_at = models.DateTimeField(auto_now=True)
    objects = PhotoManager()

    def __str__(self):
        return self.photo_caption

class Home(models.Model):
    slide_1 = models.ManyToManyField('Photo', related_name="slide_1")
    slide_2 = models.ManyToManyField('Photo', related_name="slide_2")
    slide_3 = models.ManyToManyField('Photo', related_name="slide_3")
    created_at = models.DateTimeField(auto_now_add=True)
    updated_at = models.DateTimeField(auto_now=True)

0 个答案:

没有答案