# Checkbox booleanfield modelform in Django

**URL:** <https://forum.djangoproject.com/t/checkbox-booleanfield-modelform-in-django/6933>\
**Category:** Using Django\
**Created:** [March 8, 2021, 11:22am UTC](https://forum.djangoproject.com/t/checkbox-booleanfield-modelform-in-django/6933 "2021-03-08T11:22:50Z")\
**Posts on this page:** 18\
**Page:** 1

<div class="post-metadata">

**Author:** ![Myrodis](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/myrodis/32/3529_2.png) [@Myrodis](https://forum.djangoproject.com/u/Myrodis)\
**Post date:** [March 8, 2021, 11:22am UTC](https://forum.djangoproject.com/t/checkbox-booleanfield-modelform-in-django/6933/1 "2021-03-08T11:22:50Z")

</div>

So I’m making a to-do list and I made a booleanfield modelform which has attribute “complete”. I want that user to check it when it’s complete and I tried wriring **if task.complete == True** cross out the item and it didn’t work(it only worked when I checked it from the admin panel). Then I tried form.complete instead of task.complete and it doesn’t do anything.

models:

```auto
class Task(models.Model):
    title = models.CharField(max_length=200)
    complete = models.BooleanField(default=False)
    created = models.DateTimeField(auto_now_add=True)

    def __str__ (self):
        return self.title

```

forms:

```auto
from .models import *

class TaskForm(forms.ModelForm):
    title = forms.CharField(widget= forms.TextInput(attrs={'placeholder':'Add new task...'}))
    class Meta:
        model = Task
        fields = ' __all__'

```

views:

```auto
def index(request):
    tasks = Task.objects.order_by('-created')
    form = TaskForm()

    if request.method == 'POST':
        form = TaskForm(request.POST)
        if form.is_valid():
            form.save()
        return redirect('/')

    context = {
        'tasks': tasks,
        'form': form,
    }
    return render(request, 'to-do.html', context)

```

html:

```auto
<div class="main-container">
            <form method="POST" action="/">
                {% csrf_token %}
                <input type="submit"/>
                {{ form.title }}
            </form>
            {% for task in tasks %}
              {{ form.complete }}
              {% if form.complete == True %}
                <h1><strike>{{task.title}}</strike></h1>
              {% else %}
                <h1>{{task.title}}</h1>
              {% endif %}

            {% endfor %}
        </div>

```

 ![todoscreen](https://us1.discourse-cdn.com/flex026/uploads/djangoproject/original/2X/7/7e6c309b37aa4566bf1618b1f944ac44b012110f.png)

---

<div class="post-metadata">

**Author:** ![KenWhitesell](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kenwhitesell/32/280_2.png) [@KenWhitesell](https://forum.djangoproject.com/u/KenWhitesell)\
**Post date:** [March 8, 2021, 1:55pm UTC](https://forum.djangoproject.com/t/checkbox-booleanfield-modelform-in-django/6933/2 "2021-03-08T13:55:55Z")

</div>

> [@Myrodis](#):
>
> ```auto
> <form method="POST" action="/">
> {% csrf_token %}
> <input type="submit"/>
> {{ form.title }}
> </form>
> 
> ```

Your checkbox fields are outside the form, and so are not going to be submitted when the button is clicked. Only the fields between the `<form>` and `</form>` tags get submitted.

---

<div class="post-metadata">

**Author:** ![Myrodis](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/myrodis/32/3529_2.png) [@Myrodis](https://forum.djangoproject.com/u/Myrodis)\
**Post date:** [March 8, 2021, 3:19pm UTC](https://forum.djangoproject.com/t/checkbox-booleanfield-modelform-in-django/6933/3 "2021-03-08T15:19:18Z")

</div>

I tried doing it like this but it’s still not working

```auto
                {% for task in tasks %}
                  <form method="POST" action="/">
                    {% csrf_token %}
                    {{ form.complete }}
                  </form>

                    {% if form.complete == True %}
                      <h1><strike>{{task.title}}</strike></h1>
                    {% else %}
                      <h1>{{task.title}}</h1>
                    {% endif %}

                {% endfor %}

```

---

<div class="post-metadata">

**Author:** ![Myrodis](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/myrodis/32/3529_2.png) [@Myrodis](https://forum.djangoproject.com/u/Myrodis)\
**Post date:** [March 8, 2021, 3:32pm UTC](https://forum.djangoproject.com/t/checkbox-booleanfield-modelform-in-django/6933/4 "2021-03-08T15:32:27Z")

</div>

should I fix it in views? for example:

```auto
complete = form.cleaned_data['complete']
    if complete:
       **cross out the task**

```

but how should I do that?

---

<div class="post-metadata">

**Author:** ![KenWhitesell](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kenwhitesell/32/280_2.png) [@KenWhitesell](https://forum.djangoproject.com/u/KenWhitesell)\
**Post date:** [March 8, 2021, 3:35pm UTC](https://forum.djangoproject.com/t/checkbox-booleanfield-modelform-in-django/6933/5 "2021-03-08T15:35:38Z")

</div>

> [@Myrodis](#):
>
> ```auto
> {% for task in tasks %}
> <form method="POST" action="/">
> {% csrf_token %}
> {{ form.complete }}
> </form>
> 
> {% if form.complete == True %}
> <h1><strike>{{task.title}}</strike></h1>
> {% else %}
> <h1>{{task.title}}</h1>
> {% endif %}
> 
> {% endfor %}
> 
> ```

Now you’ve created a different problem. You’re creating one form for each task.

---

<div class="post-metadata">

**Author:** ![Myrodis](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/myrodis/32/3529_2.png) [@Myrodis](https://forum.djangoproject.com/u/Myrodis)\
**Post date:** [March 8, 2021, 3:51pm UTC](https://forum.djangoproject.com/t/checkbox-booleanfield-modelform-in-django/6933/6 "2021-03-08T15:51:51Z")

</div>

Take a look at the screenshot I posted with the question. I want tasks, which are already added to the list, to have the checkbox. If I check it, it should cross out. How else can I do that?

---

<div class="post-metadata">

**Author:** ![KenWhitesell](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kenwhitesell/32/280_2.png) [@KenWhitesell](https://forum.djangoproject.com/u/KenWhitesell)\
**Post date:** [March 8, 2021, 3:55pm UTC](https://forum.djangoproject.com/t/checkbox-booleanfield-modelform-in-django/6933/7 "2021-03-08T15:55:56Z")

</div>

You’ve got to fix your template.

You need to create one form, and within that form you need to render all the elements within that form.

Your non-template code that you posted at the top is ok - don’t change anything other than the template until you’ve got your template correct. If you need an example, review the [Django Tutorial](https://docs.djangoproject.com/en/3.1/intro/tutorial04/) page that shows basically what you’re trying to achieve here.

---

<div class="post-metadata">

**Author:** ![Myrodis](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/myrodis/32/3529_2.png) [@Myrodis](https://forum.djangoproject.com/u/Myrodis)\
**Post date:** [March 8, 2021, 4:45pm UTC](https://forum.djangoproject.com/t/checkbox-booleanfield-modelform-in-django/6933/8 "2021-03-08T16:45:40Z")

</div>

I copied it all into one form, but it still doesn’t get crossed, but the checkbox stays checked when I click and refresh.

```auto
              <form method="POST" action="/">
                      {% csrf_token %}
                      <input type="submit"/>
                      {{ form.title }}

                  {% for task in tasks %}
                      {% csrf_token %}
                      {{ form.complete }}

                      {% if task.complete == True %}
                        <h1><strike>{{task.title}}</strike></h1>
                      {% else %}
                        <h1>{{task.title}}</h1>
                      {% endif %}

                  {% endfor %}
                </form>
```

---

<div class="post-metadata">

**Author:** ![KenWhitesell](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kenwhitesell/32/280_2.png) [@KenWhitesell](https://forum.djangoproject.com/u/KenWhitesell)\
**Post date:** [March 8, 2021, 4:53pm UTC](https://forum.djangoproject.com/t/checkbox-booleanfield-modelform-in-django/6933/9 "2021-03-08T16:53:20Z")

</div>

If I made a guess here, I’d say you’re testing this with just one entry. You still have an issue of what you’re creating for your context in your view.

In your template, you’re rendering a variable named “form.complete”. How many instances of “form” are you supplying to the template?

---

<div class="post-metadata">

**Author:** ![Myrodis](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/myrodis/32/3529_2.png) [@Myrodis](https://forum.djangoproject.com/u/Myrodis)\
**Post date:** [March 8, 2021, 4:59pm UTC](https://forum.djangoproject.com/t/checkbox-booleanfield-modelform-in-django/6933/10 "2021-03-08T16:59:46Z")

</div>

I have “form.title” and “form.complete”

---

<div class="post-metadata">

**Author:** ![KenWhitesell](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kenwhitesell/32/280_2.png) [@KenWhitesell](https://forum.djangoproject.com/u/KenWhitesell)\
**Post date:** [March 8, 2021, 5:01pm UTC](https://forum.djangoproject.com/t/checkbox-booleanfield-modelform-in-django/6933/11 "2021-03-08T17:01:26Z")

</div>

Yes, those are the variables being rendered. But how many “form” are you supplying?

---

<div class="post-metadata">

**Author:** ![Myrodis](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/myrodis/32/3529_2.png) [@Myrodis](https://forum.djangoproject.com/u/Myrodis)\
**Post date:** [March 8, 2021, 5:02pm UTC](https://forum.djangoproject.com/t/checkbox-booleanfield-modelform-in-django/6933/12 "2021-03-08T17:02:07Z")

</div>

do you mean “form” html tags or form fields?

---

<div class="post-metadata">

**Author:** ![KenWhitesell](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kenwhitesell/32/280_2.png) [@KenWhitesell](https://forum.djangoproject.com/u/KenWhitesell)\
**Post date:** [March 8, 2021, 5:10pm UTC](https://forum.djangoproject.com/t/checkbox-booleanfield-modelform-in-django/6933/13 "2021-03-08T17:10:13Z")

</div>

Neither, I’m talking about instances of the object named “form”.

You’re passing a list of “task” objects named “tasks” into the template. So I’m asking how many “form” objects you’re sending in.

---

<div class="post-metadata">

**Author:** ![Myrodis](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/myrodis/32/3529_2.png) [@Myrodis](https://forum.djangoproject.com/u/Myrodis)\
**Post date:** [March 8, 2021, 5:26pm UTC](https://forum.djangoproject.com/t/checkbox-booleanfield-modelform-in-django/6933/14 "2021-03-08T17:26:10Z")

</div>

yeah I have list of tasks but should I give instance to form object too?

---

<div class="post-metadata">

**Author:** ![KenWhitesell](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kenwhitesell/32/280_2.png) [@KenWhitesell](https://forum.djangoproject.com/u/KenWhitesell)\
**Post date:** [March 8, 2021, 5:29pm UTC](https://forum.djangoproject.com/t/checkbox-booleanfield-modelform-in-django/6933/15 "2021-03-08T17:29:48Z")

</div>

To answer the question I posed, you’re only sending one “form” to the template.

If you have multiple tasks, you need a check box for each one - not using the same checkbox for all tasks, which does mean multiple “form” instances.

---

<div class="post-metadata">

**Author:** ![Myrodis](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/myrodis/32/3529_2.png) [@Myrodis](https://forum.djangoproject.com/u/Myrodis)\
**Post date:** [March 8, 2021, 5:33pm UTC](https://forum.djangoproject.com/t/checkbox-booleanfield-modelform-in-django/6933/16 "2021-03-08T17:33:56Z")

</div>

okay, but I don’t know how to do that

---

<div class="post-metadata">

**Author:** ![KenWhitesell](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kenwhitesell/32/280_2.png) [@KenWhitesell](https://forum.djangoproject.com/u/KenWhitesell)\
**Post date:** [March 8, 2021, 6:10pm UTC](https://forum.djangoproject.com/t/checkbox-booleanfield-modelform-in-django/6933/17 "2021-03-08T18:10:03Z")

</div>

Well, that gets into more details than what’s practical to provide in a forum like this.

Django specifically provides a facility for handling repeated instances of a form on a page - see  
[Formsets](https://docs.djangoproject.com/en/3.1/topics/forms/formsets/#formsets). However, I also know that formsets aren’t the easiest thing to understand if you’re not really comfortable with Django forms.

If you search on Google for a “Django To Do App”, you’re likely to find a number of tutorials and blogs on the topic - one or more of which might have some samples you can draw from.

---

<div class="post-metadata">

**Author:** ![Myrodis](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/myrodis/32/3529_2.png) [@Myrodis](https://forum.djangoproject.com/u/Myrodis)\
**Post date:** [March 8, 2021, 6:21pm UTC](https://forum.djangoproject.com/t/checkbox-booleanfield-modelform-in-django/6933/18 "2021-03-08T18:21:14Z")

</div>

yeah I’m just trying to get comfortable with forms so I’ll try something. thanks
