def bookCreate(request): form = BookForm() context = {} context["form"] = form template_name = "Book/book_create.html" if request.method == "POST": form = BookForm(request.POST) if form.is_valid(): bookname = form.cleaned_data["bookname"] author = form.cleaned_data["author"] pages = form.cleaned_data["pages"] price = form.cleaned_data["price"] category = form.cleaned_data["category"] obj = Book(bookname=bookname, author=author, pages=pages, price=price, category=category) obj.save() print(bookname, ",", author) books = Book.objects.all() context = {} context["book"] = books template_name = "Book/book_list.html" return render(request, template_name, context) else: context = {} context["form"] = form return render(request, template_name, context) return render(request, template_name, context)
def save_book(request): # print("In Save Book") print(request.POST) # <WSGIRequest: POST '/save-book/'> b_name = request.POST.get("name") b_author = request.POST.get("auth") b_price = request.POST.get("price") b_qty = request.POST.get("qty") b_pub = request.POST.get("pub") if b_pub == "on": flag = True else: flag = False if request.POST.get("id"): book_obj = Book.objects.get(id=request.POST.get("id")) book_obj.name = b_name book_obj.author = b_author book_obj.price = b_price book_obj.qty = b_qty book_obj.is_published = flag book_obj.save() return redirect('welcome') else: b = Book(name=b_name, author=b_author, qty=b_qty, price=b_price, is_published=flag) b.save() return redirect('welcome')
def save_book(request): print("In save book") print(request.POST) b_name = request.POST.get("name") b_auther = request.POST.get("auth") b_qty = request.POST.get("qty") b_price = request.POST.get("price") b_pub = request.POST.get("pub") # b_tr = request.POST.get("true") # b_fs = request.POST.get("false") # print(b_name, b_auther, b_qty, b_price, b_tr, b_fs) print(b_name, b_auther, b_qty, b_price, b_pub) if b_pub == "on": flag = True else: flag = False bk = Book(name=b_name, auther=b_auther, qty=b_qty, price=b_price, is_published=flag) # to save book in database ''' if b_tr =='on': flag = True elif b_fs == 'on': flag = False else: # raise Exception("Please select one option") return HttpResponse("Please select one option from True or False button") bk = Book(name = b_name, auther = b_auther, qty = b_qty, price = b_price, is_published = flag) # to save book in database ''' bk.save() return redirect("Welcome")
def save_book(request): logger.info(request.POST) b_name = request.POST.get("name") b_author = request.POST.get("author") b_price = request.POST.get("price") b_qty = request.POST.get("qty") b_pub = request.POST.get("pub") if b_pub == "on": flag = True else: flag = False if request.POST.get('id'): try: book_obj = Book.objects.get(id=request.POST.get('id')) except Exception as msg: logger.error(f"{msg} -- in exception") book_obj.name = b_name book_obj.author = b_author book_obj.qty = b_qty book_obj.price = b_price book_obj.is_publish = flag book_obj.save() return redirect('welcome') else: b = Book(name=b_name, author=b_author, qty=b_qty, price=b_price, is_publish=flag) b.save() return redirect('welcome')
def form_valid(self, form): if Book.objects.filter( ISBN=form.cleaned_data["ISBN"]).count() > 0: #title messages.warning( self.request, f'Book "{form.cleaned_data["ISBN"]}" already exist in database.' ) return redirect( "book_details", pk=Book.objects.get(ISBN=form.cleaned_data["ISBN"]).pk) else: new_book = Book( title=form.cleaned_data["title"].lower(), publication_date=form.cleaned_data['publication_date'], ISBN=form.cleaned_data["ISBN"], pages=form.cleaned_data["pages"], language=form.cleaned_data["language"]) authors = Author.objects.filter( pk__in=form.cleaned_data["authors"]) new_book.save() for author in authors: new_book.authors.add(author) new_book.save() pk = new_book.pk return redirect("covers_add", pk=pk) #
def form_valid(self, form): search_type = form.cleaned_data['search_type'] search_type = dict(form.fields['search_type'].choices)[search_type] value = str(search_type) + ":" + str(form.cleaned_data["search_phrase"]) param = {"q":value, "key":settings.API_KEY} api_url = settings.API_URL#api_url = "https://www.googleapis.com/books/v1/volumes" response = requests.get(url=api_url, params=param) items = response.json()["items"] for item in items: isbn = "" for element in item["volumeInfo"]["industryIdentifiers"]: if element["type"] == "ISBN_13": isbn = element["identifier"] if isbn == "": continue if Book.objects.filter(ISBN=isbn).count() > 0: continue new_book = Book( title=item["volumeInfo"].get("title"), publication_date=item["volumeInfo"].get("publishedDate", "0000"), pages=item["volumeInfo"].get("pageCount", 0), language=item["volumeInfo"].get("language"), ISBN=isbn ) new_book.save() new_cover = BookCovers(book=new_book) new_cover.save() if "imageLinks" in item["volumeInfo"]: new_cover.small_thumbnail = item["volumeInfo"]["imageLinks"].get("smallThumbnail") new_cover.big_thumbnail = item["volumeInfo"]["imageLinks"].get("thumbnail") new_cover.save() if "authors" not in item["volumeInfo"]: continue for author_name in item["volumeInfo"]["authors"]: new_author, created = Author.objects.get_or_create(name=author_name.lower()) if created: new_author.save() new_book.authors.add(new_author) new_book.save() return redirect(reverse_lazy("import_books"))
# l=[i for i in range(12,20)] # b=Book.objects.filter(id__in=l)#-----12 to 20 madile data print honar # print(b) # l=[i for i in range(12,20)] # b=Book.objects.all().exclude(id__in=l)#-----12 to 20 madile data print honar # print(b) # l=[i for i in range(12,20)] # b=Book.objects.exclude(id__in=l)#-----12 to 20 madile data print honar # print(b) l = [i for i in range(12, 20)] b = Book.objects.all().filter(id__in=l) #-----12 to 20 madile data print honar print(b) #-------bulk for loop peksya bulk kami velat run honar Book.objects.bulk_create([ Book(name="java", author="aaa", qty=30, price=300), Book(name="physics", author="fgh", qty=34, price=300), Book(name="chem", author="xcs", qty=56, price=300), Book(name="math", author="cds", qty=50, price=300), Book(name="mechanical", author="vfdd", qty=56, price=300) ]) Book.objects.filter(id__in=[41, 42, 43, 44, 45, 46]).update( price=900) #----only list madile value update karnesathi
def bookdb(request): book1 = Book(id='12345678', author='java', price=123, stock=234, sales=10) book1.save() return HttpResponse("<p>数据添加成功!</p>")