Django advanced addition, django advanced
Preface: This blog adds to the previous blog's django advanced tutorial.
I,
The front-end interface is relatively simple (ugly) and has two functions:
- Extract the name from the database. For example, book
- In the form, enter the name of the book, select the publisher, select the author (select multiple), and click Create new book submit. Then, create data in the database.
II. Implementation
First, we will implement the first function to print the publishing name on the page based on the database data.
1. Add a url route
url(r'^book/', views.book),
2. Define the book method in views. py
Django uses the GET method by default to obtain data. If you want to create or modify data, such as the second feature to be implemented, you need to use the POST method.
Def book (request): books = models. book. objects. all () # Find all books publisher_list = models. publisher. objects. all () author_list = models. author. objects. all () print ("---->:", request) return render (request, "app01/book.html", {"books": books, "publishers": publisher_list, "authors": author_list })
3. Create book.html under templates/app01:
Books is the object set of all books in the database. You can use a loop in html to display the title on the front-end page.
1
Next we will implement the second function to create data.
First, let's look at the front-end html:
1 <form method = "post" action = "/payment/book/"> {% csrf_token %} 2 book name: <input type = "text" name = "name"/> 3 <select name = "publisher_id"> 4 {% for publisher in publishers %} 5 <option value = "{{ publisher. id }}" >{{ publisher. name }}</option> 6 {% endfor %} 7 </select> 8 <select name = "author_ids" multiple = "multiple"> 9 {% for author in authors %} 10 <option value = "{author. id }}" >{{ author. first_name }}</option> 11 {% endfor %} 12 </select> 13 <div> 14 <input type = "submit" value = "create new book"/> 15 </div> 16 </form>
Note:
- Because the data is created, the submission method must be post. action = "/payment/book/" is a url, indicating that the data is submitted to the book method, and the data is encapsulated in the request parameter.
- When you select a publishing house, You need to upload the publishing house name to the background ?? You only need to upload the selected id to the background. Therefore, when you click submit to submit data, the id in the value will be submitted to the name attribute of the select tag, the name attribute then submits the data to the background.
- You will find {% csrf_token %} In the first line of html code. What does this mean ~ _~, No data can be submitted if I remove this code !!
Let's look at the background book method.
1 def book (request): 2 if request. method = "POST": # If the data of the created book is 3 print (request. POST) 4 book_name = request. POST. get ("name") 5 publisher_id = request. POST. get ("publisher_id") 6 # If you select multiple authors on the front-end page, only one value is returned. You can only get the id of the Last author 7 # author_ids = request. POST. get ("author_ids") 8 author_ids = request. POST. getlist ("author_ids") # getlist can retrieve the IDs of all authors 9 10 # generate a book object 11 new_book = models. book (12 name = book_name, 13 publisher_id = publisher_id, 14 publish_date = "2017-3-18" 15) 16 new_book.save () # synchronize to database 17 18 # new_book.authors.add (1, 2) add author 19 new_book.authors.add (* author_ids) # author_ids as the list. Add * To id20 21 print ("------- >>:", book_name, publisher_id, author_ids) 22 23 books = models. book. objects. all () 24 publisher_list = models. publisher. objects. all () 25 author_list = models. author. objects. all () 26 27 print ("---->:", request) 28 return render (request, "app01/book.html", {"books": books, 29 "publishers ": publisher_list, 30 "authors": author_list })
When I enter the title: new book A on the front-end interface, select the second publisher, and select 2nd and 3rd authors, I printed them in the background for convenience:
<QueryDict: {'name': ['book a'], 'csrfmiddlewaretoken': ['v9odhsj10ofsq3rvi41tggns1w2vxwv '], 'her her _ id': ['2'], 'author _ id': ['2', '3']}> ------- >>: new book A 2 ['2', '3'] ---- >:< WSGIRequest: POST '/payment/book/'> [18/Mar/2017 14:06:23] "POST/payment/book/HTTP/1.1" 200 1335
According to the printed result, author_ids is a list. When I add an author to a book, use the following code:
new_book.authors.add(*author_ids)
Why do I add * before the list *? If you do not add *, an error is reported! * Is added to convert the list form ["2", "3"] To the Author id Form 2 and 3.
Log on to the admin background to view the new book: