分析

  • 一本书 可以由多个作者编著

  • 一本书只能由一个出版社出版

  • 一个作者可以写多本书

  • 每个作者有自己的简介

对应关系

  • Author-Book # 多对多

  • Publish-Book # 一对多

  • Author-AuhtorDetail # 一对一

如何创建

  • 多对多:ManyToManyField

  • 一对多:ForeignKey

  • 一对一:OneToOneField

注意:创建一对多表的时候,ForeignKey建在多的那一方。另外两个随意建在哪一方。

目录结构

配置 settings.py

注册app:INSTALLED_APPS 项中添加bms

配置静态文件

STATICFILES_DIRS = [
    os.path.join(BASE_DIR, 'bms', 'static')
]

配置模板

TEMPLATES中添加:

'DIRS': [os.path.join(BASE_DIR, 'templates')]

配置路由分发

DjangoBMS.urls.py

from django.conf.urls import url,include
from django.contrib import admin

urlpatterns = [
    url(r'^admin/', admin.site.urls),
    url(r'^bms/', include('bms.urls')),
]

配置母模

<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no">
    <title>Title</title>
    <link rel="stylesheet" href="/static/plugins/bootstrap/css/bootstrap.min.css">
    {% block page-css %}
    {% endblock %}
<body>
{% block page-main %}
{% endblock %}

<script src="/static/js/jquery-3.2.1.min.js"></script>
<script src="/static/plugins/bootstrap/js/bootstrap.min.js"></script>
{% block page-js %}
{% endblock %}
</body>
</html> 

layout

创建数据库表结构

bms.models.py

from django.db import models

class Publish(models.Model):
    # 出版社
    name = models.CharField(max_length=32)
    email = models.EmailField()

class AuhtorDetail(models.Model):
    # 作者简介
    addr = models.CharField(max_length=32)
    email = models.EmailField()

class Author(models.Model):
    # 作者
    name = models.CharField(max_length=32)
    age = models.IntegerField()
    detail = models.OneToOneField('AuhtorDetail')

class Book(models.Model):
    # 书籍
    title = models.CharField(max_length=32)
    publishDate = models.DateField()
    price = models.DecimalField(max_digits=5, decimal_places=2)  # 最大 999.99
    publish = models.ForeignKey('Publish')  # 一对多
    authors = models.ManyToManyField('Author')  # 多对多

生成表命令:

python manange.py makemigrations

python manage.py migrate

数据增删改查

作者-作者详情-增加-显示:

from django.conf.urls import url
from bms import views
urlpatterns = [

    url(r'^indexAuthor/',views.indexAutor),
    url(r'^addAuthor/',views.addAuthor),
]

urls.py

def indexAutor(request):
    author_list = Author.objects.all()
    return render(request, 'indexAuthor.html', {'author_list': author_list})

def addAuthor(request):
    if request.method == 'POST':
        name = request.POST.get('name')
        age = request.POST.get('age')
        addr = request.POST.get('addr')
        email = request.POST.get('email')
        detail_obj = AuhtorDetail.objects.create(addr=addr, email=email)
        Author.objects.create(name=name, age=age, detail_id=detail_obj.id)
        return redirect('/bms/indexAuthor/')
    return render(request, 'addAuthor.html')

views.py

{% extends 'layout.html' %}

{% block page-main %}

    <div class="container">

        <div>
        <span class="h1">
            图书管理系统
            <small>
                作者管理
            </small>
         </span>
        </div>
    <p><a href="/bms/addAuthor/" class="btn btn-sm bg-primary">添加</a></p>
        <div class="table-responsive">

        <table class="table table-striped table-bordered table-hover table-condensed">
<thead>
<tr>
    <th>id</th>
    <th>作者姓名</th>
    <th>作者年龄</th>
    <th>作者住址</th>
    <th>作者邮箱</th>
    <th>操作</th>
</tr>
</thead>
            <tbody>
            {% for author in author_list %}
            <tr>
                <td>{{ author.id }}</td>
                <td>{{ author.name }}</td>
                <td>{{ author.age }}</td>
                <td>{{ author.detail.addr }}</td>
                <td>{{ author.detail.email}}</td>
                <td>
                    <a href="" class="btn btn-sm bg-danger">删除</a>
                    <a href="" class="btn btn-sm bg-primary">编辑</a>
                </td>
            </tr>
            {% endfor %}
            </tbody>
        </table>
        </div>
    </div>

