Django練習——圖書管理系統

Django圖書管理系統

建立一個項目

1. django-admin startproject 圖書管理
2. cmd 命令終端下建立一個app
    python manage.py startapp app01

配置settings.py文件(此處填寫了後續須要配置的全部內容)

一、註釋該行內容(大約在47~57行)
 #   'django.middleware.csrf.CsrfViewMiddleware',
2、鏈接數據庫的配置(註釋掉原來的 DATABASES)
DATABASES = {
'default': {
    'ENGINE': 'django.db.backends.mysql',
    'NAME': "book_m_s",
    'HOST': "127.0.0.1",
    'PORT': 3306,
    'USER': 'root',
    'PASSWORD': '123456',
    }
}
3、靜態文件的配置
STATIC_URL = '/static/'
STATICFILES_DIRS = [
    os.path.join(BASE_DIR, "static")
]
4、app添加
INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'app01.apps.App01Config',
]

__init__.py文件,引入pymysql模塊,使用mysql數據庫

import pymysql
pymysql.install_as_MySQLdb()

app01/modles.py文件,建立數據表

from django.db import models
# Create your models here.
# 出版社表
class Publisher(models.Model):
    # 自增的ID主鍵
    id = models.AutoField(primary_key=True)
    # 建立一個varchar(64)的惟一的不爲空的字段,且不能爲空,且不重複
    name = models.CharField(max_length=64, null=False, unique=True)

# 書籍表
class Book(models.Model):
    # 自增的ID主鍵
    id = models.AutoField(primary_key=True)
    # 建立一個varchar(64)的惟一的不爲空的字段,且不能爲空,且不重複
    title = models.CharField(max_length=64, null=False, unique=True)
    # 告訴ORM我這張表和Publisher表是關聯關係,ORM自動幫我建立關聯ID (publisher_id)字段
    publisher = models.ForeignKey(to="Publisher")

# 做者表
class Author(models.Model):
    # 自增的ID主鍵
    id = models.AutoField(primary_key=True)
    # 建立一個varchar(64)的惟一的不爲空的字段,且不能爲空,且不重複
    name = models.CharField(max_length=16, null=False, unique=True)
    # 告訴ORM 我這張表和book表是多對多的關聯關係,ORM自動幫我生成了第三張表
    book = models.ManyToManyField(to="Book")

urls.py文件,配置路由

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

urlpatterns = [
    url(r'^admin/', admin.site.urls),
    # 出版社相關的對應關係
    # 出版社列表
    url(r'^publisher_list/', views.publisher_list),
    # 添加出版社
    url(r'^add_publisher/', views.add_publisher),
    # 刪除出版社
    url(r'^delete_publisher/', views.delete_publisher),
    # 編輯出版社
    url(r'^edit_publisher/', views.edit_publisher),

    # 書籍相關的對應關係
    # 書籍列表
    url(r'^book_list/', views.book_list),
    # 添加書籍
    url(r'^add_book/', views.add_book),
    # 刪除書籍
    url(r'^delete_book/', views.delete_book),
    # 編輯書籍
    url(r'^edit_book/', views.edit_book),

    # 做者相關的對應關係
    # 做者列表
    url(r'^author_list/', views.author_list),
    # 添加做者
    url(r'^add_author/', views.add_author),
    # 刪除做者
    url(r'^delete_author/', views.delete_author),
    # 編輯做者
    url(r'^edit_author/', views.edit_author),
]

app01/views.py文件

from django.shortcuts import render, redirect, HttpResponse
    from app01 import models
    # Create your views here.

# 展現出版社列表
def publisher_list(request):
    # 去數據庫查出全部的出版社,填充到HTML中,給用戶返回
    ret = models.Publisher.objects.all().order_by("id")
    return render(request, 'publisher_list.html', {"publisher": ret})

# 添加新的出版社
def add_publisher(request):
    error = ""
    # 若是是POST請求,我就取到用戶填寫的數據
    if request.method == "POST":
        add_publisher = request.POST.get("add_name")
        if add_publisher:
            # 從數據庫中獲取因此的出版社
            all_publisher = models.Publisher.objects.all()
            # 循環判斷新添加的出版社名字是否已經存在
            for i in all_publisher:
                # 若是存在返回錯誤提示
                if i.name == add_publisher:
                    error = "%s 已經存在" % (add_publisher)
                    return render(request,'add_publisher.html', {"error": error})
            # 經過ORM去數據庫裏建立一條記錄
            models.Publisher.objects.create(name=add_publisher)
            # 引導用戶訪問出版社列表頁,查看是否添加成功 ———> 跳轉
            return redirect("/publisher_list/")
        else:
            error = "error:出版社名字不能爲空 !"
    # 用戶第一次來,我給他返回一個用來填寫的HTML頁面
    return render(request,'add_publisher.html', {"error": error})

