def addBook(request): if request.method == 'POST': temp_name = request.POST['name'] temp_author = request.POST['author'] temp_pub_house = request.POST['pub_house'] from django.utils import timezone temp_book = Book(name=temp_name, author=temp_author, pub_house=temp_pub_house, pub_date=timezone.now()) temp_book.save()
def add_book(request): if request.method != 'POST': return render(request, 'add.html') temp_name = request.POST['name'] temp_author = request.POST['author'] temp_pub_house = request.POST['pub_house'] temp_book = Book(name=temp_name, author=temp_author, pub_house=temp_pub_house, pub_date=timezone.now()) temp_book.save() return redirect(reverse('detail'))
def addBook(request): if request.method == 'POST': temp_name = request.POST['name'] temp_author = request.POST['author'] temp_pub_house = request.POST['pub_house'] from django.utils import timezone temp_book = Book(name=temp_name,author=temp_author, pub_house=temp_pub_house,pub_date=timezone.now()) temp_book.save() #重定向 return HttpResponseRedirect(reverse('detail'))
def addBook(request): '''添加书籍''' if request.method == 'POST': temp_id = request.POST['id'] temp_name = request.POST['name'] temp_author = request.POST['author'] temp_pub_house = request.POST['pub_house'] if temp_id == '': # 新增 temp_book = Book(name=temp_name, author=temp_author, pub_house=temp_pub_house, pub_date=timezone.now()) temp_book.save() else: # 修改 Book.objects.filter(id=temp_id).update(name=temp_name, author=temp_author, pub_house=temp_pub_house, pub_date=timezone.now()) # 重定向 return HttpResponseRedirect(reverse('detail'))