{% endblock %}

indexAuthor.html

{% extends 'layout.html' %}

{% block page-main %}

    <div class="container">

        <div class="panel panel-primary">
            <div class="panel-heading">
                <h3 class="panel-title">添加作者 </h3>
            </div>
            <div class="panel-body">
                <!--表单开始-->
                <form class="form-horizontal" action="/bms/addAuthor/" method="post">
                    {% csrf_token %}
                    <div class="form-group">
                        <label for="inputName" class="col-sm-2 control-label">姓名</label>
                        <div class="col-sm-3">
                            <input type="text" name="name" class="form-control" id="inputName" placeholder="姓名">
                        </div>
                    </div>
                    <div class="form-group">
                        <label for="inputAge" class="col-sm-2 control-label">年龄</label>
                        <div class="col-sm-3">
                            <input type="text" name="age" class="form-control" id="inputAge" placeholder="年龄">
                        </div>
                    </div>
                    <div class="form-group">
                        <label for="inputAddr" class="col-sm-2 control-label">住址</label>
                        <div class="col-sm-3">
                            <input type="text" name="addr" class="form-control" id="inputAddr" placeholder="住址">
                        </div>
                    </div>
                    <div class="form-group">
                        <label for="inputEmail" class="col-sm-2 control-label">邮箱</label>
                        <div class="col-sm-3">
                            <input type="text" name="email" class="form-control" id="inputEmail" placeholder="邮箱">
                        </div>
                    </div>

                    <div class="form-group">
                        <div class="col-sm-3 col-sm-offset-2">
                            <input type="submit" value="提交" class="bg-primary">
                        </div>
                    </div>
                </form>
                <!--表单结束-->
            </div>
        </div>

    </div>

{% endblock %}

addAuthor.html

 出版社-增加-显示

from django.conf.urls import url
from bms import views
urlpatterns = [

    url(r'^indexAuthor/',views.indexAutor),
    url(r'^addAuthor/',views.addAuthor),
    url(r'^indexPublish/',views.indexPublish),
    url(r'^addPublish/',views.addPublish),
]

urls.py

# 显示出版社
def indexPublish(request):
    publish_list = Publish.objects.all()
    return render(request, 'indexPublish.html', {'publish_list': publish_list})

# 添加出版社
def addPublish(request):
    if request.method == 'POST':
        name = request.POST.get('name')
        email = request.POST.get('email')
        Publish.objects.create(name=name, email=email)
        return redirect('/bms/indexPublish/')
    return render(request, 'addPublish.html')

views.py

{% extends 'layout.html' %}

{% block page-main %}

    <div class="container">

        <div>
        <span class="h1">
            图书管理系统
            <small>
                出版社管理
            </small>
         </span>
        </div>
        <br>
        <hr>
        <br>
        <p><a href="/bms/addPublish/" class="btn btn-sm bg-primary">添加</a></p>
        <div class="table-responsive">

            <table class="table table-striped table-bordered table-hover table-condensed">
                <thead>
                <tr>
                    <th>出版社名称</th>
                    <th>出版社邮箱</th>
                    <th>操作</th>
                </tr>
                </thead>
                <tbody>
                {% for publish in publish_list %}
                    <tr>
                        <td>{{ publish.name }}</td>
                        <td>{{ publish.email }}</td>
                        <td>
                            <a href="" class="btn btn-sm bg-danger">删除</a>
                            <a href="" class="btn btn-sm bg-primary">编辑</a>
                        </td>
                    </tr>
                {% endfor %}
                </tbody>
            </table>
        </div>
    </div>

{% endblock %}

indexPublish.html

{% extends 'layout.html' %}

{% block page-main %}

    <div class="container">

        <div class="panel panel-primary">
            <div class="panel-heading">
                <h3 class="panel-title">添加出版社 </h3>
            </div>
            <div class="panel-body">
                <!--表单开始-->
                <form class="form-horizontal" action="/bms/addPublish/" method="post">
                    {% csrf_token %}
                    <div class="form-group">
                        <label for="inputName" class="col-sm-2 control-label">出版社名称</label>
                        <div class="col-sm-3">
                            <input type="text" name="name" class="form-control" id="inputName" placeholder="出版社名称">
                        </div>
                    </div>
                    <div class="form-group">
                        <label for="inputEmail" class="col-sm-2 control-label">邮箱</label>
                        <div class="col-sm-3">
                            <input type="text" name="email" class="form-control" id="inputEmail" placeholder="邮箱">
                        </div>
                    </div>

                    <div class="form-group">
                        <div class="col-sm-3 col-sm-offset-2">
                            <input type="submit" value="提交" class="bg-primary">
                        </div>
                    </div>
                </form>
                <!--表单结束-->
            </div>
        </div>

    </div>