# 刪除出版社
def delete_publisher(request):
    # 刪除指定的數據
    # 1. 從GET請求的參數裏面拿到將要刪除的ID值
    del_id = request.GET.get("id") # 字典取值,取不到默認爲None
    # 若是取到id值
    if del_id:
        # 去數據庫刪除當前的id值的數據
        # 1.根據id值查找到數據並進行刪除
        models.Publisher.objects.get(id=del_id).delete()
        # 上面這句刪除還能夠用這種方式以下:
        # del_obj = models.Publisher.objects.get(id=del_id)
        # del_obj.delete()
        # 返回刪除後的頁面,跳轉到出版社的列表頁,查看刪除是否成功
        return redirect("/publisher_list/")
    else:
        return HttpResponse('<h1 style="color: red">ERROR : 刪除的出版社不存在 !</h1>')

# 編輯出版社
def edit_publisher(request):
    # 用戶修改完出版社的名字,點擊提交按鈕,給我發來新的出版社名字
    if request.method == "POST":
        # 取新出版社的名字
        edit_id = request.POST.get("id")
        edit_newname = request.POST.get("name")
        # 更新出版社(數據庫)
        edit_publisher = models.Publisher.objects.get(id=edit_id)
        edit_publisher.name = edit_newname
        edit_publisher.save()  # 把修改提交到數據庫
        # 跳轉到出版社列表頁,查看是否修改爲功
        return redirect("/publisher_list/")
        # 根據id取到編輯的是哪一個出版社
    # 從GET請求的URL中取到id參數
    edit_id = request.GET.get("id")
    if edit_id:
        # 獲取到當前編輯的出版社對象
        edit_obj = models.Publisher.objects.get(id=edit_id)
        return render(request, "edit_publisher.html", {"publisher": edit_obj})
    else:
        return HttpResponse('<h1 style="color: red">ERROR : 編輯的出版社不存在 !</h1>')

# 展現書籍
def book_list(request):
    # 去數據庫中查詢全部的書籍
    all_book = models.Book.objects.all().order_by("id")
    # 在HTML頁面完成字符串替換(渲染書籍)
    return render(request, "book_list.html", {"all_book": all_book})

# 添加書籍
def add_book(request):
    error = ""
    if request.method == "POST":
        # 獲取新書的名字
        add_name = request.POST.get("book_name")
        # 獲取新書的出版社
        publisher = request.POST.get("publisher_id")
        if add_name:
            # 建立新書對象, 自動提交   (建立方法一)
            models.Book.objects.create(title=add_name, publisher_id=publisher)
            # 返回到書籍列表頁
            return redirect("/book_list/")
        else:
            error = "error:書名不能爲空!"
    # 取到全部的出版社
    ret = models.Publisher.objects.all()
    return render(request, "add_book.html", {"publisher_list": ret, "error": error})

# 刪除書籍
def delete_book(request):
    delete_id = request.GET.get("id")
    if delete_id:
        # 去刪除數據庫中刪除指定id的數據
        models.Book.objects.get(id=delete_id).delete()
        # 返回書籍列表頁面,查看是否刪除成功
        return redirect("/book_list")
    else:
        return HttpResponse('<h1 style="color: red">ERROR : 刪除的書籍不存在 !</h1>')

# 編輯書籍
def edit_book(request):
    # 從URL裏面獲取要刪除的書籍的id值
    if request.method == "POST":
        edit_id = request.POST.get("id")
        if edit_id:
            # 從提交的數據裏面取,書名和書名關聯的出版社
            new_publisher_id = request.POST.get("publisher")
            new_name = request.POST.get("book_name")
            # 更新
            edit_book_obj = models.Book.objects.get(id=edit_id)
            edit_book_obj.title = new_name   # 更新書名
            edit_book_obj.publisher_id = new_publisher_id   # 更新書籍關聯的出版社
            # 將修改提交到數據庫
            edit_book_obj.save()
            # 返回書籍列表頁,查看是否編輯成功
            return redirect("/book_list/")
    # 取到編輯的書的id值
    edit_id = request.GET.get("id")
    if edit_id:
        publisher_list = models.Publisher.objects.all()
        edit_obj = models.Book.objects.get(id=edit_id)
        # 返回一個頁面,讓用戶編輯書籍信息
        return render(request, "edit_book.html", {"book_obj": edit_obj, "publisher_list": publisher_list})
    else:
        return HttpResponse('<h1 style="color: red">ERROR : 編輯的書籍不存在 !</h1>')

# 做者列表
def author_list(request):
    # 查詢全部的做者
    author_obj = models.Author.objects.get(id=1)
    print(author_obj)
    all_author = models.Author.objects.all().order_by("id")
    return render(request, "author_list.html", {"author_list":all_author})

