3

I am developing a django application. In my forms. I have a checkbox in my form. My forms submits fine when the box is checked, but when the box is unchecked, the form fails to submit. The field I am using is Boolean.

Here is my code:

#models.py

class Ingredient(models.Model):
    user = models.ForeignKey('auth.User')
    recipe_id = models.ForeignKey(Recipe, on_delete=models.CASCADE)
    title = models.CharField(max_length=500)
    instructions = models.CharField(max_length=500)
    rules = models.TextField(max_length=500,blank=True)
    primal = models.CharField(default='False',max_length=500)
    def __str__(self):
        return self.title

#views.py

def create_ingredient(request):
    form = IngredientForm(current_user=request.user)
    if request.method == 'POST':
           form = IngredientForm(request.POST, current_user=request.user)
           if form.is_valid():
               current_user = request.user
               data = form.cleaned_data
               ingredient_data=Ingredient.objects.create(user=current_user, recipe_id=data['recipe_id'], title=data['title'], primal=data['primal'], instructions=data['instructions'], rules=data['rules'])
               ingredient_data.save()
               ingredient = Ingredient.objects.get(pk = ingredient_data.pk)
               return redirect('ingredient_detail', pk=ingredient.pk)
           else:
               messages.error(request, "Error")
    return render(request, 'create_ingredient.html', {'form': form })


#in my template
....
<div class="form-group">
<div class="checkbox">
      <label><input type="checkbox" name="{{ form.primal.name }}" value="True" id="primal1">Primal</label>
</div>
</div>
....

Does anyone have a solution?

1 Answer 1

4

This is not about django but about html in general. This is your template:

<div class="form-group">
    <div class="checkbox">
        <label><input type="checkbox" name="{{ form.primal.name }}" value="True" id="primal1">Primal</label>
    </div>
</div>

Your checkbox, when unchecked, will not fly because it will not make a {{ form.primal.name }}=True in the url or post body.

To solve your problem, you should ensure a way to add {{ form.primal.name }}=False to the url. The standard solution involves a fixed additional field (a hidden one) like this:

<div class="form-group">
    <div class="checkbox">
        <input type="hidden" name="{{ form.primal.name }}" value="False" />
        <label><input type="checkbox" name="{{ form.primal.name }}" value="True" id="primal1">Primal</label>
    </div>
</div>

Which will generate a query string part like {{ form.primal.name }}=False if checkbox is unchecked, or {{ form.primal.name }}=False&{{ form.primal.name }}=True if checkbox is checked. In this case, only the latter occurrence counts, so you will have "True" when checked and "False" when unchecked.

Sign up to request clarification or add additional context in comments.

1 Comment

I tried your solution but its still showing me error

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.