{% endblock %}

addPublish.html

书籍-增加-显示

from django.conf.urls import url
from bms import views
urlpatterns = [

    url(r'^indexAuthor/',views.indexAutor),
    url(r'^addAuthor/',views.addAuthor),
    url(r'^indexPublish/',views.indexPublish),
    url(r'^addPublish/',views.addPublish),
    url(r'^indexBook/',views.indexBook),
    url(r'^addBook/',views.addBook),
]

urls.py

{% extends 'layout.html' %}

{% block page-main %}

    <div class="container">

        <div>
        <span class="h1">
            图书管理系统
            <small>
                书籍管理
            </small>
         </span>
        </div>
        <br>
        <hr>
        <br>
        <p><a href="/bms/addBook/" class="btn btn-sm bg-primary">添加</a></p>
        <div class="table-responsive">

            <table class="table table-striped table-bordered table-hover table-condensed">
                <thead>
                <tr>
                    <th>书籍姓名</th>
                    <th>出版日期</th>
                    <th>售价</th>
                    <th>作者</th>
                    <th>出版社</th>
                    <th>操作</th>
                </tr>
                </thead>
                <tbody>
                {% for book in book_list %}
                    <tr>
                        <td>{{ book.title }}</td>
                        <td>{{ book.publishDate|date:'YY-mm-dd' }}</td>
                        <td>{{ book.price }}</td>

                        <td>
                            {% for author in book.authors.all %}
                                <span>{{ author.name }}</span>
                            {% endfor %}
                        </td>

                        <td>{{ book.publish.name }}</td>
                        <td>
                            <a href="" class="btn btn-sm bg-danger">删除</a>
                            <a href="" class="btn btn-sm bg-primary">编辑</a>
                        </td>
                    </tr>
                {% endfor %}
                </tbody>
            </table>
        </div>
    </div>

{% endblock %}

indexBook.html

{% extends 'layout.html' %}

{% block page-main %}

    <div class="container">

        <div class="panel panel-primary">
            <div class="panel-heading">
                <h3 class="panel-title">添加书籍 </h3>
            </div>
            <div class="panel-body">
                <!--表单开始-->
                <form class="form-horizontal" action="/bms/addBook/" method="post">
                    {% csrf_token %}
                    <div class="form-group">
                        <label for="inputTitle" class="col-sm-2 control-label">书名</label>
                        <div class="col-sm-3">
                            <input type="text" name="title" class="form-control" id="inputTitle" placeholder="书名">
                        </div>
                    </div>
                    <div class="form-group">
                        <div class="col-sm-2 "><b style="float: right">出版日期</b></div>
                        <div class="col-sm-3 ">
                            <input type="date" name="date" class="form-control">
                        </div>
                    </div>
                    <div class="form-group">
                        <label for="inputPrice" class="col-sm-2 control-label">售价</label>
                        <div class="col-sm-3">
                            <input type="text" name="price" class="form-control" id="inputPrice" placeholder="售价">
                        </div>
                    </div>
                    <div class="form-group">
                        <div class="col-sm-2 "><b style="float: right">作者</b></div>
                        <div class="col-sm-3">
                            <select class="form-control" name="authors" multiple>
                                {% for author in author_list %}
                                    <option value="{{ author.id }}">{{ author.name }}</option>
                                {% endfor %}
                            </select>
                        </div>
                    </div>

                    <div class="form-group">
                        <div class="col-sm-2 "><b style="float: right">出版社</b></div>
                        <div class="col-sm-3">
                            <select class="form-control" name="publish">
                                {% for publish in publish_list %}
                                    <option value="{{ publish.id }}">{{ publish.name }}</option>
                                {% endfor %}
                            </select>
                        </div>
                    </div>

                    <div class="form-group">
                        <div class="col-sm-3 col-sm-offset-2">
                            <input type="submit" value="提交" class="bg-primary">
                        </div>
                    </div>
                </form>
                <!--表单结束-->
            </div>
        </div>

    </div>

{% endblock %}

addBook.html

# 显示书籍
def indexBook(request):
    book_list = Book.objects.all()
    return render(request, 'indexBook.html', {'book_list': book_list})

