def book_update(request, pk, template_name='book_form.html'): book = get_object_or_404(Book, pk=pk) form = BookForm(request.POST or None, instance=book) if form.is_valid(): form.save() return redirect('book_list') return render(request, template_name, {'form': form})
def new(request): """docstring for new""" if request.method == "GET": form = BookForm(instance=Book()) return render_to_response("crud/templates/new.html", {"form":form}) else: form = BookForm(request.POST) if form.is_valid(): # All validation rules pass form.save() return redirect("/crud/books/list") else: return render_to_response("crud/templates/new.html", {"form":form, "error": "INVALID FORM REQUEST"})
def update(request, id): """docstring for update""" if request.method == 'GET': book = Book.objects.get(id=id) return render_to_response("crud/templates/update.html", {"book": book} ) else: if request.method == 'POST': # If the form has been submitted... form = BookForm(request.POST) if form.is_valid(): # All validation rules pass book = Book.objects.get(id=id) book.title = request.POST["title"] book.author = request.POST["author"] book.save() return redirect("/crud/list")
def book_create(request, template_name='book_form.html'): form = BookForm(request.POST or None) if form.is_valid(): form.save() return redirect('book_list') return render(request, template_name, {'form': form})