from django.shortcuts import HttpResponse, render, redirect from app01 import models # Create your views here. # show the list of publishers def publisher_list(request): # Go to the database to find out all the publishers, fill in the HTML, and return to the user ret = models.Publisher.objects.all().order_by("id") return render(request, "publisher_list.html", {"publisher_list": ret}) # add new publisher def add_publisher(request): error_msg = "" # If it is a POST request, I will get the data filled in by the user if request.method == "POST": new_name = request.POST.get("publisher_name", None) if new_name: # Create a new record in the database through ORM models.Publisher.objects.create(name=new_name) # Guide the user to visit the publisher list page to see if the addition is successful --> Jump return redirect("/publisher_list/") else: error_msg = "Publisher name cannot be empty!" # The first time the user comes, I return him an HTML page to fill in return render(request, "add_publisher.html", {"error": error_msg}) # delete the function of the publisher def delete_publisher(request): print(request.GET) print("=" * 120) # delete the specified data # 1. Get the ID value of the data to be deleted from the parameters of the GET request del_id = request.GET.get("id", None) # Dictionary value, if you can't marry, the default is None # If you can get the id value if del_id: # Go to the database to delete the data with the current id value # Find the data based on the id value del_obj = models.Publisher.objects.get(id=del_id) # delete del_obj.delete() # Return to the deleted page, jump to the publisher's list page, and check whether the deletion is successful return redirect("/publisher_list/") else: return HttpResponse("The data to be deleted does not exist!") # Editing Press def edit_publisher(request): # After the user has modified the name of the publisher, click the submit button and send me the new publisher name if request.method == "POST": print(request.POST) # Get the new publisher name edit_id = request.POST.get("id") new_name = request.POST.get("publisher_name") # update publisher # Get which publisher is the editor based on id edit_publisher = models.Publisher.objects.get(id=edit_id) edit_publisher.name = new_name edit_publisher.save() # commit the changes to the database # Jump to the publisher list page to see if the modification is successful return redirect("/publisher_list/") # Get the id parameter from the URL of the GET request edit_id = request.GET.get("id") if edit_id: # Get the publisher object of the current editor publisher_obj = models.Publisher.objects.get(id=edit_id) return render(request, "edit_publisher.html", {"publisher": publisher_obj}) else: return HttpResponse("Edited publisher does not exist!") # show the list of books def book_list(request): # Go to the database to query all books all_book = models.Book.objects.all() # Complete string replacement on HTML page (render data) return render(request, "book_list.html", {"all_book": all_book}) # delete books def delete_book(request): # Get the id value of the book to be deleted from the URL delete_id = request.GET.get("id") # Get data from URL # To delete the data in the database to delete the specified id models.Book.objects.get(id=delete_id).delete() # Return to the book list page to see if the deletion is successful return redirect("/book_list/") # add books def add_book(request): if request.method == "POST": print(request.POST) print("=" * 120) # {"book_title": "Learn to drive with Boss Jin", "publisher": 9} new_title = request.POST.get("book_title") new_publisher_id = request.POST.get("publisher") # Create a new book object and submit it automatically models.Book.objects.create(title=new_title, publisher_id=new_publisher_id) # Create with publisher object # publisher_obj = models.Publisher.objects.get(id=new_publisher_id) # models.Book.objects.create(title=new_title, publisher=publisher_obj) # Return to book list page return redirect("/book_list/") # Get all publishers ret = models.Publisher.objects.all() return render(request, "add_book.html", {"publisher_list": ret}) # edit books def edit_book(request): if request.method == "POST": # Take from the submitted data, the title of the book and the publisher associated with the book edit_id = request.POST.get("id") new_title = request.POST.get("book_title") new_publisher_id = request.POST.get("publisher") # renew edit_book_obj = models.Book.objects.get(id=edit_id) edit_book_obj.title = new_title # Update book title edit_book_obj.publisher_id = new_publisher_id # Update the publisher associated with the book # Commit the changes to the database edit_book_obj.save() # Return to the book list page to see if the editing is successful return redirect("/book_list/") # Return to a page for users to edit book information # Get the id value of the edited book edit_id = request.GET.get("id") # According to the id, go to the database to get the specific book object edit_book_obj = models.Book.objects.get(id=edit_id) print(edit_book_obj.id) print(edit_book_obj.title) print(edit_book_obj.publisher) # Get the publisher object associated with the current book object print(edit_book_obj.publisher_id) # Get the id value of the publisher associated with the current book object ret = models.Publisher.objects.all() return render( request, "edit_book.html", {"publisher_list": ret, "book_obj": edit_book_obj} ) # Author list def author_list(request): # author_obj = models.Author.objects.get(id=1) # print(author_obj.book.all()) # print("=" * 120) # Query all authors all_author = models.Author.objects.all() return render(request, "author_list.html", {"author_list": all_author}) # add author def add_author(request): if request.method == "POST": print("in post...") # Get the submitted data new_author_name = request.POST.get("author_name") # When the data submitted by post is multiple values, getlist must be used, such as multi-select checkbox and multi-select select books = request.POST.getlist("books") # create author new_author_obj = models.Author.objects.create(name=new_author_name) # Create a corresponding relationship between new authors and books and submit them automatically new_author_obj.book.set(books) # Jump to the author list page to see if the addition is successful! return redirect("/author_list/") # Query all books ret = models.Book.objects.all() return render(request, "add_author.html", {"book_list": ret}) # delete author def delete_author(request): # Get the author id to delete from the URL delete_id = request.GET.get("id") #Get the author object to be deleted according to the ID value and delete it directly # 1. Go to the author table and delete the author # 2. Go to the association table of author and book and delete the corresponding association record models.Author.objects.get(id=delete_id).delete() # Return to author list page return redirect("/author_list/") # edit author def edit_author(request): # If you are done editing, submit the data if request.method == "POST": # Get the edited data submitted edit_author_id = request.POST.get("author_id") new_author_name = request.POST.get("author_name") # Get the book information associated with the author after editing new_books = request.POST.getlist("books") # Find the currently edited author object by ID edit_author_obj = models.Author.objects.get(id=edit_author_id) # update the author's name edit_author_obj.name = new_author_name # Update the correspondence of the book associated with the author edit_author_obj.book.set(new_books) # Commit the changes to the database edit_author_obj.save() # Return to the author list page to see if the editing is successful return redirect("/author_list/") # Get the id information of the author to be edited from the URL edit_id = request.GET.get("id") # Find the author object to edit edit_author_obj = models.Author.objects.get(id=edit_id) # Query all book objects ret = models.Book.objects.all() return render(request, "edit_author.html", {"book_list": ret, "author": edit_author_obj}) def test(request): print(request.GET) print(request.GET.get("id")) return HttpResponse("OK") # Book review code def book_test(request): # Query all books # book_list = models.Book.objects.all() # # print(book_list) # for i in book_list: # print(i) # print(i.title) # print(i.publisher) # print(i.publisher.name) # print(i.publisher.addr) # print("=" * 20) # add new book # new_book_obj = models.Book.objects.create( # title="Name of new book", # publisher_id=10 # ) # add new book publisher_obj = models.Publisher.objects.get(id=10) new_book_obj = models.Book.objects.create( title="Name of the new book 2", publisher=publisher_obj ) print(new_book_obj) return HttpResponse("o98k")