def insert(request): if request.method == 'POST': book = BookForm(request.POST) if book.is_valid(): cd = book.cleaned_data if len(Author.objects.filter(AuthorID=cd['AuthorID']))<=0: new_author=Author(AuthorID =cd['AuthorID'], Name =cd['Name'],\ Age =cd['Age'],Country =cd['Country']) new_author.save() new_book=Book(ISBN=cd['ISBN'],Title=cd['Title'],AuthorID=new_author,\ Publisher=cd['Publisher'],PublishDate=cd['PublishDate'],Price=cd['Price']) new_book.save() else: new_book=Book(ISBN=cd['ISBN'],Title=cd['Title'],AuthorID=Author.objects.get(AuthorID=cd['AuthorID']),\ Publisher=cd['Publisher'],PublishDate=cd['PublishDate'],Price=cd['Price']) new_book.save() return render_to_response('add.html', {'form': BookForm()}) else: book= BookForm() return render_to_response('add.html', {'form': book})
def addauthor(request): error = [] b = [] errorflag = True if "b0" in request.GET: b.append(request.GET["b0"]) if "b1" in request.GET: b.append(request.GET["b1"]) if "b2" in request.GET: b.append(request.GET["b2"]) for i in b: if not i: errorflag = False id = request.GET["id"] if errorflag: newauthor = Author(AuthorID=id, Name=b[0], Age=b[1], Country=b[2]) newauthor.save() return render_to_response("success.html") else: error.append("信息不完善哦") return render_to_response("addauthor.html", {"AuthorID": id, "error": error})