The content of this article is about the implementation steps of Django's library management system (with code). It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.
Add, Delete, Modify and Query of Publishers
Display the list of publishers:
1. Create a table structure:
2. Combine those two commands to create a press table
Create an HTML page to display the publishing house’s tablefor loop{% for i in ret %} ---- Template language
i refers to the object obtained from ret
{{ forloop.counter }} ; for the count from 1 to count
{{forloop.Counter0}}-& gt; for loop starting from 0
{ % endFor %}
Completed addition and display:
Create a function to delete a publishing house: that is, create a function to receive a request and process the request.
Edit publishing houseModify the information of the original publishing house, add the 'Edit' option to the original list, click this option to jump to the editing page.Create a function for editing the publishing house (and Complete the configuration) Modify and synchronize the database in the function
Create the edited HTML page
Add, delete, modify and query booksCreate a list of books in the database:Create the table structure class of books in models through those two commands in the database Create a table #Create a function that operates on the book list (and configure it in the url)Create an html page displayed on the front end Note: To operate the table in the function, you need to use the table structure class written in the ORM language. Get table information #The output Press object object is a publishing house object obtained through a foreign key. You can also print out the publishing house name: print(data[0].press. name)###
In addition, data[0].press_id can query the publisher ID associated with this book through foreign keys. During the process of creating the table structure in the class, creating foreign keys will automatically create foreign keys in the database. Associated id, the foreign key id at this time is in this table.
data[0].press.id can also be queried with this The publisher id associated with this book is queried through the join table. .press is the object of the publisher. .id can naturally obtain the publisher id related to data[0].
Add books
Create the operation function for adding books (configured in the url)
(Note: The name in the above picture is wrong, the books in the table structure The name title is title)
Create an add page
Delete books
Add a delete operation item in the book list, click the operation item Jump to the delete function to perform the deletion operation.
Create the operation function of the book to be deleted
Edit the book
In the book page, add the option of editing operations, select the book object to be edited, jump to the page to modify, and then save.
Create the operation function for editing books (and configure it):
Note: The 'return' at the end means that when the information has not been edited, you need to jump to the editing page first and follow the selected Edit the information of the good object. After the editing is completed, submit it.
Create the editing page:
Check the author’s deletions and modifications (many-to-many)
An author can write multiple books, and a book can also have multiple authors.
Create the relationship between the author and the book
Method 1:
# 作者: class Author(models.Model): id = models.AuthorField(primary_key=True) # 自增id主键 name = models.CharField(max_length=32) # 作者名字 # 创建作者和书籍的关系表 class Author2Book(models.Model): id = models.AuthorField(primary_key=True) author = models.Foreignkey(to='Author', on_delete=models.CASCADE) book = models.Foreignkey(to='Book', on_delete=models.CASCADE)
Method 2 (created with orm)
class Author(models.Model): id = models.AuthorField(primary_key=True) # 自增id主键 name = models.CharField(max_length=32) # 作者名字 books = models.ManyToManyField(to='Book')
Query: Find all author information in the database, and then display it on the page
Create the author function and configure it
##Create html file and display it on the page
Add: Create a function for the add operation, display the add page, obtain the user's added information, return it to the database, and then display the author information .Create a function to add operations and configure itCreate a page to obtain information
##Delete and edit
Add delete operation items in the author list and lock the objects to be deleted,
Create the operation function to be deleted,
##Add the editing operation in the author list, Lock the object to be edited
Create the editing function and configure it:
Create html page, displays the editing page and obtains the information filled in by the user
Note: template language, in judgment {% if book in author.books.all %} means the book If it is in the list of books associated with the author.
ORM Editing many-to-many cannot edit the third table directly. You must use the method provided by ORM:all(); add(id1, id2); set([id1, id2]); clear() clear Upload file创建上传文件的操作函数,并配置好
创建上传文件的html页面
enctype="multipart/form-data" 是文件操作必须要有的
The above is the detailed content of Implementation steps of Django's library management system (with code). For more information, please follow other related articles on the PHP Chinese website!