Django Multiple Form as dynamic checkbox

时间:2017-10-09 14:10:48

标签: python html django django-forms

希望有一个快速灵活的Django-esque解决方案来解决以下挑战,而不涉及JavaScript

目前,我创建了一个表单,将多对多字段显示为一个简单的倍数(Django中的内置行为)utalising以下内容:

Forms.py:

class ProfileForm(forms.ModelForm):
    class Meta:
        model = Profile
        exclude = ['email_address']

模板:

<form  method="post">{% csrf_token %}
    {{form}}            
    <button class="btn" type="submit">submit</button>
</form>

型号:

class Profile(models.Model):

    email_address = models.CharField(max_length = 100)

    subscriptions = models.ManyToManyField(Item, blank=True, null=True)

但是,为了减轻用户的体验(并提高移动性能),我想改为在每个旁边显示一个带复选框的列表。

如果没有JavaScript或手动设置forms.py?

中的每个选项,是否有一种简单的方法可以实现此目的

1 个答案:

答案 0 :(得分:1)

更简单的方法是在class Meta: model = Profile exclude = ['email_address'] widgets = { 'subscriptions': forms.CheckboxSelectMultiple, } import com.eviware.soapui.support.GroovyUtils import groovy.json.JsonOutput import groovy.json.JsonSlurper def response = messageExchange.response.responseContent def json = new JsonSlurper().parseText(response) def jsonFormat = (response).toString() def policies = [ [x: 28, xxx: 41, xxxxx: 1, name: 'Individual 18-50', aaa: true], [x: 31, xxx: 41, xxxxx: 1, name: 'Individual 51-60', aaa: true], [x: 34, xxx: 41, xxxxx: 1, name: 'Individual 61-75', aaa: true], [x: 37, xxx: 41, xxxxx: 1, name: 'Individual 76-85', aaa: false] ] log.warn json.policies log.error policies assert json.policies == policies

中为订阅提供小部件
json.policies