Django ModelForm ImageField

前端 未结 3 747
鱼传尺愫
鱼传尺愫 2020-12-16 06:51

I have a model and a form (forms.ModelForm) in which I have an ImageField. The model resembles:

class Business(models.Model):
    business_name = models.Char         


        
相关标签:
3条回答
  • 2020-12-16 07:25

    Here is my solution to the same problem.

    from django.utils.safestring import mark_safe
    from django.utils.html import escape, conditional_escape
    from django.utils.encoding import force_unicode
    from django.forms.widgets import ClearableFileInput, Input, CheckboxInput
    
    class CustomClearableFileInput(ClearableFileInput):
    
        def render(self, name, value, attrs=None):
            substitutions = {
                #uncomment to get 'Currently'
                'initial_text': "", # self.initial_text, 
                'input_text': self.input_text,
                'clear_template': '',
                'clear_checkbox_label': self.clear_checkbox_label,
                }
            template = '%(input)s'
            substitutions['input'] = Input.render(self, name, value, attrs)
    
            if value and hasattr(value, "url"):
                template = self.template_with_initial
                substitutions['initial'] = ('<img src="%s" alt="%s"/>'
                                            % (escape(value.url),
                                               escape(force_unicode(value))))
                if not self.is_required:
                    checkbox_name = self.clear_checkbox_name(name)
                    checkbox_id = self.clear_checkbox_id(checkbox_name)
                    substitutions['clear_checkbox_name'] = conditional_escape(checkbox_name)
                    substitutions['clear_checkbox_id'] = conditional_escape(checkbox_id)
                    substitutions['clear'] = CheckboxInput().render(checkbox_name, False, attrs={'id': checkbox_id})
                    substitutions['clear_template'] = self.template_with_clear % substitutions
    
            return mark_safe(template % substitutions)
    

    and then just use the extended widget:

    business_image = forms.ImageField(widget=CustomClearableFileInput())
    
    0 讨论(0)
  • 2020-12-16 07:38

    This is my version of an imageInput that shows the image and doesn't allow for clearing the image

    Just have to specify in the form class that the field has widget=NonClearableImageInput()

    from django.forms.widgets import FileInput
    from django.utils.html import conditional_escape
    from django.utils.safestring import mark_safe
    
    class NonClearableImageInput(FileInput):
        def render(self, name, value, attrs=None):
            template = '%(input)s'
            data = {'input': None, 'url': None}
            data['input'] = super(NonClearableImageInput, self).render(name, value, attrs)
    
            if hasattr(value, 'url'):
                data['url'] = conditional_escape(value.url)
                template = '%(input)s <img src="%(url)s">'
    
            return mark_safe(template % data)
    
    0 讨论(0)
  • 2020-12-16 07:43

    I'd put this into the template form

    {% if business.business_image %}
       <img src="{{ business.business_image.url }}" title="{{ business.business_name}}" />
    {% endif %}
    
    0 讨论(0)
提交回复
热议问题