Display book form

This commit is contained in:
Gabriel Augendre 2018-05-21 22:23:53 +02:00
parent 63fd65fb5e
commit 5a5eefa134
4 changed files with 42 additions and 4 deletions

View file

@ -0,0 +1,9 @@
from django import forms
from manuels.models import Book, Teacher
class AddBookForm(forms.ModelForm):
class Meta:
model = Book
fields = '__all__'

View file

@ -48,4 +48,4 @@ class Book(models.Model):
publication_year = models.PositiveIntegerField('année de publication') publication_year = models.PositiveIntegerField('année de publication')
isbn = models.TextField('ISBN/EAN', validators=[isbn_validator]) isbn = models.TextField('ISBN/EAN', validators=[isbn_validator])
price = models.PositiveIntegerField('prix') price = models.PositiveIntegerField('prix')
previously_acquired = models.BooleanField("manuel acquis précédemment par l'élève") previously_acquired = models.BooleanField("manuel acquis précédemment par l'élève", blank=True)

View file

@ -10,4 +10,15 @@
Bienvenue {{ teacher.full_name }} Bienvenue {{ teacher.full_name }}
</div> </div>
</div> </div>
<div class="row">
<div class="col-12">
<form action="#" method="post" class="form">
{% csrf_token %}
{% bootstrap_form form %}
{% buttons %}
<button type="submit" class="btn btn-primary">Submit</button>
{% endbuttons %}
</form>
</div>
</div>
{% endblock %} {% endblock %}

View file

@ -1,7 +1,8 @@
from django.shortcuts import get_object_or_404 from django.shortcuts import get_object_or_404
from django.views.generic import CreateView, FormView, TemplateView from django.views.generic import CreateView, FormView, TemplateView
from manuels.models import Teacher from manuels.forms import AddBookForm
from manuels.models import Teacher, Book
class HomePageView(CreateView): class HomePageView(CreateView):
@ -10,11 +11,28 @@ class HomePageView(CreateView):
template_name = 'manuels/home_page.html' template_name = 'manuels/home_page.html'
class AddBookView(TemplateView): class AddBookView(CreateView):
model = Book
template_name = 'manuels/add_book.html' template_name = 'manuels/add_book.html'
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): def get_context_data(self, **kwargs):
context = super().get_context_data() context = super().get_context_data()
context['teacher'] = get_object_or_404(Teacher, pk=self.kwargs['id']) context['teacher'] = self.teacher
return context return context
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