# 添加做者
def add_author(request):
    error = ""
    if request.method == "POST":
        # 取到提交的數據
        new_author_name = request.POST.get("author_name")
        if new_author_name:
            # post提交的數據是多個值的時候,必定要用getlist , 如多選的checkbox和多選的select
            books = request.POST.getlist("books")
            # 建立做者
            new_author_obj = models.Author.objects.create(name=new_author_name)
            # 把新做者和書籍創建對應關係,自動提交
            new_author_obj.book.set(books)
            # 跳轉到做者列表頁面,查看是否添加成功
            return redirect("/author_list/")
        else:
            error = "error:做者不能爲空!"
    # 查詢全部的書籍
    ret = models.Book.objects.all()
    return render(request, "add_author.html", {"book_list": ret, "error": error})

# 刪除做者
def delete_author(request):
    # 從URl中獲取須要刪除的做者的ID值
    delete_id = request.GET.get("id")
    if delete_id:
        # 若是獲取到值,那麼進行刪除
        models.Author.objects.get(id=delete_id).delete()
        # 跳轉到做者列表頁,查看是否刪除成功
        return redirect("/author_list/")
    else:
        return HttpResponse('<h1 style="color: red">ERROR : 刪除的做者不存在 !</h1>')

# 編輯做者
def edit_author(request):
    # 若是編輯完提交數據過來
    if request.method == "POST":
        # 拿到提交過來的編輯後的數據
        edit_author_id = request.POST.get("author_id")
        new_author_name = request.POST.get("author_name")
        # 拿到編輯後做者關聯的書籍信息
        new_books = request.POST.getlist("books")
        # 根據ID找到當前編輯的做者對象
        edit_author_obj = models.Author.objects.get(id=edit_author_id)
        # 更新做者的名字
        edit_author_obj.name = new_author_name
        # 更新做者關聯的書的對應關係
        edit_author_obj.book.set(new_books)
        # 將修改提交到數據庫
        edit_author_obj.save()
        # 返回做者列表頁,查看是否編輯成功
        return redirect("/author_list/")
    # 從URL裏面取要編輯的做者的id信息
    edit_id = request.GET.get("id")
    # 找到要編輯的做者對象
    edit_author_obj = models.Author.objects.get(id=edit_id)
    # 查詢全部的書籍對象
    ret = models.Book.objects.all()
    return render(request, "edit_author.html", {"book_list": ret, "author": edit_author_obj})
views.py

HTML文件:出版社、書籍、做者

展現出版社

<!DOCTYPE html>
<!-- saved from url=(0042)https://v3.bootcss.com/examples/dashboard/ -->
<html lang="zh-CN">
<head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge">
        <meta name="viewport" content="width=device-width, initial-scale=1">
        <!-- 上述3個meta標籤*必須*放在最前面,任何其餘內容都*必須*跟隨其後! -->
        <meta name="description" content="">
        <meta name="author" content="">
        <link rel="icon" href="https://v3.bootcss.com/favicon.ico">
        <title>出版社列表</title>
        <!-- Bootstrap core CSS -->
        <link href="/static/bootstrap/css/bootstrap.min.css" rel="stylesheet">
        <!-- Custom styles for this template -->
        <link href="/static/dashboard.css" rel="stylesheet">
        <link rel="stylesheet" href="/static/fontawesome/css/font-awesome.min.css">
