Python - Django - Add author

In the bottom of the page book_list.html plus the "Add Author" link

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>作者列表</title>
</head>
<body>

<h1>作者列表</h1>

<table border="1">
    <thead>
    <tr>
        <th>#</th>
        <th>id</th>
        <th>名字</th>
        <th>书籍</th>
    </tr>
    </thead>
    <tbody>
        {% for author in author_list %}
            <tr>
            <td>{{ forloop.counter }}</td>
            <td>{{ author.id }}</td>
            <td>{{ author.name }}</td>
            <td>
                {% for book in author.book.all %}
                    {% if forloop.last %}
                        {{ book.title }}
                    {% else %}
                        {{ book.title }} |
                    {% endif %}
                {% endfor %}
            </td>
            </tr>
        {% endfor %}
    </tbody>
</table>

<a href="/add_author/">添加书籍</a>

</body>
</html>

running result:

Adding the "Add Author" at the url in urls.py

from django.conf.urls import url
from django.contrib import admin
from app01 import views

urlpatterns = [
    # 出版社
    url(r'^publisher_list/', views.publisher_list),
    url(r'^add_publisher/', views.add_publisher),
    url(r'^del_publisher/', views.del_publisher),
    url(r'^edit_publisher/', views.edit_publisher),
    # 书籍
    url(r'^book_list/', views.book_list),
    url(r'^add_book/', views.add_book),
    url(r'^del_book/', views.del_book),
    url(r'^edit_book/', views.edit_book),
    # 作者
    url(r'^author_list/', views.author_list),
    url(r'^add_author/', views.add_author),
]

Add author to create add_author.html page

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>添加作者</title>
</head>
<body>

<h1>添加作者</h1>

<form action="/add_author/" method="post">
    <p>
        作者姓名:<input type="text" name="author_name">
    </p>

    <p>
        书籍:
        <select multiple name="books">
            {% for book in book_list %}
                <option value="{{ book.id }}">{{ book.title }}</option>
            {% endfor %}
        </select>
    </p>

    <p>
        <input type="submit" value="提交">
    </p>
</form>

</body>
</html>

Finally the author plus add function views.py

Import the render django.shortcuts from, redirect, HttpResponse 
from app01 Import Models 


# Press to show a list of 
DEF publisher_list (Request): 
    Pass 


# to add a new press 
DEF add_publisher (Request): 
    Pass 


# delete Press 
DEF del_publisher (Request): 
    Pass 


# edit Press 
DEF edit_publisher (Request): 
    Pass 


# display a list of books 
DEF book_list (Request): 
    Pass 


# add books 
DEF add_book (Request): 
    Pass 


# delete books 
DEF del_book (Request): 
    Pass 


# edit books 
def edit_book (request) : 
    Pass 


# list of authors 
DEF author_list (Request): 
    # query all authors
    = models.Author.objects.all all_author () 
    return the render (Request, "author_list.html", { "author_list": all_author}) 


# OF adding 
DEF add_author (Request): 
    IF request.method == "POST": 
        # obtain data submitted 
        new_author_name = request.POST.get ( "author_name") 
        # If the submitted data is then more values with getlist 
        Books = request.POST.getlist ( "Books") 
        # create author 
        new_author_obj = models.Author.objects .create (name = new_author_name) 
        # new authors and books to establish a correspondence between automatically submitted 
        new_author_obj.book.set (Books) 
        # Jump to the list of authors page to see if adding a successful 
        return redirect ( "/ author_list /") 
    # inquiry All books 
    all_books = models.Book.objects.all()
    return render(request, "add_author.html", {"book_list": all_books})

operation result:

Submitted after

Look again at the database

 

Guess you like

Origin www.cnblogs.com/sch01ar/p/11240489.html