-
Notifications
You must be signed in to change notification settings - Fork 49
/
Copy pathviews.py
73 lines (61 loc) · 1.9 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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
from django.http import HttpResponseRedirect
from django.shortcuts import render
from django.urls import reverse
from .models import Student
from .forms import StudentForm
# Create your views here.
def index(request):
return render(request, 'students/index.html', {
'students': Student.objects.all()
})
def view_student(request, id):
return HttpResponseRedirect(reverse('index'))
def add(request):
if request.method == 'POST':
form = StudentForm(request.POST)
if form.is_valid():
new_student_number = form.cleaned_data['student_number']
new_first_name = form.cleaned_data['first_name']
new_last_name = form.cleaned_data['last_name']
new_email = form.cleaned_data['email']
new_field_of_study = form.cleaned_data['field_of_study']
new_gpa = form.cleaned_data['gpa']
new_student = Student(
student_number=new_student_number,
first_name=new_first_name,
last_name=new_last_name,
email=new_email,
field_of_study=new_field_of_study,
gpa=new_gpa
)
new_student.save()
return render(request, 'students/add.html', {
'form': StudentForm(),
'success': True
})
else:
form = StudentForm()
return render(request, 'students/add.html', {
'form': StudentForm()
})
def edit(request, id):
if request.method == 'POST':
student = Student.objects.get(pk=id)
form = StudentForm(request.POST, instance=student)
if form.is_valid():
form.save()
return render(request, 'students/edit.html', {
'form': form,
'success': True
})
else:
student = Student.objects.get(pk=id)
form = StudentForm(instance=student)
return render(request, 'students/edit.html', {
'form': form
})
def delete(request, id):
if request.method == 'POST':
student = Student.objects.get(pk=id)
student.delete()
return HttpResponseRedirect(reverse('index'))