Display image from ImageField by means of form

前端 未结 3 1230
猫巷女王i
猫巷女王i 2021-01-05 07:18

prelude:

Here\'s the simpliest way to display an ImageField. Lets assume I have 10 fields in my form and I don\'t wanna iterate over all of them in

3条回答
  •  悲哀的现实
    2021-01-05 07:47

    As Selcuk mentioned the simplest solution was to create my own widget.

    from string import Template
    from django.utils.safestring import mark_safe
    from django.forms import ImageField
    
    class PictureWidget(forms.widgets.Widget):
        def render(self, name, value, attrs=None, **kwargs):
            html =  Template("""""")
            return mark_safe(html.substitute(link=value))
    
    class UserProfileForm(forms.ModelForm):
        photo = ImageField(widget=PictureWidget)
    

提交回复
热议问题