Usage Crispy Form

linkaiyi
Follow

Render many to many field with checkbox with crsipy form

stackoverflow: https://stackoverflow.com/questions/43979278/crispy-form-render-manytomanyfield-as-checkboxes

class ContactFormChoices(models.Model):
    '''
    The contact form options to show
    '''
    text = models.CharField(max_length=256)
    active = models.BooleanField(default=True)

    def __unicode__(self):
        return self.text

class ContactData(models.Model):
    '''
    The contact data (when customer fills in the contact form,
     this is mailed and saved here
    '''
    name = models.CharField(max_length=256,help_text=_("Name"))
    phone_number= models.CharField(max_length=256,null=True,blank=True,default=None)
    email = models.EmailField(max_length=256,null=True,blank=True,default=None)
    choice = models.ManyToManyField(ContactFormChoices,blank=True,default=None)
    message = models.TextField()

    def __unicode__(self):
        return self.name

helper.layout = Layout(
        Field('name', css_class='input-xlarge'),
        Field('email', css_class='input-xlarge'),
        Field('phone_number', css_class='input-xlarge'),
        Field('message', rows="3", css_class='input-xlarge'),
        PrependedText('choice', ''),
        FormActions(
            Submit('submit', _('Submit'), css_class="btn-primary")
        )
    )
Object has 0 attachments

Was this article helpful?

This article is viewed 10 times!

Recent Viewed Articles

0 Comments

Leave a Comment

Support