# 添加书籍
def addBook(request):
    if request.method == 'POST':
        title = request.POST.get('title')
        date = request.POST.get('date')
        price = request.POST.get('price')
        publish_id = request.POST.get('publish')
        authors = request.POST.getlist('authors')
        book_obj = Book.objects.create(
            title=title,
            publishDate=date,
            price=price,
            publish_id=publish_id,
        )
        book_obj.authors.add(*authors)
        return redirect('/bms/indexBook/')
    publish_list = Publish.objects.all()
    author_list = Author.objects.all()
    return render(request, 'addBook.html', {'publish_list': publish_list, 'author_list': author_list})

views.py

删除与编辑

from django.conf.urls import url
from bms import views

urlpatterns = [

    url(r'^indexAuthor/', views.indexAutor),
    url(r'^addAuthor/', views.addAuthor),
    url(r'^indexPublish/', views.indexPublish),
    url(r'^addPublish/', views.addPublish),
    url(r'^indexBook/', views.indexBook),
    url(r'^addBook/', views.addBook),
    url(r'^delBook/', views.delBook),
    url(r'^delPublish/', views.delPublish),
    url(r'^delAuthor/', views.delAuthor),
    url(r'^editPublish/', views.editPublish),
    url(r'^editBook/', views.editBook),
    url(r'^editAuthor/', views.editAuthor),
]

urls.py

from django.shortcuts import render, redirect
from bms.models import *

# 显示作者
def indexAutor(request):
    author_list = Author.objects.all()
    return render(request, 'indexAuthor.html', {'author_list': author_list})

# 添加作者与作者信息
def addAuthor(request):
    if request.method == 'POST':
        name = request.POST.get('name')
        age = request.POST.get('age')
        addr = request.POST.get('addr')
        email = request.POST.get('email')
        detail_obj = AuhtorDetail.objects.create(addr=addr, email=email)
        Author.objects.create(name=name, age=age, detail_id=detail_obj.id)
        return redirect('/bms/indexAuthor/')
    return render(request, 'addAuthor.html')

# 显示出版社
def indexPublish(request):
    publish_list = Publish.objects.all()
    return render(request, 'indexPublish.html', {'publish_list': publish_list})

# 添加出版社
def addPublish(request):
    if request.method == 'POST':
        name = request.POST.get('name')
        email = request.POST.get('email')
        Publish.objects.create(name=name, email=email)
        return redirect('/bms/indexPublish/')
    return render(request, 'addPublish.html')

# 显示书籍
def indexBook(request):
    book_list = Book.objects.all()
    return render(request, 'indexBook.html', {'book_list': book_list})

# 添加书籍
def addBook(request):
    if request.method == 'POST':
        title = request.POST.get('title')
        date = request.POST.get('date')
        price = request.POST.get('price')
        publish_id = request.POST.get('publish')
        authors = request.POST.getlist('authors')
        book_obj = Book.objects.create(
            title=title,
            publishDate=date,
            price=price,
            publish_id=publish_id
        )
        print(authors)
        book_obj.authors.set(authors)
        return redirect('/bms/indexBook/')
    publish_list = Publish.objects.all()
    author_list = Author.objects.all()
    return render(request, 'addBook.html', {'publish_list': publish_list, 'author_list': author_list})

# 删除书籍
def delBook(request):
    id=request.GET.get('id')
    Book.objects.filter(id=id).delete()
    return redirect('/bms/indexBook/')

# 删除出版社
def delPublish(request):
    id=request.GET.get('id')
    Publish.objects.filter(id=id).delete()
    book_obj=Book.objects.filter(publish_id=id)
    for book in book_obj:
        book.authors.all().delete()  # book-authors关系表中的该出版社的关系也删除
    Book.objects.filter(publish_id=id).delete()  # 该出版社出版的书也删除
    return redirect('/bms/indexPublish')

# 删除作者
def delAuthor(request):
    id = request.GET.get('id')
    Author.objects.filter(id=id).delete()
    return redirect('/bms/indexAuthor/')

# 编辑出版社
def editPublish(request):
    if request.method=='POST':
        id=request.POST.get('id')
        name=request.POST.get('name')
        email=request.POST.get('email')
        Publish.objects.filter(id=id).update(name=name,email=email)
        return redirect('/bms/indexPublish/')
    id=request.GET.get('id')
    publish=Publish.objects.filter(id=id).first()
    return render(request,'editPublish.html',{'publish':publish})

