forked from UTSAVS26/PyVerse
-
Notifications
You must be signed in to change notification settings - Fork 0
/
views.py
53 lines (40 loc) · 1.58 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
43
44
45
46
47
48
49
50
51
52
53
from django.shortcuts import render, redirect, get_object_or_404
from django.views import View
from .models import Note
from .forms import NoteForm
def home(request):
return render(request, "home.html")
class NoteListView(View):
def get(self, request):
notes = Note.objects.all()
return render(request, "note_list.html", {"notes": notes})
class NoteCreateView(View):
def get(self, request):
form = NoteForm()
return render(request, "note_form.html", {"form": form})
def post(self, request):
form = NoteForm(request.POST)
if form.is_valid():
form.save()
return redirect("note_list")
return render(request, "note_form.html", {"form": form})
class NoteUpdateView(View):
def get(self, request, pk):
note = get_object_or_404(Note, pk=pk)
form = NoteForm(instance=note)
return render(request, "note_form.html", {"form": form})
def post(self, request, pk):
note = get_object_or_404(Note, pk=pk)
form = NoteForm(request.POST, instance=note)
if form.is_valid():
form.save()
return redirect("note_list")
return render(request, "note_form.html", {"form": form})
class NoteDeleteView(View):
def get(self, request, pk):
note = get_object_or_404(Note, pk=pk)
return render(request, "note_confirm_delete.html", {"note": note})
def post(self, request, pk):
note = get_object_or_404(Note, pk=pk)
note.delete()
return redirect("note_list")