• 简易的图书管理系统代码(实现对数据的增删改查)


    简易的图书管理系统


    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'^$', views.home, name='home1'),
        url(r'^book_list', views.book_list, name='book_show'),
        url(r'^add_book', views.add_book, name='add'),
        url(r'^edit_book/(?Pd+)', views.edit_book, name='edit'),
        url(r'^delete_book/(d+)', views.delete_book, name='delete')
    ]   
    
    models.py
    
        from django.db import models
    # Create your models here.
    # 图书表
    class Book(models.Model):
        # 书名
        title = models.CharField(max_length=64)
        # 出版日期
        publish_date = models.DateField(auto_now_add=True)
        # 价格
        price = models.CharField(max_length=254)
        # 图书与出版社   一对多
        publish = models.ForeignKey(to='Publish')
        # 图书与作者   多对多
        authors = models.ManyToManyField(to='Author')
    # 出版社表
    class Publish(models.Model):
        name = models.CharField(max_length=64)
        addr = models.CharField(max_length=254)
    # 作者表
    class Author(models.Model):
        name = models.CharField(max_length=64)
        age = models.IntegerField()
        # 作者表与作者细节表     一对一
        author_detail = models.OneToOneField(to='AuthorDetail')
    # 作者细节表
    class AuthorDetail(models.Model):
        gender_choices = [
            (1, '男'),
            (2, '女'),
            (3, '其他'),
        ]
        gender = models.IntegerField(choices=gender_choices)
        phone = models.CharField(max_length=11)
    
    views.py
    
    from django.shortcuts import render, redirect, reverse
    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_show.html', locals())
    def add_book(request):
        if request.method == 'POST':
            title = request.POST.get('title')
            publish_date = request.POST.get('publish_date')
            price = request.POST.get('price')
            publish_id = request.POST.get('publish')
            authors_lists = request.POST.getlist('authors')
            # 操作数据库
            # 书籍表
            book_obj = models.Book.objects.create(title=title, publish_date=publish_date, price=price,                                            publish_id=publish_id)
            # 书籍与作者关系表
            book_obj.authors.add(*authors_lists)
            # 后端反向解析
            _url = reverse('book_show')
            # 后端重定向
            return redirect(_url)
        publish_queryset = models.Publish.objects.all()
        author_queryset = models.Author.objects.all()
        return render(request, 'add_book.html', locals())
    def edit_book(request, edit_id):
        edit_obj = models.Book.objects.filter(pk=edit_id).first()
        if request.method == 'POST':
            title = request.POST.get('title')
            publish_date = request.POST.get('publish_date')
            price = request.POST.get('price')
            publish_id = request.POST.get('publish')
            authors_lists = request.POST.getlist('authors')
            # 操作数据库
            # 书籍表    models.Book.objects.filter(pk=edit_id).update(title=title, publish_date=publish_date, price=price,                                                    publish_id=publish_id)
            # 书籍与作者关系表
            edit_obj.authors.set(authors_lists)
            # 后端反向解析
            _url = reverse('book_show')
            # 后端重定向
            return redirect(_url)
        publish_queryset = models.Publish.objects.all()
        author_queryset = models.Author.objects.all()
        return render(request, 'edit_book.html', locals())
    def delete_book(request, delete_id):
        models.Book.objects.filter(pk=delete_id).delete()
        _url = reverse('book_show')
        return redirect(_url)
    

    home.html文件

    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>Title</title>
        <script src="https://cdn.bootcss.com/jquery/3.4.1/jquery.js"></script>
    {#    <link href="https://cdn.bootcss.com/twitter-bootstrap/3.4.1/css/bootstrap.min.css" rel="stylesheet">#}
    {#    <script src="https://cdn.bootcss.com/twitter-bootstrap/3.4.1/js/bootstrap.min.js"></script>#}
        {% load static %}
        <link rel="stylesheet" href="{% static 'bootstrap-3.3.7-dist/css/bootstrap.min.css' %}">
        <script src="{% static 'bootstrap-3.3.7-dist/js/bootstrap.min.js' %}"></script>
    </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="#">BMS</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="#">yyx</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="col-md-4">
            <div class="list-group">
              <a href="{% url 'home1' %}" class="list-group-item active">
                首页
              </a>
              <a href="{% url 'book_show' %}" 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-8">
            <div class="panel panel-default">
              <div class="panel-heading">
                <h3 class="panel-title text-center">BMS系统</h3>
              </div>
              <div class="panel-body">
                  {% block content %}
                   <div class="jumbotron">
                  <h1>欢迎来到BMS系统</h1>
                  <p>在这里你可以饱阅天下群书!</p>
                  <p><a class="btn btn-danger btn-lg" href="#" role="button">比心</a></p>
                </div>
                  {% endblock %}
    
              </div>
            </div>
    
        </div>
    </div>
    
    </body>
    </html>
    

    book_show.py文件

    {% extends 'home.html' %}
    {% block content %}
    <div>
        <a href="{% url 'add' %}" class="btn btn-warning">新增</a>
    </div>
        <br>
    <table class="table table-bordered table-hover table-striped">
    
        <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>
                    <td>{{ forloop.counter }}</td>
                    <td>{{ book_obj.title }}</td>
                    <td>{{ book_obj.price }}</td>
                    <td>{{ book_obj.publish.name }}</td>
                    <td>{{ book_obj.publish_date|date:'Y-m-d' }}</td>
                    <td>
                        {% for author_obj in book_obj.authors.all %}
                            {% if forloop.last %}
                                {{ author_obj.name }}
                            {% else %}
                                {{ author_obj.name }},
                            {% endif %}
                        {% endfor %}
                        
                    </td>
                    <td>
                        <a href="{% url 'edit' book_obj.pk %}" class="btn btn-success btn-xs">编辑</a>
                        <a href="{% url 'delete' book_obj.pk %}" class="btn btn-success btn-xs">删除</a>
                    </td>
                </tr>
    
            {% endfor %}
    
        </tbody>
    </table>
    <nav aria-label="Page navigation" class="text-center">
      <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>
    
    {% endblock %}
    

    add_book.py文件

    {% extends 'home.html' %}
    {% block content %}
        <h2 class="text-center">新增图书</h2>
        <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="" class="form-control">
                    {% for publish_obj in publish_queryset %}
                        <option value="{{ publish_obj.pk }}">{{ publish_obj.name }}</option>
                        
                    {% endfor %}
    
         </select>
            </p>
          <p>
                 作者<select name="authors" id="" class="form-control" multiple>
                    {% for author_obj in author_queryset %}
                        <option value="{{ author_obj.pk }}">{{ author_obj.name }}</option>
                    {% endfor %}
    
    
          </select>
            </p>
            <input type="submit" class="btn btn-success pull-right">
        </form>
    
    
    {% endblock %}
    

    edit_book.py文件

    {% extends 'home.html' %}
    {% block content %}
        <h2 class="text-center">编辑图书</h2>
        <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_date" class="form-control" value="{{ edit_obj.publish_date|date:'Y-m-d' }}">
            </p>
         <p>
                 出版社<select name="publish" id="" class="form-control">
                    {% for publish_obj in publish_queryset %}
                        {% if edit_obj.publish == publish_obj %}
                            <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>
                 作者<select name="authors" 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>
            <input type="submit" class="btn btn-warning pull-right">
        </form>
    
    
    {% endblock %}
    
  • 相关阅读:
    Feature的开发和部署
    MOSS 2007应用如何修改上传文件大小及类型的限制
    学习Ajax的优秀网站
    Office SharePoint 权限开发
    Asp.net操作Excel汇总
    如何取到MOSS列表中item的链接
    解决MOSS、SharePoint的未知错误
    Ajax 之 XMLHttpRequest
    C#中从资源文件里加载文件
    linux 技巧:使用 screen 管理你的远程会话 [linux]
  • 原文地址:https://www.cnblogs.com/godlover/p/12180082.html
Copyright © 2020-2023  润新知