Django Advanced Learning Record

Source: Internet
Author: User
Preface : This blog adds to the previous blog Django step.

One

The front-end interface is simple (ugly) and has two functions:

    • Remove the title from the database eg: new book A

    • Enter the title in the form form, select the Publisher, select the author (multiple selection), and after entering, click Create new book submit, create the data in the database

Second, the realization

Let's start by implementing the first feature, which prints out the title of the page based on the database data.

1. Add URL routes

    URL (r ' ^book/', Views.book),

2. Define the book method in views.py

Django uses the Get method by default, which is to get the data, and if you want to create/modify data, such as the second function to be implemented later, you need to post.

def book (request):    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":p Ublisher_list,                                                     "authors": Author_list})


3. Create the book.html under TEMPLATES/APP01:

Books is a collection of objects for all books in the database, and the title can be displayed on the front page with a loop in HTML.


Next, implement the second function to create the data.

First look at the front-end HTML:

<form method= "POST" action= "/payment/book/" > {% csrf_token%} book        name:<input type= "text" name= "name"/ >        <select name= "publisher_id" >            {% for publisher in publishers%}                <option value= "{{ Publisher.id}} ">{{publisher.name}}</option>            {% endfor%}        </select>        <select name=" Author_ids "multiple=" multiple ">            {% for author in authors%}                <option value=" {{author.id}} ">{{ Author.first_name}}</option>            {% endfor%}        </select>        <div>            <input type= " Submit "value=" Create new book "/>        </div>    </form>

Attention:

    • Because it is the creation of data, so the submission method to use post, action= "/payment/book/" is a URL, indicating that the data submitted to the book method, the data encapsulated in the request parameter.

    • When you choose a publishing house, you want to upload the publisher name to the backstage? In fact, you just have to upload the selected ID to the background. So I add the Value property to the option tag to get the publisher's ID, and when you click Submit to commit the data, the ID in value is presented to the SELECT tag's Name property, and the Name property submits the data to the background.

    • You will find that the first line of HTML code has {% Csrf_token%}, what does this mean? I don't know ~_~ now, I'm going to get rid of this code, I can't submit the data !!

Then look at the backstage book method

def book (request): if Request.method = = "POST": #若是创建书的数据 print (Request. POST) Book_name = Request. Post.get ("name") publisher_id = Request. Post.get ("publisher_id") # Even if multiple authors are selected on the front page, only one value will be returned, only the last author's id #author_ids = Request. Post.get ("Author_ids") Author_ids = Request. Post.getlist ("Author_ids") #getlist can remove the ID of all authors #生成一个书的对象 New_book = models.        Book (name = book_name, publisher_id = publisher_id, publish_date = "2017-3-18") New_book.save () #同步到数据库 #new_book. Authors.add Add author New_book.authors.add (*author_ids) #author_id S is a list that needs to be preceded by a * conversion 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":p ublisher_list, "authors": Author_list}) 

When I enter the title in the front-end interface: Book A, select the second Publisher, select the 2nd and 3rd authors, in order to see, I print out in the background:

<querydict: {' name ': [' new book a '], ' csrfmiddlewaretoken ': [' V9odhsj10ofsq3rvi41tggns1w2vxwv '], ' publisher_id ': [' 2 '], ' Author_ids ': [' 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 results know that Author_ids is a list, when I add an author to a book, use the following code:

New_book.authors.add (*author_ids)

Why add * to the list? Not adding * will expose the wrong! Add * is to convert the list form ["2", "3"] to the Author ID form 2,3.

Login Admin Backstage to view the new book a Just created:

Related Article

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.