def create(request): # 默认增加一条数据到数据库 book = BookInfo(btitle='流星蝴蝶剑', bpub_date=date(1995, 12, 30)) book.save() # 页面重定向到首页 return redirect('/books/index/')
def convert_book_to_html (book_subdir, lang, book_name, book_text): lang=str(lang) dirname = os.path.dirname PROJECT_ROOT = dirname(dirname(os.path.realpath(__file__))) TEXT_DIR = os.path.join(PROJECT_ROOT, '..', 'texts') output_dir = os.path.join(TEXT_DIR, book_subdir, lang.upper()) if (os.path.exists(output_dir)==False): os.makedirs(output_dir) chapters=re.split("\[Chapter\]", book_text, flags=re.I) # chapters=book_text.split("[Chapter]") chap_num=1 for chapter in (chapters): if re.search("\w", chapter, re.UNICODE): # no blank chapters convert_chapter_to_html (output_dir, lang, book_name, chapter, chap_num) chap_num+=1 # add to db dj_book="" if (BookInfo.objects.filter( title="%s" % book_name, chaps="%d" % chap_num).exists() == False): dj_book = BookInfo(title="%s" % book_name, chaps="%d" % chap_num) dj_book.save() if (dj_book == ""): dj_book= BookInfo.objects.get(title="%s" % book_name) tran_lang = Languages.objects.get(abbr="%s" % lang.lower()) if (BookTranslation.objects.filter( book_id=dj_book, language_id=tran_lang).exists() == False): dj_tran = BookTranslation ( book_id=dj_book, language_id=tran_lang) dj_tran.save()
def post(self, request): bookName = request.POST.get("bookName") author = request.POST.get("author") type = request.POST.get("type") describe = request.POST.get("describe") book = BookInfo(bookName=bookName, author=author, type=type, describe=describe) book.save() message = "书籍创建成功" return shortcuts.success_response(message)
def main(): global books global BookInfo for book in books: if (BookInfo.objects.filter( title="%s" % book[0], author="%s" % book[1], chaps="%d" % book[2]).exists() == False): dj_book = BookInfo(title="%s" % book[0], author="%s" % book[1], chaps="%d" % book[2]) dj_book.save()
def update(self, request, pk): data = request.data try: book = BookInfo.objects(id=pk) except Exception: return Response('Eroor') res = BookValserializer(book, data=data) res.is_valid() # is_valid res.save() return Response(res.data)
def cud_c(request): book = BookInfo() book.btitle = '天龙八部' book.bpub_date = date.today() book.save() return redirect('book_list', permanent=True)
def book_info_test(): b = BookInfo() b.name = "三体" b.author = "刘慈欣" b.price = 80 b.pub_date = datetime.datetime() b.save() a = BookInfo.objects.get(id=1) print(a) a.price = 50 a.save() a2 = BookInfo.objects.get(id=1) print(a2)
def create(self, validated_data): """新建""" # 调用模型类 book = BookInfo(**validated_data) book.save() # 保持到数据库 return book