# 编辑书籍
def editBook(request):
    if request.method=='POST':
        id=request.POST.get('id')
        title=request.POST.get('title')
        date=request.POST.get('date')
        price=request.POST.get('price')
        authors=request.POST.getlist('authors')
        publish_id=request.POST.get('publish')
        book_obj=Book.objects.filter(id=id).update(
            title=title,
            publishDate=date,
            price=price,
            publish_id=publish_id
        )
        Book.objects.get(id=id).authors.set(authors)
        return redirect('/bms/indexBook/')

    id=request.GET.get('id')
    book=Book.objects.filter(id=id).first()
    author_list=Author.objects.all()
    publish_list=Publish.objects.all()
    return render(request,'editBook.html',{'book':book,'author_list':author_list,'publish_list':publish_list})

# 编辑作者
def editAuthor(request):
    if request.method=='POST':
        aid=request.POST.get('aid')
        did=request.POST.get('did')
        name=request.POST.get('name')
        age=request.POST.get('age')
        addr=request.POST.get('addr')
        email=request.POST.get('email')
        Author.objects.filter(id=aid).update(name=name,age=age,detail_id=did)
        AuhtorDetail.objects.filter(id=did).update(addr=addr,email=email)
        return redirect('/bms/indexAuthor/')
    id=request.GET.get('id')
    author=Author.objects.filter(id=id).first()
    detail=AuhtorDetail.objects.filter(id=author.detail_id).first()
    return render(request,'editAuthor.html',{'author':author,'detail':detail})

views.py

{% extends 'layout.html' %}

{% block page-main %}

    <div class="container">

        <div>
        <span class="h1">
            图书管理系统
            <small>
                作者管理
            </small>
         </span>
        </div>
        <br>
        <hr>
        <br>
        <p><a href="/bms/addAuthor/" class="btn btn-sm bg-primary">添加</a></p>
        <div class="table-responsive">

            <table class="table table-striped table-bordered table-hover table-condensed">
                <thead>
                <tr>
                    <th>作者姓名</th>
                    <th>作者年龄</th>
                    <th>作者住址</th>
                    <th>作者邮箱</th>
                    <th>操作</th>
                </tr>
                </thead>
                <tbody>
                {% for author in author_list %}
                    <tr>
                        <td>{{ author.name }}</td>
                        <td>{{ author.age }}</td>
                        <td>{{ author.detail.addr }}</td>
                        <td>{{ author.detail.email }}</td>
                        <td>
                            <a href="/bms/delAuthor/?id={{ author.id }}" class="btn btn-sm bg-danger">删除</a>
                            <a href="/bms/editAuthor/?id={{ author.id }}" class="btn btn-sm bg-primary">编辑</a>
                        </td>
                    </tr>
                {% endfor %}
                </tbody>
            </table>
        </div>
    </div>

{% endblock %}

indexAuthor.HTML

{% extends 'layout.html' %}

{% block page-main %}

    <div class="container">

        <div>
        <span class="h1">
            图书管理系统
            <small>
                书籍管理
            </small>
         </span>
        </div>
        <br>
        <hr>
        <br>
        <p><a href="/bms/addBook/" class="btn btn-sm bg-primary">添加</a></p>
        <div class="table-responsive">

            <table class="table table-striped table-bordered table-hover table-condensed">
                <thead>
                <tr>
                    <th>书籍姓名</th>
                    <th>出版日期</th>
                    <th>售价</th>
                    <th>作者</th>
                    <th>出版社</th>
                    <th>操作</th>
                </tr>
                </thead>
                <tbody>
                {% for book in book_list %}
                    <tr>
                        <td>{{ book.title }}</td>
                        <td>{{ book.publishDate|date:'Y-m-d' }}</td>
                        <td>{{ book.price }}</td>

                        <td>
                            {% for author in book.authors.all %}
                                <span>{{ author.name }}</span>
                            {% endfor %}
                        </td>

                        <td>{{ book.publish.name }}</td>
                        <td>
                            <a href="/bms/delBook/?id={{ book.id }}" class="btn btn-sm bg-danger">删除</a>
                            <a href="/bms/editBook/?id={{ book.id }}" class="btn btn-sm bg-primary">编辑</a>
                        </td>
                    </tr>
                {% endfor %}
                </tbody>
            </table>
        </div>
    </div>

{% endblock %}

indexBook.HTML

{% extends 'layout.html' %}

