-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathviews.py
158 lines (141 loc) · 4.5 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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
from django.shortcuts import render,redirect
from django.http import HttpResponse
from .models import Student,Coach
from django.views.generic import ListView, DetailView,CreateView,UpdateView,DeleteView
from .forms import StudentForm,StudentModelForm,CustomUserForm
from django.urls import reverse,reverse_lazy
from django.contrib.auth.views import LoginView
from django.contrib.auth.decorators import login_required
from django.contrib.auth.mixins import LoginRequiredMixin
from django.contrib.auth import authenticate,login
# Create your views here.
def homePage(request):
return HttpResponse ("<h1> Welcome To The ... </h1>")
@login_required
def student_list(request):
list =Student.objects.all() #activer l orm de django
return render(
request,
'hub/index.html',
{
'students': list,
}
)
class StudentListView(LoginRequiredMixin,ListView):
model= Student
template_name ="hub/index.html"
# paginate_by =2 #pour limité l'affichage en 2 etudiants seulement
class StudentDetailView(DetailView):
model= Student
def student_details(request,id):
student =Student.objects.get(id=id) #activer l orm de django
return render(
request,
'hub/st_details.html',
{
'student': student,
}
)
def coach_list(request):
listC =Coach.objects.all() #activer l orm de django
return render(
request,
'hub/index.html',
{
'coachs': listC,
}
)
def studentCreate(request):
#print(request)
if request.method == 'POST':
firstName=request.POST.get('first_name')
lastName = request.POST.get('last_name')
email = request.POST.get('email')
Student.objects.create(
first_name=firstName,
last_name=lastName,
email=email
)
return redirect('listStudent1')
return render(
request,
'hub/add_student.html'
)
def studentCreateForm(request):
form = StudentForm()
if request.method == 'POST':
form = StudentForm(request.POST)
if form.is_valid():
# Student.objects.create(
# first_name = form.cleaned_data.get('first_name'),
# last_name = form.cleaned_data.get('last_name'),
# email = form.cleaned_data['email']
# )
Student.objects.create(**form.cleaned_data)
return redirect('listStudent1')
#print(request)
return render(
request,
'hub/add_student.html',
{
'form':form
}
)
def add_Student(request):
form = StudentModelForm()
if request.method == 'POST':
form = StudentModelForm(request.POST)
if form.is_valid():
student = form.save(commit =False)
#traitement
student.save()
return redirect('listStudent1')
return render(
request,
'hub/add_student.html',
{
'form' : form
}
)
class StudentCreateView(CreateView):
model = Student
form_class = StudentModelForm
template_name = "hub/add_student.html"
def get_success_url(self):
return reverse('listStudent1')
class StudentUpdateView(UpdateView):
#obligatoire juste model+form_class
model = Student
form_class = StudentModelForm
template_name = "hub/add_student.html"
def get_success_url(self):
return reverse('listStudent1')
class StudentDeleteView(DeleteView):
model=Student
success_url = reverse_lazy('listStudent1')
#student_confirm_delete.html Pour le redirection
#2eme solution
# def student_delete(request, id) :
# student= Student.objects.get(id=id)
# student.delete()
# return redirect('listStudent1')
class LoginPage(LoginView):
template_name = "login.html"
fields ="__all__"
redirect_autheticated_user =True
success_url = reverse_lazy('listStudent1')
def register(request):
form = CustomUserForm()
if request.method == "POST":
form = CustomUserForm(request.POST)
if form.is_valid():
form.save()
username = form.cleaned_data['username']
password = form.cleaned_data.get('password')
user = authenticate(request, username=username, password=password)
if user is not None:
login(request, user)
return redirect('listStudent1')
else:
return redirect('login')
return render(request, "hub/register.html", {'form': form})