如何在django中将布尔值设置为False

时间:2016-01-13 11:01:34

标签: python html django

我在表单中有一个名为'hof'的复选框按钮,如果我单击表单中的复选框并发送,它可以工作,但如果我取消选中它拒绝发送,我得到的是这个字段是必需的。

这是我的模特

    package com.example.nileshkashid.secureactiveandroid;
import android.app.Application;
import com.activeandroid.ActiveAndroid;
public class MainApplication extends Application
{
    @Override
    public void onCreate() {
        super.onCreate();
        ActiveAndroid.initialize(this);
    }
}

此处如果我的视图

class CreateSingleSigner(models.Model):
    firstname = models.CharField(max_length = 32)
    middlename = models.CharField(max_length = 32)
    lastname = models.CharField(max_length = 32)
    displayname = models.CharField(max_length = 32)
    era = models.CharField(max_length = 11)
    category = models.CharField(max_length = 32)
    hof = models.BooleanField()
    externallink = models.CharField(max_length = 70)
    loaprice = models.FloatField(max_length = 10)
    basiccertprice = models.FloatField(max_length = 10)
    appraisalcost = models.FloatField(max_length = 10)
    notability = models.CharField(max_length = 32)

这是我的表格

def singlesigner(request):
    context = {}
    if request.method == 'POST':
        createsinglesigner_form = CreateSingleSignerForm(data=request.POST)
        if createsinglesigner_form.is_valid():

            createsinglesigner = createsinglesigner_form.save(commit=False)
            createsinglesigner.firstname = request.POST['firstname']
            createsinglesigner.middlename = request.POST['middlename']
            createsinglesigner.lastname = request.POST['lastname']
            createsinglesigner.displayname = request.POST['displayname']
            createsinglesigner.era = request.POST['era']
            createsinglesigner.category = request.POST['category']
            createsinglesigner.hof = request.POST['hof']
            createsinglesigner.externallink = request.POST['externallink']
            createsinglesigner.loaprice = request.POST['loaprice']
            createsinglesigner.basiccertprice = request.POST['basiccertprice']
            createsinglesigner.appraisalcost = request.POST['appraisalcost']
            createsinglesigner.notability = request.POST['notability']

            createsinglesigner_form.save()
        else:
                print createsinglesigner_form.errors
    else:
        # context['createsinglesigner'] = CreateSingleSigner()
        createsinglesigner_form =CreateSingleSignerForm()

    return render(request, "signer/singlesigner.html", {"createsinglesigner_form":createsinglesigner_form})

2 个答案:

答案 0 :(得分:2)

尝试更改表单

hof = forms.BooleanField(required=False, help_text = 'hall of fame')

hof = forms.BooleanField(initial=False, required=False, help_text = 'hall of fame')

答案 1 :(得分:0)

您不应该在表单上为#include <stdio.h> void main() { char input[100]; int i,j; int val = 0; printf("Type a String which will be converted to an Integer: "); scanf("%s",input); for(j=0; input[j] != '\0'; j++); // find string size for (i = 0; i < j; i++) { val = val * 10 + input[i] - 48; } } 设置初始值。

您还使用了BooleanField,没有必要在表单中重写模型的逻辑。

<强> forms.py:

ModelForm

如果您想坚持使用代码,请继承class CreateSingleSignerForm(forms.ModelForm): class Meta: model = CreateSingleSigner fields = ( 'hof', ... ) # Instantiate the object and override the required setting on hof field: def __init__(self, *args, **kwargs): super(CreateSingleSignerForm, self).__init__(*args, **kwargs) self.fields['hof'].required = True 而不是forms.Form,并指示Django保存复选框的未选中状态:

forms.ModelForm

hof = forms.BooleanField(required=True, help_text='Hall of Fame') 中为hof设置models.py的默认值也是一种很好的做法。