七、Django实战--图书管理系统搭建

图书管理系统搭建

models.py

from django.db import models

# Create your models here.

# 书籍表
class Book(models.Model):
    title = models.CharField(max_length=32)
    price = models.DecimalField(max_digits=8,decimal_places=2)
    publish_date = models.DateField(auto_now_add=True)

    # 书籍表和出版社表的关系 一对多
    publishs = models.ForeignKey(to='Publish')

    # 书籍表和作者表的关系 多对多
    authors = models.ManyToManyField(to='Author')


# 作者表
class Author(models.Model):
    name = models.CharField(max_length=32)
    age = models.IntegerField()

    # 作者表和作者详情表的关系 一对一
    author_detail = models.OneToOneField(to='AuthorDetail')

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

# 作者详情表
class AuthorDetail(models.Model):
    phone = models.BigIntegerField()
    addr = models.CharField(max_length=64)

views.py

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

# 首页展示页面
def home(request):
    return render(request,'home.html')

# 书籍展示页面
def book_list(request):
    book_queryset = models.Book.objects.all()
    return render(request,'book_list.html',locals())

# 书籍添加页面
def book_add(request):
    if request.method == 'POST':
        title = request.POST.get('title')
        price = request.POST.get('price')
        publish_date = request.POST.get('publish_date')
        publish_id = request.POST.get('publish')
        author_id_list = request.POST.getlist('author')
        book_obj = models.Book.objects.create(title=title,
                                   price=price,
                                   publish_date=publish_date,
                                   publishs_id=publish_id)
        book_obj.authors.add(*author_id_list)
        return redirect('book_list')

    author_queryset = models.Author.objects.all()
    publish_queryset = models.Publish.objects.all()
    return render(request,'book_add.html',locals())

# 书籍修改页面
def book_edid(request,edit_id):
    edit_obj = models.Book.objects.filter(pk=edit_id).first()
    if request.method == 'POST':
        title = request.POST.get('title')
        price = request.POST.get('price')
        publish_date = request.POST.get('publish_date')
        publish_id = request.POST.get('publish')
        author_id_list = request.POST.getlist('author')
        models.Book.objects.filter(pk=edit_id).update(title=title,
                                                   price=price,
                                                   publish_date=publish_date,
                                                   publishs_id=publish_id)
        edit_obj.authors.set(author_id_list)
        return redirect('book_list')

    author_queryset = models.Author.objects.all()
    publish_queryset = models.Publish.objects.all()
    return render(request,'book_edid.html',locals())

# 书籍删除页面
def book_delete(request,delete_id):
    models.Book.objects.filter(pk=delete_id).delete()
    return redirect('book_list')

url.py

"""day03 URL Configuration

The `urlpatterns` list routes URLs to views. For more information please see:
    https://docs.djangoproject.com/en/1.11/topics/http/urls/
Examples:
Function views
    1. Add an import:  from my_app import views
    2. Add a URL to urlpatterns:  url(r'^$', views.home, name='home')
Class-based views
    1. Add an import:  from other_app.views import Home
    2. Add a URL to urlpatterns:  url(r'^$', Home.as_view(), name='home')
Including another URLconf
    1. Import the include() function: from django.conf.urls import url, include
    2. Add a URL to urlpatterns:  url(r'^blog/', include('blog.urls'))
"""
from django.conf.urls import url
from django.contrib import admin
from app01 import views

urlpatterns = [
    url(r'^admin/', admin.site.urls),
    # 首页
    url(r'^$',views.home,name='home'),
    # 书籍展示
    url(r'^book/list/',views.book_list,name='book_list'),
    # 书籍添加
    url(r'^book/add/',views.book_add,name='book_add'),
    # 书籍修改
    url(r'^book/edit/(\d+)/',views.book_edid,name='book_edit'),
    # 书籍删除
    url(r'^book/delete/(?P<delete_id>\d+)/',views.book_delete,name='book_delete')
]

templates文件夹

1、book_add.html

{% extends 'home.html' %}

