Django version=1.10.2 and python 2.7 Im learning django and trying to clone to-do list item like this Here is the models file for todo:-
class Todo(models.Model):
title = models.CharField(max_length=200)
# text = models.TextField()
completed = models.BooleanField(null=False,default=False)
created_at = models.DateTimeField(auto_now_add=True)
def __str__(self):
return self.title #this makes djangoadmin page show title inthe list
The views file
from django.shortcuts import render
from models import Todo
def index(request):
todos = Todo.objects.all()
context = {
'todos':todos
}
if request.method == 'POST':
title = request.POST['title']
todo = Todo(title=title)
todo.save()
return render(request,'index.html',context)
else:
return render(request,'index.html',context)
def show_completed(request): #show completed task only
todos = Todo.objects.filter(completed=True)
context = {
'todos': todos
}
return render(request, 'index.html', context)
def show_active(request): #show active task list
todos = Todo.objects.filter(completed=False)
context = {
'todos': todos
}
return render(request, 'index.html', context)
def clear_completed(request): #Delete the completed tasks
Todo.objects.filter(completed=True).delete()
todos = Todo.objects.all()
context = {
'todos': todos
}
return render(request, 'index.html', context)
def save_state(request):
pass
The template file "index.html"
<!DOCTYPE html>
<html lang="en">
<head>
</head>
<body>
<h3>Todo List:</h3><hr>
<form method="post" action="{% url 'index' %}">
{% csrf_token %}
<input type="text" name="title" id="title"/>
<input type="submit" value="submit" />
</form>
<hr><br>
<form method="post" action="{% url 'save_state'%}">
{% csrf_token %}
<ul>{% for todo in todos %}
<li> <input type="checkbox" name="completed" value="True" {% if todo.completed is True %} checked = "checked" {% endif %} >{{ todo.title }} </li>
{% endfor %}
</ul>
<input type="submit" value="Submit">
</form>
<a href="/todos/">All</a>
<a href="/todos/active">Active</a>
<a href="/todos/completed">Completed</a>
<a href="/todos/clear_completed">ClearCompleted</a>
</body>
</html>
I want to know how to get the checkbox's of todo items and save it if the checkbox is checked by passing those to view called "save_state"