Django之图书管理系统(2)

app01/views.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')
        print(title,price,date,publish)
        book_obj=Book.objects.create(title=title,price=price,pub_date=date,publish=publish)
        return redirect('/books/')
    return render(request,'addbook.html')
# def addbook_test(request):
#     book_obj = Book.objects.create(title="python葵花宝典", price=100, publish="苹果出版社", pub_date="2012-12-12")
#     return redirect('/books/')


def books(request):
    book_list=Book.objects.all()
    return render(request,'books.html',locals())

def delbook(request,id):
    Book.objects.filter(id=id).delete()
    #重定向
    return redirect('/books/')

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')
        print(title,price,date,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})

book/books.py

"""book URL Configuration

The `urlpatterns` list routes URLs to views. For more information please see:
    https://docs.djangoproject.com/en/2.1/topics/http/urls/
Examples:
Function views
    1. Add an import:  from my_app import views
    2. Add a URL to urlpatterns:  path('', views.home, name='home')
Class-based views
    1. Add an import:  from other_app.views import Home
    2. Add a URL to urlpatterns:  path('', Home.as_view(), name='home')
Including another URLconf
    1. Import the include() function: from django.urls import include, path
    2. Add a URL to urlpatterns:  path('blog/', include('blog.urls'))
"""
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),
    path('addbook_test/',views.addbook),
    re_path(r'books/(\d+)/delete',views.delbook),
    re_path(r'books/(\d+)/change',views.changebook),

]

books.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>

    <link rel="stylesheet" href="/static/bs/css/bootstrap.css">
    <style>
        .container{
            margin-top: 100px;
        }
        .btn{
            margin-top: 10px;
        }
    </style>
</head>
<body>

<h3 align="center">查看书籍</h3>

<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">编辑</a></td>
                              <td><a href="/books/{{ book.pk }}/delete" class="btn btn-danger">删除</a></td>
                          </tr>
                        {% endfor %}
                   </tbody>
             </table>
         </div>
     </div>
</div>
</body>
</html>

addbooks.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>addbook</title>
    <link rel="stylesheet" href="/static/bs/css/bootstrap.css">
    <style>
        .container{
            margin-top: 100px;
        }
        .btn{
            margin-top:10px;
        }
    </style>
</head>
<body>
<h3 align="center">添加书籍</h3>
<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>

changebooks.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>

    <link rel="stylesheet" href="/static/bs/css/bootstrap.css">
    <style>
        .container{
            margin-top: 100px;
        }
        .btn{
            margin-top: 10px;
        }
    </style>
</head>
<body>

<h3 align="center">编辑书籍</h3>

<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>

至此,简单的图书管理系统完结。
后台与前端交互的数据统一通过‘’request‘’获取,其实是对用户请求的一个解析,不管是添加书籍,还是删除或者编辑,也都是同理,由此延伸在多表查询,一对多,多对多等,均是同样的道理。

    原文作者:wangcc_sd
    原文地址: https://www.jianshu.com/p/5df697812c67
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