How to exclude django form field from render but still keep it for validation?

前端 未结 4 466
悲&欢浪女
悲&欢浪女 2021-01-19 17:42

If I have a model (extract):

class Coupon(models.Model):
    image = models.ImageField(max_length=64, null=True, upload_to=\"couponimages\")
<
4条回答
  •  逝去的感伤
    2021-01-19 17:49

    I think the best way to achieve that is to create a widget that renders nothing:

    class NoInput(forms.Widget):
        input_type = "hidden"
        template_name = ""
    
        def render(self, name, value, attrs=None, renderer=None):
            return ""
    
    class YourForm(forms.Form):
        control = forms.CharField(required=False, widget=NoInput)
    

    This way you can still use {{form}} in your template.

提交回复
热议问题