Django Advanced Learning Record

Source: Internet
Author: User
This article introduces the django Advanced Learning Record 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.

Book list:
 
 
    {% For book in books %}
  • {Book. name }}
  • {% Endfor %}
Next we will implement the second function to create data.

First, let's look at the front-end html:

 

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.

Def book (request): if request. method = "POST": # if the data of the created book is print (request. POST) book_name = request. POST. get ("name") publisher_id = request. POST. get ("publisher_id") # 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 # author_ids = request. POST. get ("author_ids") author_ids = request. POST. getlist ("author_ids") # getlist can retrieve the IDs of all authors # generate a book object new_book = models. book (name = book_name, publisher_id = publisher_id, publish_date = "2017-3-18") new_book.save () # synchronize to database # new_book.authors.add (1, 2) add author new_book.authors.add (* author_ids) # author_ids, add * to the front to convert to id print ("------- >>:", book_name, publisher_id, author_ids) books = models. book. objects. all () 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 })

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:

 
  
------- >>: New book A 2 ['2', '3'] ---->:
  
   
[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:

The above is the details of the django Advanced Learning Record. For more information, see other related articles in the first PHP community!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.