美文网首页
模拟图书馆作业(四)

模拟图书馆作业(四)

作者: Chris0Yang | 来源:发表于2021-03-10 15:57 被阅读0次

    图书管理系统

    实现功能:Books单表的增删改查

    下载Bootstrap前端框架包

    https://v3.bootcss.com/getting-started/#download

    项目目录

    image.png

    前端逻辑

    添加书籍页面(addbook.html)

    访问http://IP:端口/addbook/

    image.png
    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>添加书籍</title>
    
        <link rel="stylesheet" href="/static/bs/dist/css/bootstrap.css">
        <style>
            .container {
                margin-top: 100px;
            }
    
            .btn {
                margin-top: 10px;
            }
        </style>
    </head>
    <body>
    <div class="container">
        <div class="row">
            <div class="col-md-6 col-md-offset-3">
                <form action="" method="post">
                    {% csrf_token %}
                    <div>
                        <label for="">书籍名称</label>
                        <input type="text" class="form-control" name="title">
                    </div>
                    <div>
                        <label for="">价格</label>
                        <input type="text" class="form-control" name="price">
                    </div>
                    <div>
                        <label for="">出版日期</label>
                        <input type="date" class="form-control" name="date">
                    </div>
                    <div>
                        <label for="">出版社</label>
                        <input type="text" class="form-control" name="publish">
                    </div>
                    <input type="submit" class="btn btn-success pull-right">
                </form>
            </div>
        </div>
    </div>
    </body>
    </html>
    

    查看书籍页面(books.html)

    访问http://IP:端口/books/

    image.png
    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>查看书籍</title>
        <link rel="stylesheet" href="/static/bs/dist/css/bootstrap.css">
        <style>
            .container {
                margin-top: 100px;
            }
    
            .btn {
                margin-top: 10px;
            }
        </style>
    </head>
    <body>
    <div class="container">
        <div class="row">
            <div class="col-md-6 col-md-offset-3">
                <a href="/addbook/" class="btn btn-primary">添加书籍</a>
                <table class="table table-striped table-bordered">
                    <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.price }}</td>
                            <td>{{ book.pub_date|date:'Y-m-d' }}</td>
                            <td>{{ book.publish }}</td>
                            <td><a href="/books/{{ book.pk }}/change" class="btn btn-info">编辑</td>
                            <td><a href="/books/{{ book.pk }}/delete" class="btn btn-danger">删除</td>
                        </tr>
                    {% endfor %}
                    </tbody>
                </table>
            </div>
        </div>
    </div>
    </body>
    </html>
    

    编辑书籍页面(changebook.html)

    image.png
    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>编辑书籍</title>
        <link rel="stylesheet" href="/static/bs/dist/css/bootstrap.css">
        <style>
            .container {
                margin-top: 100px;
            }
    
            .btn {
                margin-top: 10px;
            }
        </style>
    </head>
    <body>
    <div class="container">
        <div class="row">
            <div class="col-md-6 col-md-offset-3">
                <form action="" method="post">
                    {% csrf_token %}
                    <div>
                        <label for="">书籍名称</label>
                        <input type="text" class="form-control" name="title" value="{{ book_obj.title }}">
                    </div>
                    <div>
                        <label for="">价格</label>
                        <input type="text" class="form-control" name="price" value="{{ book_obj.price }}">
                    </div>
                    <div>
                        <label for="">出版日期</label>
                        <input type="date" class="form-control" name="date" value="{{ book_obj.pub_date|date:'Y-m-d' }}">
                    </div>
                    <div>
                        <label for="">出版社</label>
                        <input type="text" class="form-control" name="publish" value="{{ book_obj.publish }}">
                    </div>
                    <input type="submit" class="btn btn-success pull-right">
                </form>
            </div>
        </div>
    </div>
    </body>
    </html>
    

    后端逻辑

    配置数据库操作(重要)

    提示:注释之前DATABASES内容

    DATABASES = {
        'default': {
            'ENGINE': 'django.db.backends.mysql',
            'NAME': 'bookms',        # 要连接的数据库,连接前需要创建好
            'USER': 'root',       # 连接数据库的用户名
            'PASSWORD': '123',    # 连接数据库的密码
            'HOST': '127.0.0.1',  # 连接主机,默认本级
            'PORT': 3306          # 端口 默认3306
        }
    }
    
    LOGGING = {
        'version': 1,
        'disable_existing_loggers': False,
        'handlers': {
            'console': {
                'level': 'DEBUG',
                'class': 'logging.StreamHandler',
            },
        },
        'loggers': {
            'django.db.backends': {
                'handlers': ['console'],
                'propagate': True,
                'level': 'DEBUG',
            },
        }
    }
    
    
    

    需要通过两条数据库迁移命令,即可在指定的数据库中创建表

    mysql> create database orm;
    mysql> use orm;
    mysql> show tables;
    mysql> desc XXXX_book;
    
    python manage.py makemigrations
    python manage.py migrate
    

    app01 的路由层urls.py

    (\d+) 是匹配 [0~9] ( 以下链接是正则表达式内容 )

    https://www.liaoxuefeng.com/wiki/1016959663602400/1017639890281664

    from django.contrib import admin
    from django.urls import path, re_path
    
    from app01 import views
    
    urlpatterns = [
        path('admin/', admin.site.urls),
        path('addbook/', views.addbook),
        path('books/', views.books),
        re_path(r"books/(\d+)/change", views.changebook),
        re_path(r"books/(\d+)/delete", views.delbook),
    ]
    
    

    app01 的路由层urls.py

    from django.shortcuts import render, HttpResponse, redirect
    
    # Create your views here.
    from app01.models import Book
    
    
    def addbook(request):
        if request.method == "POST":
            title = request.POST.get("title")
            price = request.POST.get("price")
            date = request.POST.get("date")
            publish = request.POST.get("publish")
    
            book_obj = Book.objects.create(title=title, price=price, pub_date=date, publish=publish)
    
            return redirect("/books/")
    
        return render(request, "addbook.html")
    
    
    def books(request):
        book_list = Book.objects.all()
    
        return render(request, "books.html", locals())
    
    
    def changebook(request, id):
        book_obj = Book.objects.filter(id=id).first()
    
        if request.method == "POST":
            title = request.POST.get("title")
            price = request.POST.get("price")
            date = request.POST.get("date")
            publish = request.POST.get("publish")
    
            Book.objects.filter(id=id).update(title=title, price=price, pub_date=date, publish=publish)
    
            return redirect("/books/")
    
        return render(request, "changebook.html", {"book_obj": book_obj})
    
    
    def delbook(request, id):
        Book.objects.filter(id=id).delete()
    
        return redirect("/books/")
    
    

    相关文章

      网友评论

          本文标题:模拟图书馆作业(四)

          本文链接:https://www.haomeiwen.com/subject/qcpwqltx.html