-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathviews.py
35 lines (26 loc) · 944 Bytes
/
views.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
from django.shortcuts import render, get_object_or_404, redirect
from django.views import generic
from .models import Todo
from django.http import HttpResponseRedirect
class IndexView(generic.ListView):
template_name = 'todos/index.html'
context_object_name = 'todo_list'
def get_queryset(self):
"""Return all the latest todos."""
return Todo.objects.order_by('-created_at')
def add(request):
title = request.POST['title']
Todo.objects.create(title=title)
return redirect('todos:index')
def delete(request, todo_id):
todo = get_object_or_404(Todo, pk=todo_id)
todo.delete()
return redirect('todos:index')
def update(request, todo_id):
todo = get_object_or_404(Todo, pk=todo_id)
isCompleted = request.POST.get('isCompleted', False)
if isCompleted == 'on':
isCompleted = True
todo.isCompleted = isCompleted
todo.save()
return redirect('todos:index')