{% block page-main %}

    <div class="container">

        <div>
        <span class="h1">
            图书管理系统
            <small>
                出版社管理
            </small>
         </span>
        </div>
        <br>
        <hr>
        <br>
        <p><a href="/bms/addPublish/" class="btn btn-sm bg-primary">添加</a></p>
        <div class="table-responsive">

            <table class="table table-striped table-bordered table-hover table-condensed">
                <thead>
                <tr>
                    <th>出版社名称</th>
                    <th>出版社邮箱</th>
                    <th>操作</th>
                </tr>
                </thead>
                <tbody>
                {% for publish in publish_list %}
                    <tr>
                        <td>{{ publish.name }}</td>
                        <td>{{ publish.email }}</td>
                        <td>
                            <a href="/bms/delPublish/?id={{ publish.id }}" class="btn btn-sm bg-danger">删除</a>
                            <a href="/bms/editPublish/?id={{ publish.id }}" class="btn btn-sm bg-primary">编辑</a>
                        </td>
                    </tr>
                {% endfor %}
                </tbody>
            </table>
        </div>
    </div>

{% endblock %}

indexPublish.html

{% extends 'layout.html' %}

{% block page-main %}

    <div class="container">

        <div class="panel panel-primary">
            <div class="panel-heading">
                <h3 class="panel-title">编辑作者信息 </h3>
            </div>
            <div class="panel-body">
                <!--表单开始-->
                <form class="form-horizontal" action="/bms/editAuthor/" method="post">
                    {% csrf_token %}
                    <input type="text" name="aid" value="{{ author.id }}" style="display: none">
                    <input type="text" name="did" value="{{ detail.id }}" style="display: none">
                    <div class="form-group">
                        <label for="inputName" class="col-sm-2 control-label">姓名</label>
                        <div class="col-sm-3">
                            <input type="text" value="{{ author.name }}" name="name" class="form-control" id="inputName"
                                   placeholder="姓名">
                        </div>
                    </div>
                    <div class="form-group">
                        <label for="inputAge" class="col-sm-2 control-label">年龄</label>
                        <div class="col-sm-3">
                            <input type="text" value="{{ author.age }}" name="age" class="form-control" id="inputAge"
                                   placeholder="年龄">
                        </div>
                    </div>
                    <div class="form-group">
                        <label for="inputAddr" class="col-sm-2 control-label">住址</label>
                        <div class="col-sm-3">
                            <input type="text" value="{{ detail.addr }}" name="addr" class="form-control" id="inputAddr"
                                   placeholder="住址">
                        </div>
                    </div>
                    <div class="form-group">
                        <label for="inputEmail" class="col-sm-2 control-label">邮箱</label>
                        <div class="col-sm-3">
                            <input type="text" value="{{ detail.email }}" name="email" class="form-control"
                                   id="inputEmail" placeholder="邮箱">
                        </div>
                    </div>

                    <div class="form-group">
                        <div class="col-sm-3 col-sm-offset-2">
                            <input type="submit" value="提交" class="bg-primary">
                        </div>
                    </div>
                </form>
                <!--表单结束-->
            </div>
        </div>

    </div>

{% endblock %}

editAuthor.html

{% extends 'layout.html' %}

