我很难从django形式获取图像url.
模型:
class Sponsor(models.Model): image = ProcessedImageField(upload_to='sponsors/', processors=[SmartResize(300, 120, upscale=False)], format='JPEG', options={'quality': 100}, null=True)
形成:
class SponsorForm(forms.ModelForm): class Meta: model = Sponsor fields = ('image',)
我使用时渲染它:
{{ sponsorform.image }}
它打印:
Currently: sponsors/random1.jpg
Change:
所以我的问题是,如何在模板中获取该URL?我试过了:
{{ sponsorform.image.url }} {{ sponsorform.image.path }} {{ sponsorform.image.href }}
但似乎没有任何效果,任何建议?
你应该尝试{{ sponsorform.instance.image.url }}
。如果表单不受限制,则可以执行以下操作:
{% if sponsorform.instance.image %} {{ sponsorform.instance.image.url }} {% endif %}