Example #1
0
def add_book(request):
    if request.POST:
        post = request.POST
        try:
            try:
                book = Author.objects.get(ISBN = post["ISBN"])
                return render_to_response("add_book.html", Context({"information":"Wrong ISBN!"}))
            except:
                try:
                    author = Author.objects.get(AuthorID = post["AuthorID"])
                except:
                    return render_to_response("add_book.html",Context({"information":\
                        "The author doesn't exist! Please add the author first"}))
                new_book = Book(
                    ISBN = post["ISBN"],
                    Title = post["Title"],
                    AuthorID = author,
                    Publisher = post["Publisher"],
                    PublishDate = post["PublishDate"],
                    Price = post["Price"])         
                new_book.save()
                b = Context({"author":author.Name,"book_list":author.book_set.all()})
                return render_to_response('search.html', b)
        except:
            return render_to_response("add_book.html", Context({"information":"Wrong information!"}))
    else:
        return render_to_response("add_book.html")
Example #2
0
def update(request):
    id = request.GET["id"]
    book = Book.objects.get(id=int(id))
    if request.POST:
        post = request.POST
        try:
            try:
                author = Author.objects.get(AuthorID = post["AuthorID"])
            except:
                return render_to_response("update.html",id,Context({"information":\
                    "The author doesn't exist! Please add the author first","book":book}))
            new_book = Book(
                ISBN = post["ISBN"],
                Title = post["Title"],
                AuthorID = author,
                Publisher = post["Publisher"],
                PublishDate = post["PublishDate"],
                Price = post["Price"])         
            new_book.save()
            book.delete()
            return render_to_response('detail.html',Context({"book":new_book}))
        except:
            return render_to_response("update.html",id, Context({"information":"Wrong information!","book":book}))
    return render_to_response("update.html",id,Context({"book":book}))