{% block page-main %}

    <div class="container">

        <div class="panel panel-primary">
            <div class="panel-heading">
                <h3 class="panel-title">修改书籍信息 </h3>
            </div>
            <div class="panel-body">
                <!--表单开始-->
                <form class="form-horizontal" action="/bms/editBook/" method="post">
                    {% csrf_token %}
                    <input type="text" name="id" value="{{ book.id }}" style="display: none">
                    <div class="form-group">
                        <label for="inputTitle" class="col-sm-2 control-label">书名</label>
                        <div class="col-sm-3">
                            <input type="text" value="{{ book.title }}" name="title" class="form-control"
                                   id="inputTitle" placeholder="书名">
                        </div>
                    </div>
                    <div class="form-group">
                        <div class="col-sm-2 "><b style="float: right">出版日期</b></div>
                        <div class="col-sm-3 ">
                            <input type="date" value="{{ book.publishDate|date:'Y-m-d' }}" name="date"
                                   class="form-control">
                        </div>
                    </div>
                    <div class="form-group">
                        <label for="inputPrice" class="col-sm-2 control-label">售价</label>
                        <div class="col-sm-3">
                            <input type="text" value="{{ book.price }}" name="price" class="form-control"
                                   id="inputPrice" placeholder="售价">
                        </div>
                    </div>
                    <div class="form-group">
                        <div class="col-sm-2 "><b style="float: right">作者</b></div>
                        <div class="col-sm-3">
                            <select class="form-control" name="authors" multiple>
                                {% for author in author_list %}
                                    {% if author in book.authors.all %} }}
                                        <option value="{{ author.id }}" selected>{{ author.name }}</option>
                                    {% else %}
                                        <option value="{{ author.id }}">{{ author.name }}</option>
                                    {% endif %}
                                {% endfor %}
                            </select>
                        </div>
                    </div>

                    <div class="form-group">
                        <div class="col-sm-2 "><b style="float: right">出版社</b></div>
                        <div class="col-sm-3">
                            <select class="form-control" name="publish">
                                {% for publish in publish_list %}
                                    {% if publish.id == book.publish_id %}
                                        <option value="{{ publish.id }}" selected>{{ publish.name }}</option>
                                    {% else %}
                                        <option value="{{ publish.id }}">{{ publish.name }}</option>
                                    {% endif %}
                                {% endfor %}
                            </select>
                        </div>
                    </div>

                    <div class="form-group">
                        <div class="col-sm-3 col-sm-offset-2">
                            <input type="submit" value="提交" class="bg-primary">
                        </div>
                    </div>
                </form>
                <!--表单结束-->
            </div>
        </div>

    </div>

{% endblock %}

editBook.html

{% extends 'layout.html' %}

{% block page-main %}

    <div class="container">

        <div class="panel panel-primary">
            <div class="panel-heading">
                <h3 class="panel-title">添加出版社 </h3>
            </div>
            <div class="panel-body">
                <!--表单开始-->
                <form class="form-horizontal" action="/bms/editPublish/" method="post">
                    {% csrf_token %}
                    <input type="text" value="{{ publish.id }}" name="id" style="display: none">
                    <div class="form-group">
                        <label for="inputName" class="col-sm-2 control-label">出版社名称</label>
                        <div class="col-sm-3">
                            <input type="text" value="{{ publish.name }}" name="name" class="form-control"
                                   id="inputName" placeholder="出版社名称">
                        </div>
                    </div>
                    <div class="form-group">
                        <label for="inputEmail" class="col-sm-2 control-label">邮箱</label>
                        <div class="col-sm-3">
                            <input type="text" value="{{ publish.email }}" name="email" class="form-control"
                                   id="inputEmail" placeholder="邮箱">
                        </div>
                    </div>

                    <div class="form-group">
                        <div class="col-sm-3 col-sm-offset-2">
                            <input type="submit" value="提交" class="bg-primary">
                        </div>
                    </div>
                </form>
                <!--表单结束-->
            </div>
        </div>

    </div>

{% endblock %}

editBook.html

完整代码已推到GitHub : https://github.com/lichengguang/DjangoBMS

一对多查询:

def test(request):
    # 查询 如何让孩子爱上学习 这本书的出版社名字
    book_obj=Book.objects.filter(title='如何让孩子爱上学习').first()
    print(book_obj.publish.name)
    # 查询 捞钱出版社出版的所有书籍QuerySet
    publish_obj=Publish.objects.filter(name='捞钱出版社').first()
    # 方式一
    print(Book.objects.filter(publish_id=publish_obj.id).all())
    # 方式二
    print(publish_obj.book_set.all())

    return HttpResponse('OK')

# 结果:
捞钱出版社
<QuerySet [<Book: Book object>, <Book: Book object>, <Book: Book object>, <Book: Book object>]>
<QuerySet [<Book: Book object>, <Book: Book object>, <Book: Book object>, <Book: Book object>]>

  