{% block content %}
    <h3 class="text-center" style="color: olivedrab  ">新增书籍</h3>
    <form action="" method="post">
        <p>书名:<input type="text" name="title" class="form-control"></p>
        <p>价格:<input type="text" name="price" class="form-control"></p>
        <p>出版时间:<input type="date" name="publish_date" class="form-control"></p>
        <p>出版社:
            <select name="publish" id="">
                {% for publish_obj in publish_queryset %}
                    <option value="{{ publish_obj.pk }}">
                        {{ publish_obj.name }}
                    </option>
                {% endfor %}

            </select>
        </p>
        <p>作者:
            <select name="author" id="" multiple>
                {% for author_obj in author_queryset %}
                    <option value="{{ author_obj.pk }}">
                        {{ author_obj.name }}
                    </option>
                {% endfor %}
            </select>
        </p>
        <input type="submit" value="提交" class="btn btn-primary btn-block">
    </form>
{% endblock %}

2、book_edit.html

{% extends 'home.html' %}

{% block content %}
    <h3 class="text-center" style="color: #1b6d85 ">书籍修改</h3>
    <form action="" method="post">
        <p>书名:<input type="text" name="title" class="form-control" value="{{ edit_obj.title }}"></p>
        <p>价格:<input type="text" name="price" class="form-control" value="{{ edit_obj.price }}"></p>
        <p>出版时间:<input type="date" name="publish_data" class="form-control" value="{{ edit_obj.publish_date|date:'Y-m-d' }}"></p>
        <p>作者:
            <select name="author" id="" class="form-control" multiple>
                {% for author_obj in author_queryset %}
                    {% if author_obj in edit_obj.authors.all %}
                        <option value="{{ author_obj.pk }}" selected>
                            {{ author_obj.name }}
                        </option>
                    {% else %}
                        <option value="{{ author_obj.pk }}">
                            {{ author_obj.name }}
                        </option>
                    {% endif %}
                {% endfor %}
            </select>
        </p>
        <p>出版社:
            <select name="publish" id="" class="form-control">
                {% for publish_obj in publish_queryset %}
                    {% if publish_obj == edit_obj.publishs %}
                        <option value="{{ publish_obj.pk }}" selected>
                            {{ publish_obj.name }}
                        </option>
                    {% else %}
                        <option value="{{ publish_obj.pk }}">
                            {{ publish_obj.name }}
                        </option>
                    {% endif %}
                {% endfor %}

            </select>
        </p>
        <p><input type="submit" value="提交" class="btn btn-block btn-primary"></p>
    </form>
{% endblock %}

3、book_list.html

{% extends 'home.html' %}

{% block content %}

    <div class="row">
      <div class="col-lg-6">
        <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">Go!</button>
          </span>
        </div><!-- /input-group -->
      </div><!-- /.col-lg-6 -->
      <div class="col-lg-6">
        <a href="{% url 'book_add' %}" class="btn btn-primary btn-sm pull-right">添加</a>
      </div>
    </div><!-- /.row -->
    <br>
    <table class="table table-striped table-hover">
        <thead>
            <tr>
                <th>编号</th>
                <th>书名</th>
                <th>作者</th>
                <th>价格</th>
                <th>出版社</th>
                <th>出版时间</th>
                <th>操作</th>
            </tr>
        </thead>
        <tbody>
            {% for book_obj in book_queryset %}
                <tr>
                    <th>{{ forloop.counter }}</th>
                    <th>{{ book_obj.title }}</th>
                    <th>
                        {% for author_obj in book_obj.authors.all %}
                            {% if forloop.last %}
                                {{ author_obj.name }}
                            {% else %}
                                {{ author_obj.name }}、
                            {% endif %}
                        {% endfor %}
                    </th>
                    <th>{{ book_obj.price }}</th>
                    <th>{{ book_obj.publishs.name }}</th>
                    <th>{{ book_obj.publish_date|date:'Y-m-d' }}</th>
                    <th>
                        <a href="{% url 'book_edit' book_obj.pk %}" class="btn btn-primary btn-xs">修改</a>
                        <a href="{% url 'book_delete' book_obj.pk %}" class="btn btn-danger btn-xs">删除</a>
                    </th>
                </tr>
            {% endfor %}
        </tbody>
    </table>
{% endblock %}

4、home.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>图书管理系统</title>
    <link href="https://cdn.bootcss.com/twitter-bootstrap/3.4.1/css/bootstrap.min.css" rel="stylesheet">
    <script src="https://cdn.bootcss.com/jquery/3.3.1/jquery.min.js"></script>
    <script src="https://cdn.bootcss.com/twitter-bootstrap/3.4.1/js/bootstrap.min.js"></script>
    {% block css %}

    {% endblock %}
