learning django, and trying to make a todo list.
A task is created first with only a task.name. Then I go to a taskdetail.html, with a form for task.detail, task.duedate, and task.done. The latter is a boolean that is true if the task is completed. I have the following in my template:
<input type="hidden" name="done" value=0 /> <input type="checkbox" name="done" value=1 {% if task.done %} checked {% endif %}>
In my views.py, I have:
def taskdetail(request, task_id): task = Task.objects.get(pk=task_id) if request.method == 'POST': form = DetailTaskForm(request.POST) if form.is_valid(): task.name = form.cleaned_data["name"] task.description = form.cleaned_data["description"] task.done = form.cleaned_data["done"] task.duedate = form.cleaned_data["duedate"] task.save() ... Strange enough, task.done alsways ends up true. It's default is false, and I have verified that it is indeed false before task.done = form.cleaned_data["done"]
I read that somewhere that value=0 is the same as false, but that doesn't seem to work for me. I also tried
<input type="hidden" name="done" value="" /> but then my form.is_valid returns false.
What could be the problem here? (I am working with Django 2.1 and python 3.7)
EDIT1: Willem's answer put me on track, but my problem isn't completely solved yet. I have now set task.done default to False, and made it not a required field. I changed my template to:
<input type="hidden" name="done" value="" /> <input type="checkbox" name="done" value=1 {% if task.done %} checked {% endif %}> {% if task.done %} <input type="hidden" name="done" value="on" /> {% endif %} the {{task.done}} that I send to the form is False, and I don't see how it can be manipulated and set to True in the form. (only after POSTing) So instead of checking if task.done == True, I should be able to check if the checkbox has been ticked.