manuels-scolaires/manuels/views.py

39 lines
1.1 KiB
Python
Raw Normal View History

from django.shortcuts import get_object_or_404
from django.views.generic import CreateView, FormView, TemplateView
2018-05-21 19:28:36 +02:00
2018-05-21 22:23:53 +02:00
from manuels.forms import AddBookForm
from manuels.models import Teacher, Book
2018-05-21 21:03:08 +02:00
class HomePageView(CreateView):
model = Teacher
fields = ['first_name', 'last_name', 'phone_number']
2018-05-21 21:03:08 +02:00
template_name = 'manuels/home_page.html'
2018-05-21 22:23:53 +02:00
class AddBookView(CreateView):
model = Book
template_name = 'manuels/add_book.html'
2018-05-21 22:23:53 +02:00
form_class = AddBookForm
teacher = None
def dispatch(self, request, *args, **kwargs):
self.teacher = get_object_or_404(Teacher, pk=self.kwargs['id'])
return super().dispatch(request, *args, **kwargs)
def get_context_data(self, **kwargs):
context = super().get_context_data()
2018-05-21 22:23:53 +02:00
context['teacher'] = self.teacher
return context
2018-05-21 22:23:53 +02:00
def get_initial(self):
return {
'teacher': self.teacher
}
def get_form(self, form_class=None):
form = super().get_form(form_class)
form.fields['teacher'].queryset = Teacher.objects.filter(pk=self.teacher.pk)
return form