[Answer]-Adding in a custom checkbox to a django form

1👍

You need to add a field to a ModelForm, then you have in forms.py:

class MyForm(forms.ModelForm):
   userSwearsTheyKnowWhatTheyAreDoing = forms.BooleanField()

   class Meta:
      model = MyObject

and in your views.py:

...
myform = MyForm(request.POST)
if len(similar) == 0 or myform.cleaned_data['userSwearsTheyKnowWhatTheyAreDoing']:
   newObj = myforms.save() 
   messages.success(request,"New Object Saved")
   return HttpResponseRedirect('/object/%d'% newObj.pk) # Redirect after POST
...
👤geoom

0👍

The Django form does not get created and defined by whatever you are doing in the template. Instead you have to define your form with pyton by doing something like

class MyForm(forms.Form):
    userSwearsTheyKnowWhatTheyAreDoing = forms.BooleanField()
    ...

So at lease you need to add it there. That is exaclty what the error message is telling you.

Leave a comment