def bookCreate(request): template_name = "bookCreate.html" form = BookCreateForm() context = {} books = Book.objects.all() context["books"] = books context["form"] = form if request.method == "POST": form = BookCreateForm(request.POST) if form.is_valid(): # book_name=form.cleaned_data.get("book_name") # author=form.cleaned_data.get("author") # price=form.cleaned_data.get("price") # pages=form.cleaned_data.get("pages") # obj=Book(book_name=book_name,author=author,price=price,pages=pages) # obj.save() # qs=Book.objects.all() # context["books"]=qs # return render(request, template_name, context) form.save() return redirect("create") else: context["form"] = form return render(request, template_name, context) return render(request, template_name, context)
def bookcreate(request): print("INSIDE BOOKCREATE") # template_name = 'book_create.html' form = BookCreateForm() context = {'form': form} books = Book.objects.all() context['books'] = books if request.method == "POST": form = BookCreateForm(request.POST) if form.is_valid(): form.save() # book_name = form.cleaned_data.get("book_name") # author = form.cleaned_data.get("author") # price = form.cleaned_data.get("price") # pages = form.cleaned_data.get("pages") # # obj = Book(book_name = book_name, author = author, # price = price, pages = pages) # obj.save() qs = Book.objects.all() context['books'] = qs return render(request, 'book_create.html', context) else: context['form'] = form return render(request, 'book_create.html', context) return render(request, 'book_create.html', context)
def findbookview(request): if request.method == 'POST' and '_search' in request.POST: searchform = BookSearchForm(request.POST) if searchform.is_valid(): cd = searchform.cleaned_data findbooks = Books.objects.filter( Q(title=cd['title']) | Q(author=cd['author']) | Q(ISBN=cd['ISBN'])) if findbooks.count() == 0: if '_add' in request.POST: addbookconfirm = BookCreateForm(request.POST) c_d = addbookconfirm.cleaned_data Books.objects.create(title=c_d['title'], author=c_d['author'], ISBN=c_d['ISBN']) return render(request, 'book/added.html', {'addbookconfirm': addbookconfirm}) else: addbook = BookCreateForm() addbook.fields['title'].initial = cd['title'] addbook.fields['author'].initial = cd['author'] addbook.fields['ISBN'].initial = cd['ISBN'] return render(request, 'book/notfound.html', {'addbook': addbook}) else: return render(request, 'book/foundbook.html', {'findbooks': findbooks}) else: reverse(UserRegistrationView) searchform = BookSearchForm() return render(request, 'book/book.html', {'searchform': searchform})
class BookCreate(TemplateView): form_class = BookCreateForm() template_name = "books/book_form.html" context = {} def get(self, request, *args, **kwargs): form = BookCreateForm() self.context["form"] = form return render(request, self.template_name, self.context) def post(self, request, *args, **kwargs): form = BookCreateForm(request.POST) if form.is_valid(): form.save() return redirect("list")
def updatebook(request, pk): book = Book.objects.get(id=pk) form = BookCreateForm(instance=book) context = {} context["form"] = form if request.method == "POST": form = BookUpdate(instance=book, data=request.POST) if form.is_valid(): form.save() return redirect("listbook") return render(request, "updatebook.html", context)
def updatebooks(request, pk): book = Book.objects.get(id = pk) form = BookCreateForm(instance = book) context = {} context['form'] = form if request.method == 'POST': form = BookCreateForm(instance = book, data = request.POST) if form.is_valid(): form.save() return redirect('create') else: context['form'] = form return render(request, 'updatebooks.html', context) return render(request, 'updatebooks.html', context)
def bookcreate(request): template_name="create.html" form=BookCreateForm() context={} context["form"]=form if request.method=="POST": form=BookCreateForm(request.POST) if form.is_valid(): form.save() """ book_name=form.cleaned_data.get("book_name") author=form.cleaned_data.get("author") price=form.cleaned_data.get("price") pages=form.cleaned_data.get("pages") obj=Book(book_name=book_name,author=author,price=price,pages=pages) obj.save()""" return redirect("listbooks") else: context={} context["form"] = form return render(request, template_name, context) return render(request, template_name, context)
def post(self, request, *args, **kwargs): book = self.get_query_set(kwargs.get("pk")) form = BookCreateForm(instance=book, data=request.POST) if form.is_valid(): form.save() return redirect("list")
def get(self, request, *args, **kwargs): book = self.get_query_set(kwargs.get("pk")) form = BookCreateForm(instance=book) self.context["form"] = form return render(request, self.template_name, self.context)
def post(self, request, *args, **kwargs): form = BookCreateForm(request.POST) if form.is_valid(): form.save() return redirect("list")
def get(self, request, *args, **kwargs): form = BookCreateForm() self.context["form"] = form return render(request, self.template_name, self.context)