</head>
<body>
    <nav class="navbar navbar-inverse navbar-fixed-top">
        <div class="container-fluid">
            <div class="navbar-header">
                <button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#navbar"
                        aria-expanded="false" aria-controls="navbar">
                    <span class="sr-only">Toggle navigation</span>
                    <span class="icon-bar"></span>
                    <span class="icon-bar"></span>
                    <span class="icon-bar"></span>
                </button>
                <a class="navbar-brand" href="https://v3.bootcss.com/examples/dashboard/#">BMS-S10</a>
            </div>
            <div id="navbar" class="navbar-collapse collapse">
                <ul class="nav navbar-nav navbar-right">
                    <li><a href="https://v3.bootcss.com/examples/dashboard/#">Dashboard</a></li>
                    <li><a href="https://v3.bootcss.com/examples/dashboard/#">Settings</a></li>
                    <li><a href="https://v3.bootcss.com/examples/dashboard/#">Profile</a></li>
                    <li><a href="https://v3.bootcss.com/examples/dashboard/#">Help</a></li>
                </ul>
                <form class="navbar-form navbar-right">
                    <input type="text" class="form-control" placeholder="Search...">
                </form>
            </div>
        </div>
    </nav>
    <div class="container-fluid">
        <div class="row">
            <div class="col-sm-3 col-md-2 sidebar">
                <ul class="nav nav-sidebar">
                    <li class="active"><a href="/publisher_list/">出版社列表</a></li>
                    <li><a href="/book_list/">書籍列表</a></li>
                    <li><a href="/author_list/">做者列表</a></li>
                </ul>
            </div>
            <div class="col-sm-9 col-sm-offset-3 col-md-10 col-md-offset-2 main">
                <h1 class="page-header">出版社管理頁面</h1>
                <div class="panel panel-primary">
                    <!-- Default panel contents -->
                    <div class="panel-heading">出版社列表 <i class="fa fa-thumb-tack pull-right"></i></div>
                    <div class="panel-body">
                        <div class="row" style="margin-bottom: 15px">
                            <div class="col-md-4">
                                <div class="input-group">
                                    <input type="text" class="form-control" placeholder="Search for...">
                                    <span class="input-group-btn">
                                        <button class="btn btn-default" type="button">搜索</button>
                                    </span>
                                </div><!-- /input-group -->
                            </div><!-- /.col-md-4 -->
                            <div class="col-md-1 pull-right">
                                <a href="/add_publisher/" class="btn btn-success">新增</a>
                                <!-- <button class="btn btn-success" data-toggle="modal" data-target="#myModal">新增</button>-->
                            </div>
                        </div><!-- /.row -->
                        <table class="table table-bordered">
                            <thead>
                            <tr>
                                <th>ID值</th>
                                <th>出版社名稱</th>
                                <th>操做</th>
                            </tr>
                            </thead>
                            <tbody>
                            {% for publisher in publisher %}
                                <tr>
                                    <td>{{ forloop.counter }}</td>
                                    <td>{{ publisher.name }}</td>
                                    <td>
                                        <a class="btn btn-danger" href="/delete_publisher/?id={{ publisher.id }}">刪除</a>
                                        <a class="btn btn-info" href="/edit_publisher/?id={{ publisher.id }}">編輯</a>
                                    </td>
                                </tr>
                            {% endfor %}
                            </tbody>
                        </table>
                        <nav aria-label="Page navigation" class="text-right">
                            <ul class="pagination">
                                <li>
                                    <a href="#" aria-label="Previous">
                                        <span aria-hidden="true">&laquo;</span>
                                    </a>
                                </li>
                                <li><a href="#">1</a></li>
                                <li><a href="#">2</a></li>
                                <li><a href="#">3</a></li>
                                <li><a href="#">4</a></li>
                                <li><a href="#">5</a></li>
                                <li>
                                    <a href="#" aria-label="Next">
                                        <span aria-hidden="true">&raquo;</span>
                                    </a>
                                </li>
                            </ul>
                        </nav>
                    </div>
                </div>
            </div>
        </div>
    </div>
<!-- Bootstrap core JavaScript
================================================== -->
<!-- Placed at the end of the document so the pages load faster -->
<script src="/static/jquery-3.3.1.js"></script>
<script src="/static/bootstrap/js/bootstrap.min.js"></script>
</body>
</html>
publisher_list.html

添加出版社

<!DOCTYPE html>
<!-- saved from url=(0042)https://v3.bootcss.com/examples/dashboard/ -->
<html lang="zh-CN">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <!-- 上述3個meta標籤*必須*放在最前面,任何其餘內容都*必須*跟隨其後! -->
    <meta name="description" content="">
    <meta name="author" content="">
    <link rel="icon" href="https://v3.bootcss.com/favicon.ico">
    <title>添加出版社</title>
    <!-- Bootstrap core CSS -->
    <link href="/static/bootstrap/css/bootstrap.min.css" rel="stylesheet">
    <!-- Custom styles for this template -->
    <link href="/static/dashboard.css" rel="stylesheet">
    <link rel="stylesheet" href="/static/fontawesome/css/font-awesome.min.css">