</head>
<body>
    <nav class="navbar navbar-inverse">
          <div class="container-fluid">
            <!-- Brand and toggle get grouped for better mobile display -->
            <div class="navbar-header">
              <button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#bs-example-navbar-collapse-1" aria-expanded="false">
                <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="#">图书管理系统</a>
            </div>

            <!-- Collect the nav links, forms, and other content for toggling -->
            <div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
              <ul class="nav navbar-nav">
                <li class="active"><a href="#">图书 <span class="sr-only">(current)</span></a></li>
                <li><a href="#">作者</a></li>
                <li class="dropdown">
                  <a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="false">更多 <span class="caret"></span></a>
                  <ul class="dropdown-menu">
                    <li><a href="#">Action</a></li>
                    <li><a href="#">Another action</a></li>
                    <li><a href="#">Something else here</a></li>
                    <li role="separator" class="divider"></li>
                    <li><a href="#">Separated link</a></li>
                    <li role="separator" class="divider"></li>
                    <li><a href="#">One more separated link</a></li>
                  </ul>
                </li>
              </ul>
              <form class="navbar-form navbar-left">
                <div class="form-group">
                  <input type="text" class="form-control" placeholder="Search">
                </div>
                <button type="submit" class="btn btn-default">Submit</button>
              </form>
              <ul class="nav navbar-nav navbar-right">
                <li><a href="#">Jason</a></li>
                <li class="dropdown">
                  <a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="false">更多操作 <span class="caret"></span></a>
                  <ul class="dropdown-menu">
                    <li><a href="#">Action</a></li>
                    <li><a href="#">Another action</a></li>
                    <li><a href="#">Something else here</a></li>
                    <li role="separator" class="divider"></li>
                    <li><a href="#">Separated link</a></li>
                  </ul>
                </li>
              </ul>
            </div><!-- /.navbar-collapse -->
          </div><!-- /.container-fluid -->
    </nav>
    <div class="container-fluid">
        <div class="row">
            <div class="col-md-3 ">
                <div class="list-group">
                  <a href="{% url 'home' %}" class="list-group-item active">首页</a>
                  <a href="{% url 'book_list' %}" class="list-group-item">图书列表</a>
                  <a href="#" class="list-group-item">作者列表</a>
                  <a href="#" class="list-group-item">出版社列表</a>
                  <a href="#" class="list-group-item">更多操作</a>
                </div>
            </div>
            <div class="col-md-9">
                <div class="panel panel-primary">
                  <div class="panel-heading">
                    <h3 class="panel-title">图书管理系统 <span class="glyphicon glyphicon-book pull-right"></span></h3>
                  </div>
                    <div class="panel-body">
                    {% block content %}
                        <div id="carousel-example-generic" class="carousel slide" data-ride="carousel">
                        <ol class="carousel-indicators">
                          <li data-target="#carousel-example-generic" data-slide-to="0" class="active"></li>
                          <li data-target="#carousel-example-generic" data-slide-to="1" class=""></li>
                          <li data-target="#carousel-example-generic" data-slide-to="2" class=""></li>
                        </ol>
                        <div class="carousel-inner" role="listbox">
                          <div class="item active">
                            <img data-src="holder.js/1140x500/auto/#777:#555/text:First slide" alt="First slide [1140x500]" src="data:image/svg+xml;base64,PD94bWwgdmVyc2lvbj0iMS4wIiBlbmNvZGluZz0iVVRGLTgiIHN0YW5kYWxvbmU9InllcyI/PjxzdmcgeG1sbnM9Imh0dHA6Ly93d3cudzMub3JnLzIwMDAvc3ZnIiB3aWR0aD0iMTE0MCIgaGVpZ2h0PSI1MDAiIHZpZXdCb3g9IjAgMCAxMTQwIDUwMCIgcHJlc2VydmVBc3BlY3RSYXRpbz0ibm9uZSI+PCEtLQpTb3VyY2UgVVJMOiBob2xkZXIuanMvMTE0MHg1MDAvYXV0by8jNzc3OiM1NTUvdGV4dDpGaXJzdCBzbGlkZQpDcmVhdGVkIHdpdGggSG9sZGVyLmpzIDIuNi4wLgpMZWFybiBtb3JlIGF0IGh0dHA6Ly9ob2xkZXJqcy5jb20KKGMpIDIwMTItMjAxNSBJdmFuIE1hbG9waW5za3kgLSBodHRwOi8vaW1za3kuY28KLS0+PGRlZnM+PHN0eWxlIHR5cGU9InRleHQvY3NzIj48IVtDREFUQVsjaG9sZGVyXzE3NDViNmFmZDEzIHRleHQgeyBmaWxsOiM1NTU7Zm9udC13ZWlnaHQ6Ym9sZDtmb250LWZhbWlseTpBcmlhbCwgSGVsdmV0aWNhLCBPcGVuIFNhbnMsIHNhbnMtc2VyaWYsIG1vbm9zcGFjZTtmb250LXNpemU6NTdwdCB9IF1dPjwvc3R5bGU+PC9kZWZzPjxnIGlkPSJob2xkZXJfMTc0NWI2YWZkMTMiPjxyZWN0IHdpZHRoPSIxMTQwIiBoZWlnaHQ9IjUwMCIgZmlsbD0iIzc3NyIvPjxnPjx0ZXh0IHg9IjM5MC41IiB5PSIyNzUuNSI+Rmlyc3Qgc2xpZGU8L3RleHQ+PC9nPjwvZz48L3N2Zz4=" data-holder-rendered="true">
                          </div>
                          <div class="item">
                            <img data-src="holder.js/1140x500/auto/#666:#444/text:Second slide" alt="Second slide [1140x500]" src="data:image/svg+xml;base64,PD94bWwgdmVyc2lvbj0iMS4wIiBlbmNvZGluZz0iVVRGLTgiIHN0YW5kYWxvbmU9InllcyI/PjxzdmcgeG1sbnM9Imh0dHA6Ly93d3cudzMub3JnLzIwMDAvc3ZnIiB3aWR0aD0iMTE0MCIgaGVpZ2h0PSI1MDAiIHZpZXdCb3g9IjAgMCAxMTQwIDUwMCIgcHJlc2VydmVBc3BlY3RSYXRpbz0ibm9uZSI+PCEtLQpTb3VyY2UgVVJMOiBob2xkZXIuanMvMTE0MHg1MDAvYXV0by8jNjY2OiM0NDQvdGV4dDpTZWNvbmQgc2xpZGUKQ3JlYXRlZCB3aXRoIEhvbGRlci5qcyAyLjYuMC4KTGVhcm4gbW9yZSBhdCBodHRwOi8vaG9sZGVyanMuY29tCihjKSAyMDEyLTIwMTUgSXZhbiBNYWxvcGluc2t5IC0gaHR0cDovL2ltc2t5LmNvCi0tPjxkZWZzPjxzdHlsZSB0eXBlPSJ0ZXh0L2NzcyI+PCFbQ0RBVEFbI2hvbGRlcl8xNzQ1YjZhYmFkNCB0ZXh0IHsgZmlsbDojNDQ0O2ZvbnQtd2VpZ2h0OmJvbGQ7Zm9udC1mYW1pbHk6QXJpYWwsIEhlbHZldGljYSwgT3BlbiBTYW5zLCBzYW5zLXNlcmlmLCBtb25vc3BhY2U7Zm9udC1zaXplOjU3cHQgfSBdXT48L3N0eWxlPjwvZGVmcz48ZyBpZD0iaG9sZGVyXzE3NDViNmFiYWQ0Ij48cmVjdCB3aWR0aD0iMTE0MCIgaGVpZ2h0PSI1MDAiIGZpbGw9IiM2NjYiLz48Zz48dGV4dCB4PSIzMzUuNTkzNzUiIHk9IjI3NS41Ij5TZWNvbmQgc2xpZGU8L3RleHQ+PC9nPjwvZz48L3N2Zz4=" data-holder-rendered="true">
                          </div>
                          <div class="item">
                            <img data-src="holder.js/1140x500/auto/#555:#333/text:Third slide" alt="Third slide [1140x500]" src="data:image/svg+xml;base64,PD94bWwgdmVyc2lvbj0iMS4wIiBlbmNvZGluZz0iVVRGLTgiIHN0YW5kYWxvbmU9InllcyI/PjxzdmcgeG1sbnM9Imh0dHA6Ly93d3cudzMub3JnLzIwMDAvc3ZnIiB3aWR0aD0iMTE0MCIgaGVpZ2h0PSI1MDAiIHZpZXdCb3g9IjAgMCAxMTQwIDUwMCIgcHJlc2VydmVBc3BlY3RSYXRpbz0ibm9uZSI+PCEtLQpTb3VyY2UgVVJMOiBob2xkZXIuanMvMTE0MHg1MDAvYXV0by8jNTU1OiMzMzMvdGV4dDpUaGlyZCBzbGlkZQpDcmVhdGVkIHdpdGggSG9sZGVyLmpzIDIuNi4wLgpMZWFybiBtb3JlIGF0IGh0dHA6Ly9ob2xkZXJqcy5jb20KKGMpIDIwMTItMjAxNSBJdmFuIE1hbG9waW5za3kgLSBodHRwOi8vaW1za3kuY28KLS0+PGRlZnM+PHN0eWxlIHR5cGU9InRleHQvY3NzIj48IVtDREFUQVsjaG9sZGVyXzE3NDViNmFjZmIyIHRleHQgeyBmaWxsOiMzMzM7Zm9udC13ZWlnaHQ6Ym9sZDtmb250LWZhbWlseTpBcmlhbCwgSGVsdmV0aWNhLCBPcGVuIFNhbnMsIHNhbnMtc2VyaWYsIG1vbm9zcGFjZTtmb250LXNpemU6NTdwdCB9IF1dPjwvc3R5bGU+PC9kZWZzPjxnIGlkPSJob2xkZXJfMTc0NWI2YWNmYjIiPjxyZWN0IHdpZHRoPSIxMTQwIiBoZWlnaHQ9IjUwMCIgZmlsbD0iIzU1NSIvPjxnPjx0ZXh0IHg9IjM3Ny44NTkzNzUiIHk9IjI3NS41Ij5UaGlyZCBzbGlkZTwvdGV4dD48L2c+PC9nPjwvc3ZnPg==" data-holder-rendered="true">
                          </div>
                        </div>
                        <a class="left carousel-control" href="#carousel-example-generic" role="button" data-slide="prev">
                          <span class="glyphicon glyphicon-chevron-left" aria-hidden="true"></span>
                          <span class="sr-only">Previous</span>
                        </a>
                        <a class="right carousel-control" href="#carousel-example-generic" role="button" data-slide="next">
                          <span class="glyphicon glyphicon-chevron-right" aria-hidden="true"></span>
                          <span class="sr-only">Next</span>
                        </a>
                      </div>
                        <div class="row">
                    <div class="col-sm-6 col-md-4">
                      <div class="thumbnail">
                      <img src="https://dss1.bdstatic.com/70cFvXSh_Q1YnxGkpoWK1HF6hhy/it/u=3190214567,3115602234&fm=26&gp=0.jpg" alt="美女">
                      <div class="caption">
                        <h3>Thumbnail label</h3>
                        <p>...</p>
                        <p><a href="#" class="btn btn-primary" role="button">Button</a> <a href="#" class="btn btn-default" role="button">Button</a></p>
                      </div>
                    </div>
                  </div>
                    <div class="col-sm-6 col-md-4">
                      <div class="thumbnail">
                      <img src="https://ss0.bdstatic.com/70cFvHSh_Q1YnxGkpoWK1HF6hhy/it/u=246999383,416183293&fm=26&gp=0.jpg" alt="美女">
                      <div class="caption">
                        <h3>Thumbnail label</h3>
                        <p>...</p>
                        <p><a href="#" class="btn btn-primary" role="button">Button</a> <a href="#" class="btn btn-default" role="button">Button</a></p>
                      </div>
                    </div>
                  </div>
                    <div class="col-sm-6 col-md-4">
                      <div class="thumbnail">
                      <img src="https://dss1.bdstatic.com/70cFuXSh_Q1YnxGkpoWK1HF6hhy/it/u=267354473,1106740650&fm=26&gp=0.jpg" alt="美女">
                      <div class="caption">
                        <h3>Thumbnail label</h3>
                        <p>...</p>
                        <p><a href="#" class="btn btn-primary" role="button">Button</a> <a href="#" class="btn btn-default" role="button">Button</a></p>
                      </div>
                    </div>
                  </div>
                </div>
                    {% endblock %}

                  </div>
                </div>
            </div>
        </div>
    </div>
{% block js %}

{% endblock %}
</body>
</html>

posted @ 2021-01-26 15:39  今天捡到一百块钱  阅读(429)  评论(0编辑  收藏  举报