-
Notifications
You must be signed in to change notification settings - Fork 177
/
Copy pathviews.py
42 lines (35 loc) · 1.17 KB
/
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
36
37
38
39
40
41
42
from django.shortcuts import render, get_object_or_404, redirect
from .models import Todo
from .forms import TodoForm
# Todo List View
def todo_list(request):
todos = Todo.objects.all()
return render(request, 'todo_list.html', {'todos': todos})
# Todo Create View
def todo_create(request):
if request.method == 'POST':
form = TodoForm(request.POST)
if form.is_valid():
form.save()
return redirect('todo_list')
else:
form = TodoForm()
return render(request, 'todo_create.html', {'form': form})
# Todo Update View
def todo_update(request, pk):
todo = get_object_or_404(Todo, pk=pk)
if request.method == 'POST':
form = TodoForm(request.POST, instance=todo)
if form.is_valid():
form.save()
return redirect('todo_list')
else:
form = TodoForm(instance=todo)
return render(request, 'todo_update.html', {'form': form})
# Todo Delete View
def todo_delete(request, pk):
todo = get_object_or_404(Todo, pk=pk)
if request.method == 'POST':
todo.delete()
return redirect('todo_list')
return render(request, 'todo_delete.html', {'todo': todo})