</head>
<body>
<div class="container">
    <div class="row">
        <div class="col-md-6 col-md-offset-3">
            <div class="panel panel-primary">
                {# 標題 #}
                <div class="panel-heading">添加出版社</div>
                <div class="panel-body">
                    {# form表單 #}
                    <form class="form-horizontal" action="/add_publisher/" method="post">
                        <div class="form-group">
                            <label for="publisher_name" class="col-sm-2 control-label">出版社</label>
                            <div class="col-sm-10">
                                <input type="text" class="form-control" id="inputEmail3" placeholder="出版社"
                                       name="add_name">
                            </div>
                        </div>
                        <div class="form-group">
                            <div class="col-sm-offset-2 col-sm-10">
                                <button type="submit" class="btn btn-success">提交</button>
                                <h3 style="color: red"> {{ error }}  </h3>
                            </div>
                        </div>
                    </form>
                </div>
            </div>
        </div>
    </div>
</div>
</body>
</html>
add_publisher.html

編輯出版社

<!DOCTYPE html>
<!-- saved from url=(0042)https://v3.bootcss.com/examples/dashboard/ -->
<html lang="zh-CN">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <!-- 上述3個meta標籤*必須*放在最前面,任何其餘內容都*必須*跟隨其後! -->
    <meta name="description" content="">
    <meta name="author" content="">
    <link rel="icon" href="https://v3.bootcss.com/favicon.ico">
    <title>編輯出版社</title>
    <!-- Bootstrap core CSS -->
    <link href="/static/bootstrap/css/bootstrap.min.css" rel="stylesheet">
    <!-- Custom styles for this template -->
    <link href="/static/dashboard.css" rel="stylesheet">
    <link rel="stylesheet" href="/static/fontawesome/css/font-awesome.min.css">
</head>
<body>
<div class="container">
    <div class="row">
        <div class="col-md-6 col-md-offset-3">
            <div class="panel panel-primary">
                <div class="panel-heading">編輯出版社</div>
                <div class="panel-body">
                    <form class="form-horizontal" action="/edit_publisher/" method="post">
                        <input type="text" value="{{ publisher.id }}" name="id" style="display: none;">
                        <div class="form-group">
                            <label for="publisher_name" class="col-sm-2 control-label">出版社</label>
                            <div class="col-sm-10">
                                <input type="text" class="form-control" id="inputEmail3" value="{{ publisher.name }}"
                                       name="name">
                            </div>
                        </div>
                        <div class="form-group">
                            <div class="col-sm-offset-2 col-sm-10">
                                <button type="submit" class="btn btn-success">提交</button>
                                <h3 style="color: red"> {{ error }}  </h3>
                            </div>
                        </div>
                    </form>
                </div>
            </div>
        </div>
    </div>
</div>
</div>
</body>
</html>
edit_publisher.html

展現書籍

<!DOCTYPE html>
<!-- saved from url=(0042)https://v3.bootcss.com/examples/dashboard/ -->
<html lang="zh-CN">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <!-- 上述3個meta標籤*必須*放在最前面,任何其餘內容都*必須*跟隨其後! -->
    <meta name="description" content="">
    <meta name="author" content="">
    <link rel="icon" href="https://v3.bootcss.com/favicon.ico">
    <title>書籍列表</title>
    <!-- Bootstrap core CSS -->
    <link href="/static/bootstrap/css/bootstrap.min.css" rel="stylesheet">
    <!-- Custom styles for this template -->
    <link href="/static/dashboard.css" rel="stylesheet">
    <link rel="stylesheet" href="/static/fontawesome/css/font-awesome.min.css">
</head>
<body>
<nav class="navbar navbar-inverse navbar-fixed-top">
    <div class="container-fluid">
        <div class="navbar-header">
            <button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#navbar"
                    aria-expanded="false" aria-controls="navbar">
                <span class="sr-only">Toggle navigation</span>
                <span class="icon-bar"></span>
                <span class="icon-bar"></span>
                <span class="icon-bar"></span>
            </button>
            <a class="navbar-brand" href="https://v3.bootcss.com/examples/dashboard/#">BMS-S10</a>
        </div>
        <div id="navbar" class="navbar-collapse collapse">
            <ul class="nav navbar-nav navbar-right">
                <li><a href="https://v3.bootcss.com/examples/dashboard/#">Dashboard</a></li>
                <li><a href="https://v3.bootcss.com/examples/dashboard/#">Settings</a></li>
                <li><a href="https://v3.bootcss.com/examples/dashboard/#">Profile</a></li>
                <li><a href="https://v3.bootcss.com/examples/dashboard/#">Help</a></li>
            </ul>
            <form class="navbar-form navbar-right">
                <input type="text" class="form-control" placeholder="Search...">
            </form>
        </div>
    </div>
</nav>
<div class="container-fluid">
    <div class="row">
        <div class="col-sm-3 col-md-2 sidebar">
            <ul class="nav nav-sidebar">
                <li><a href="/publisher_list/">出版社列表頁</a></li>
                <li class="active"><a href="/book_list/">書籍列表</a></li>
                <li><a href="/author_list/">做者列表</a></li>
            </ul>
        </div>
        <div class="col-sm-9 col-sm-offset-3 col-md-10 col-md-offset-2 main">
            <h1 class="page-header">出版社管理頁面</h1>
            <div class="panel panel-primary">
                <!-- Default panel contents -->
                <div class="panel-heading">書籍列表 <i class="fa fa-thumb-tack pull-right"></i></div>
                <div class="panel-body">
                    <div class="row" style="margin-bottom: 15px">
                        <div class="col-md-4">
                            <div class="input-group">
                                <input type="text" class="form-control" placeholder="Search for...">
                                <span class="input-group-btn">
                                    <button class="btn btn-default" type="button">搜索</button>
                                </span>
                            </div><!-- /input-group -->
                        </div><!-- /.col-md-4 -->
                        <div class="col-md-1 pull-right">
                            <a href="/add_book/" class="btn btn-success">新增</a>
                            <!-- <button class="btn btn-success" data-toggle="modal" data-target="#myModal">新增</button>-->
                        </div>
                    </div><!-- /.row -->
                    <table class="table table-bordered">
                        <thead>
                        <tr>
                            <th>ID值</th>
                            <th>書名</th>
                            <th>出版社</th>
                            <th>操做</th>
                        </tr>
                        </thead>
                        <tbody>
                        {% for book in all_book %}
                            <tr>
                                <td>{{ forloop.counter }}</td>
                                <td>{{ book.title }}</td>
                                <td>{{ book.publisher.name }}</td>
                                <td>
                                    <a class="btn btn-danger" href="/delete_book/?id={{ book.id }}">刪除</a>
                                    <a class="btn btn-info" href="/edit_book/?id={{ book.id }}">編輯</a>
                                </td>
                            </tr>
                        {% endfor %}
                        </tbody>
                    </table>
                    <nav aria-label="Page navigation" class="text-right">
                        <ul class="pagination">
                            <li>
                                <a href="#" aria-label="Previous">
                                    <span aria-hidden="true">&laquo;</span>
                                </a>
                            </li>
                            <li><a href="#">1</a></li>
                            <li><a href="#">2</a></li>
                            <li><a href="#">3</a></li>
                            <li><a href="#">4</a></li>
                            <li><a href="#">5</a></li>
                            <li>
                                <a href="#" aria-label="Next">
                                    <span aria-hidden="true">&raquo;</span>
                                </a>
                            </li>
                        </ul>
                    </nav>
                </div>
            </div>
        </div>
    </div>
</div>
<!-- Bootstrap core JavaScript
================================================== -->
<!-- Placed at the end of the document so the pages load faster -->
<script src="/static/jquery-3.3.1.js"></script>
<script src="/static/bootstrap/js/bootstrap.min.js"></script>
</body>
</html>
book_list.html

添加書籍

<!DOCTYPE html>
<!-- saved from url=(0042)https://v3.bootcss.com/examples/dashboard/ -->
<html lang="zh-CN">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <!-- 上述3個meta標籤*必須*放在最前面,任何其餘內容都*必須*跟隨其後! -->
    <meta name="description" content="">
    <meta name="author" content="">
    <link rel="icon" href="https://v3.bootcss.com/favicon.ico">
    <title>添加書籍</title>
    <!-- Bootstrap core CSS -->
    <link href="/static/bootstrap/css/bootstrap.min.css" rel="stylesheet">
    <!-- Custom styles for this template -->
    <link href="/static/dashboard.css" rel="stylesheet">
    <link rel="stylesheet" href="/static/fontawesome/css/font-awesome.min.css">
</head>
<body>
<div class="container">
    <div class="row">
        <div class="col-md-6 col-md-offset-3">
            <div class="panel panel-primary">
                {# 標題 #}
                <div class="panel-heading">添加書籍</div>
                <div class="panel-body">
                    <form class="form-horizontal" action="/add_book/" method="post">
                        <div class="form-group">
                            <label for="publisher_name" class="col-sm-2 control-label">書名</label>
                            <div class="col-sm-10">
                                <input type="text" class="form-control" id="inputEmail3" placeholder="書名"
                                       name="book_name">
                            </div>
                        </div>
                        <div class="form-group">
                            <label for="publisher_name" class="col-sm-2 control-label">出版社</label>
                            <div class="col-sm-10">
                                <select class="form-control" name="publisher_id">
                                    {% for publisher in publisher_list %}
                                        <option value="{{ publisher.id }}">{{ publisher.name }}</option>
                                    {% endfor %}
                                </select>
                            </div>
                        </div>
                        <div class="form-group">
                            <div class="col-sm-offset-2 col-sm-10">
                                <button type="submit" class="btn btn-success">提交</button>
                                <h4 style="color: red;">{{ error }}</h4>
                            </div>
                        </div>
                    </form>
                </div>
            </div>
        </div>
    </div>
</div>
</body>
</html>
add_book.html

編輯書籍

<!DOCTYPE html>
<!-- saved from url=(0042)https://v3.bootcss.com/examples/dashboard/ -->
<html lang="zh-CN">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <!-- 上述3個meta標籤*必須*放在最前面,任何其餘內容都*必須*跟隨其後! -->
    <meta name="description" content="">
    <meta name="author" content="">
    <link rel="icon" href="https://v3.bootcss.com/favicon.ico">
    <title>編輯書籍</title>
    <!-- Bootstrap core CSS -->
    <link href="/static/bootstrap/css/bootstrap.min.css" rel="stylesheet">
    <!-- Custom styles for this template -->
    <link href="/static/dashboard.css" rel="stylesheet">
    <link rel="stylesheet" href="/static/fontawesome/css/font-awesome.min.css">
</head>
<body>
<div class="container">
    <div class="row">
        <div class="col-md-6 col-md-offset-3">
            <div class="panel panel-primary">
                <div class="panel-heading">編輯書籍</div>
                <div class="panel-body">
                    <form class="form-horizontal" action="/edit_book/" method="post">
                        <input type="text" name="id" value="{{ book_obj.id }}" style="display: none">
                        <div class="form-group">
                            <label for="publisher_name" class="col-sm-2 control-label">書名</label>
                            <div class="col-sm-10">
                                <input type="text" name="book_name" value="{{ book_obj.title }}">
                            </div>
                        </div>
                        <div class="form-group">
                            <label for="publisher_name" class="col-sm-2 control-label">出版社</label>
                            <div class="col-sm-10">
                                <select class="form-control" name="publisher">
                                    {% for publisher in publisher_list %}
                                        {% if book_obj.publisher.id == publisher.id %}
                                            <option selected value="{{ publisher.id }}">{{ publisher.name }}</option>
                                        {% endif %}
                                        <option value="{{ publisher.id }}">{{ publisher.name }}</option>
                                    {% endfor %}
                                </select>
                            </div>
                        </div>
                        <div class="form-group">
                            <div class="col-sm-offset-2 col-sm-10">
                                <button type="submit" class="btn btn-success">提交</button>
                            </div>
                        </div>
                    </form>
                </div>
            </div>
        </div>
    </div>
</div>
</body>
</html>
edit_book.html

展現做者

<!DOCTYPE html>
<!-- saved from url=(0042)https://v3.bootcss.com/examples/dashboard/ -->
<html lang="zh-CN">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <!-- 上述3個meta標籤*必須*放在最前面,任何其餘內容都*必須*跟隨其後! -->
    <meta name="description" content="">
    <meta name="author" content="">
    <link rel="icon" href="https://v3.bootcss.com/favicon.ico">
    <title>做者列表</title>
    <!-- Bootstrap core CSS -->
    <link href="/static/bootstrap/css/bootstrap.min.css" rel="stylesheet">
    <!-- Custom styles for this template -->
    <link href="/static/dashboard.css" rel="stylesheet">
    <link rel="stylesheet" href="/static/fontawesome/css/font-awesome.min.css">
</head>
<body>
<nav class="navbar navbar-inverse navbar-fixed-top">
    <div class="container-fluid">
        <div class="navbar-header">
            <button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#navbar"
                    aria-expanded="false" aria-controls="navbar">
                <span class="sr-only">Toggle navigation</span>
                <span class="icon-bar"></span>
                <span class="icon-bar"></span>
                <span class="icon-bar"></span>
            </button>
            <a class="navbar-brand" href="https://v3.bootcss.com/examples/dashboard/#">BMS-S10</a>
        </div>
        <div id="navbar" class="navbar-collapse collapse">
            <ul class="nav navbar-nav navbar-right">
                <li><a href="https://v3.bootcss.com/examples/dashboard/#">Dashboard</a></li>
                <li><a href="https://v3.bootcss.com/examples/dashboard/#">Settings</a></li>
                <li><a href="https://v3.bootcss.com/examples/dashboard/#">Profile</a></li>
                <li><a href="https://v3.bootcss.com/examples/dashboard/#">Help</a></li>
            </ul>
            <form class="navbar-form navbar-right">
                <input type="text" class="form-control" placeholder="Search...">
            </form>
        </div>
    </div>
</nav>
<div class="container-fluid">
    <div class="row">
        <div class="col-sm-3 col-md-2 sidebar">
            <ul class="nav nav-sidebar">
                <li><a href="/publisher_list/">出版社列表頁</a></li>
                <li><a href="/book_list/">書籍列表</a></li>
                <li class="active"><a href="/author_list/">做者列表</a></li>
            </ul>
        </div>
        <div class="col-sm-9 col-sm-offset-3 col-md-10 col-md-offset-2 main">
            <h1 class="page-header">出版社管理頁面</h1>
            <div class="panel panel-primary">
                <!-- Default panel contents -->
                <div class="panel-heading">做者列表 <i class="fa fa-thumb-tack pull-right"></i></div>
                <div class="panel-body">
                    <div class="row" style="margin-bottom: 15px">
                        <div class="col-md-4">
                            <div class="input-group">
                                <input type="text" class="form-control" placeholder="Search for...">
                                <span class="input-group-btn">
                                    <button class="btn btn-default" type="button">搜索</button>
                                </span>
                            </div><!-- /input-group -->
                        </div><!-- /.col-md-4 -->
                        <div class="col-md-1 pull-right">
                            <a href="/add_author/" class="btn btn-success">新增</a>
                            <!-- <button class="btn btn-success" data-toggle="modal" data-target="#myModal">新增</button>-->
                        </div>
                    </div><!-- /.row -->
                    <table class="table table-bordered">
                        <thead>
                        <tr>
                            <th>#</th>
                            <th>ID</th>
                            <th>名字</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 %}
                                    《{{ book.title }}》 &nbsp;
                                {% endfor %}
                                </td>
                                <td>
                                    <a class="btn btn-danger" href="/delete_author/?id={{ author.id }}"><i
                                            class="fa fa-trash-o fa-fw"></i>刪除</a>
                                    <a class="btn btn-info" href="/edit_author/?id={{ author.id }}"><i
                                            class="fa fa-pencil fa-fw"></i>編輯</a>
                                </td>
                            </tr>
                        {% endfor %}
                        </tbody>
                    </table>
                    <nav aria-label="Page navigation" class="text-right">
                        <ul class="pagination">
                            <li>
                                <a href="#" aria-label="Previous">
                                    <span aria-hidden="true">&laquo;</span>
                                </a>
                            </li>
                            <li><a href="#">1</a></li>
                            <li><a href="#">2</a></li>
                            <li><a href="#">3</a></li>
                            <li><a href="#">4</a></li>
                            <li><a href="#">5</a></li>
                            <li>
                                <a href="#" aria-label="Next">
                                    <span aria-hidden="true">&raquo;</span>
                                </a>
                            </li>
                        </ul>
                    </nav>
                </div>
            </div>
        </div>
    </div>
</div>
<!-- Bootstrap core JavaScript
================================================== -->
<!-- Placed at the end of the document so the pages load faster -->
<script src="/static/jquery-3.3.1.js"></script>
<script src="/static/bootstrap/js/bootstrap.min.js"></script>
</body>
</html>
author_list.html

添加做者

<!DOCTYPE html>
<!-- saved from url=(0042)https://v3.bootcss.com/examples/dashboard/ -->
<html lang="zh-CN">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <!-- 上述3個meta標籤*必須*放在最前面,任何其餘內容都*必須*跟隨其後! -->
    <meta name="description" content="">
    <meta name="author" content="">
    <link rel="icon" href="https://v3.bootcss.com/favicon.ico">
    <title>添加做者</title>
    <!-- Bootstrap core CSS -->
    <link href="/static/bootstrap/css/bootstrap.min.css" rel="stylesheet">
    <!-- Custom styles for this template -->
    <link href="/static/dashboard.css" rel="stylesheet">
    <link rel="stylesheet" href="/static/fontawesome/css/font-awesome.min.css">
</head>
<body>
<div class="container">
    <div class="row">
        <div class="col-md-6 col-md-offset-3">
            <div class="panel panel-primary">
                {# 標題 #}
                <div class="panel-heading">添加做者</div>
                <div class="panel-body">
                    <form class="form-horizontal" action="/add_author/" method="post">
                        <div class="form-group">
                            <label for="publisher_name" class="col-sm-2 control-label">做者</label>
                            <div class="col-sm-10">
                                <input type="text" name="author_name">
                            </div>
                        </div>
                        <div class="form-group">
                            <label for="publisher_name" class="col-sm-2 control-label">做品</label>
                            <div class="col-sm-10">
                                <select class="form-control" multiple name="books">
                                    {% for book in book_list %}
                                        <option value="{{ book.id }}">{{ book.title }}</option>
                                    {% endfor %}
                                </select>
                            </div>
                        </div>
                        <div class="form-group">
                            <div class="col-sm-offset-2 col-sm-10">
                                <button type="submit" class="btn btn-success">提交</button>
                                <h4 style="color: red;">{{ error }}</h4>
                            </div>
                        </div>
                    </form>
                </div>
            </div>
        </div>
    </div>
</div>
</body>
</html>
add_author.html

編輯做者

<!DOCTYPE html>
<!-- saved from url=(0042)https://v3.bootcss.com/examples/dashboard/ -->
<html lang="zh-CN">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <!-- 上述3個meta標籤*必須*放在最前面,任何其餘內容都*必須*跟隨其後! -->
    <meta name="description" content="">
    <meta name="author" content="">
    <link rel="icon" href="https://v3.bootcss.com/favicon.ico">
    <title>編輯做者</title>
    <!-- Bootstrap core CSS -->
    <link href="/static/bootstrap/css/bootstrap.min.css" rel="stylesheet">
    <!-- Custom styles for this template -->
    <link href="/static/dashboard.css" rel="stylesheet">
    <link rel="stylesheet" href="/static/fontawesome/css/font-awesome.min.css">
</head>
<body>
<div class="container">
    <div class="row">
        <div class="col-md-6 col-md-offset-3">
            <div class="panel panel-primary">
                <div class="panel-heading">編輯書籍</div>
                <div class="panel-body">
                    <form class="form-horizontal" action="/edit_author/" method="post">
                        <input type="text" name="author_id" value="{{ author.id }}" style="display: none">
                        <div class="form-group">
                            <label for="publisher_name" class="col-sm-2 control-label">做者</label>
                            <div class="col-sm-10">
                                <input type="text" name="author_name" value="{{ author.name }}">
                            </div>
                        </div>
                        <div class="form-group">
                            <label for="publisher_name" class="col-sm-2 control-label">做品</label>
                            <div class="col-sm-10">
                                <select class="form-control" multiple name="books">
                                    {% for book in book_list %}
                                        {#                若是當前這本書 在 當前做者關聯的全部書 裏面 #}
                                        {% if book in author.book.all %}
                                            <option selected value="{{ book.id }}">{{ book.title }}</option>
                                        {% else %}
                                            <option value="{{ book.id }}">{{ book.title }}</option>{% endif %}
                                    {% endfor %}
                                </select>
                            </div>
                        </div>
                        <div class="form-group">
                            <div class="col-sm-offset-2 col-sm-10">
                                <button type="submit" class="btn btn-success">提交</button>
                            </div>
                        </div>
                    </form>
                </div>
            </div>
        </div>
    </div>
</div>
</body>
</html>
edit_author.html
相關文章
相關標籤/搜索