ORM练习项目-图书管理系统(BMS)实现细节的更多相关文章

  1. java web 项目 图书管理系统的设计与实现

     java web 项目 图书管理系统的设计与实现

  2. Java swing项目-图书管理系统(swing+mysql+jdbc)

    (一)项目功能分析 该项目是设计一个图书管理系统,主要包含的内容有: (1)管理员登陆界面 ->信息录入 ->登录 ->重置 (2)图书管理系统总界面 ->子界面菜单: 1)图 ...

  3. Java swing项目-图书管理系统(swing+mysql+jdbc) 总结

    (一)java Swing的学习. (1)学习如何安装windowbuilder插件的安装. <1>在eclipse中点击help <2>在help的下拉选中选择install ...

  4. 在Django中使用ORM创建图书管理系统

    一.ORM(对象关系映射) 很多语言的web框架中都有这个概念 1. 为什么要有ORM? 1. 写程序离不开数据,要使用数据就需要连接数据库,但是不同的数据库在sql语句上(mysql,oracle等 ...

  5. 新建Django项目示例--图书管理系统

    知识点: Django 1. 安装 1. Django版本 1.11.xx 2. 安装方式 1. 命令行 --> Python环境(双版本,pip的使用) 2. PyCharm安装 2. 创建D ...

  6. Django学习——分组查询、图书管理系统项目、wsgi, uwsgi, cgi, fastcgi

    1 分组查询 # 分组查询 # 查询每一个出版社id,以及图书平均价格(单表) # 原生sql # select publish_id,avg(price) from app01_book group ...

  7. struts2+hibernate 项目实战:图书管理系统

    经典项目,练手必备. 图书管理系统 需求分析(大致,并不专业):1.需要有用户管理: 1.1 用户注册: 1.2 用户登录: 1.3 用户信息修改: 1.4 用户修改密码: 2.需要有书本管理: 2. ...

  8. Linux-基础学习(四)-部署图书管理系统项目

    部署图书管理项目需要以下软件 项目文件(django项目文件夹) 数据库文件(django项目对应的数据库文件) centos7(linux本体) nginx(反向代理以及静态文件收集) uWSGI( ...

  9. C项目实践--图书管理系统(4)

    前面已经把图书管理系统的所有功能模块都已实现完毕了,下面通过运行来分析该系统的操作流程并检验是否符合逻辑设计要求. 3.系统操作过程 F5 运行 1.登录系统 系统运行之后,提示输入用户名和密码,系统 ...

随机推荐

  1. SQL Server如何清除曾经登录过的登录名

    我用的是SQL Server2008数据库,在数据库登录界面,有时我们用户已经在安全性已经删除了,但是登录名痕迹还是存在, 那如何删除掉这些用户登录过的登录记录呢? 我本机是要删除这个登录名为s的记录

  2. laravel 连接同一服务器上多个数据库操作 、 连接多个不同服务器上的不同数据库操作以及多个数据库操作的事务处理

    !注意:标红的要注意区分开 第一步.配置.env文件(同一服务器上多个数据库) DB_CONNECTION=pgsqlDB_HOST=IP(例如:127.0.0.1)DB_PORT=端口号(例如:54 ...

  3. Oracle sqlloader

    一.SQL*LOADER简介 SQL*Loader是oracle提供的可以从多种平面文件中向数据库中加载数据的工具,使用sqlldr工具可以在很短的时间内向数据库中加载大量的数据,像把制作好的exce ...

  4. sql server 的游标

    -- sql server 中的游标 --声明游标 /* declare cursorname [insensitive] [scroll] cursor for <select-查询块> ...

  5. 浅谈:从为什么学习python到如何学好python

    虽然目前的编程语言有很多,但是基础语法上的概念,本质上都是相通的.可以做到一通百通.所以没有必要为了学哪门语言纠结太多. python是目前市面上,我个人认为是最简洁&&最优雅& ...

  6. 福大软工 1816:项目UML设计(团队作业三)

    项目UML设计(团队) 团队信息 团队名:第三视角 各成员学号及姓名 姓名 学号 博客链接 张扬(组长) 031602345 http://www.cnblogs.com/sxZhangYang/p/ ...

  7. 数据结构(C语言)关于查找与排序

    1)利用readData()函数从data1.txt中读入不同规模的数据存入数组,编写基于数组的顺序查找算法,测试数据量为1万.5万.10万.20万.30万.40万和50万时的数据查询时间. 算法代码 ...

  8. 关于python课程的想法和建议。

    第一次听说python是在刚结束与世隔绝的高中生活之后,当时的网络上铺天遍地都是人工智能和机器学习,于是便知道了python这门编程语言.我是光电信息科学与工程专业的学生,这个专业的学生必须要懂计算机 ...

  9. HTML5的classList API优化对样式名className的操作

    //添加一个class elem.classList.add(classname); //删除一个class elem.classList.remove(classname); //判断一个class ...

  10. SharePoint 2010: Change welcome page on PowerShell

    摘要: SharePoint 2010之后呢, 建立一个 Team Site会有两个 default page, 分别是 Sitepages/home.aspx and default.aspx. 这 ...