def save(request):

	library_form = LibraryForm(request.POST or None)

	if library_form.is_valid():

		form_clean = library_form.cleaned_data

		pk = form_clean.get('id', None)

		if not pk:
			library_model = Library(**form_clean)
			library_model.save()
		else:
			library_model = get_object_or_404(Library, pk=pk)

			library_model.title = form_clean.get('title', '')
			library_model.description = form_clean.get('description', '')
			library_model.status = form_clean.get('status', '')
			library_model.friend_name = form_clean.get('friend_name', '')
			library_model.friend_email = form_clean.get('friend_email', '')
			
			library_model.save()

		return HttpResponseRedirect(reverse('index'))

	book_list = Library.objects.all()

	return render(request, 'library/library.html', {'book_list': book_list, 'library_form': library_form})
Пример #2
0
    def post(self, request):
        user = User.objects.get(id=request.POST.get('user'))
        library = Library()

        library.title = request.POST.get('title')

        library.save()

        library.user.add(user)

        return redirect("library:libraries")