1. 程式人生 > >6.1 - 圖書管理系統

6.1 - 圖書管理系統

migration direct ont root token you -h ddb ogg

實現功能:book單表的增刪改查

技術分享圖片

代碼:

model.py

from django.db import models

# Create your models here.

class Book(models.Model):
    id = models.AutoField(primary_key=True)
    title = models.CharField(max_length=32)
    pub_date = models.DateField()
    price = models.DecimalField(max_digits=8, decimal_places=2)
    publish 
= models.CharField(max_length=32)

settings.py

DATABASES = {
    default: {
        ENGINE: django.db.backends.mysql,
        NAME: bookms,          # 要連接的數據庫,連接前需要創建好
        USER: root,            # 連接數據庫的用戶名
        PASSWORD: 123,         # 連接數據庫的密碼
        HOST: 127.0.0.1,       #
連接主機,默認本機 PORT: 3306 # 端口 默認3306 } } # 打印orm轉換過程中的sql LOGGING = { version: 1, disable_existing_loggers: False, handlers: { console:{ level:DEBUG, class:logging.StreamHandler, }, }, loggers: {
django.db.backends: { handlers: [console], propagate: True, level:DEBUG, }, } }

python manage.py makemigrations

python manage.py migrate

urls.py

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(checkbook/, views.checkbook), # 查看書籍
    re_path(rcheckbook/(\d+)/delete,views.delbook), # 刪除書籍
    re_path(rcheckbook/(\d+)/update, views.update), # 修改書籍
]

views.py

# -*- encoding:utf-8 -*-
from django.shortcuts import render,HttpResponse,redirect

from app01.models import Book
# Create your views here.

def addbook(request):

    if request.method == "POST":
        title = request.POST.get("title")
        price = request.POST.get("price")
        pub_date = request.POST.get("pub_date")
        publish = request.POST.get("publish")

        book_obj = Book.objects.create(title=title,price=price,pub_date=pub_date,publish=publish)

        # return HttpResponse(‘添加數據成功!‘)
        # return render(request,‘checkbook.html‘,locals())
        return redirect(/checkbook/)
    return render(request,addbook.html)

def checkbook(request):

    book_list = Book.objects.all()

    return render(request,checkbook.html,locals())

def delbook(request,id):

    Book.objects.filter(id=id).delete()

    # 重定向
    return redirect(/checkbook/)  # 兩次請求

def update(request,id):

    book_obj = Book.objects.filter(id=id).first()

    if request.method == POST:
        title = request.POST.get("title")
        price = request.POST.get("price")
        pub_date = request.POST.get("pub_date")
        publish = request.POST.get("publish")

        Book.objects.filter(id=id).update(title=title,price=price,pub_date=pub_date,publish=publish)

        return redirect(/checkbook/)

    return  render(request,updatebook.html,{book_obj:book_obj})

addbook.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <link rel="stylesheet" href="/static/bs/css/bootstrap.css">
    <title>Title</title>
    <style>
        .container{
            margin-top: 100px;
        }
        .btn{
            margin-top: 10px;
        }
    </style>
</head>
<body>

    <h3>添加書籍</h3>

    <div class="container">
        <div class="row project">
            <div class="col-md-4 col-md-offset-4">
                <div class="thumbnail" style="height: 288px;">
                    <form action="" method="post">
                       {#  為了通過防跨域請求  #}
                        {% csrf_token %}
                        <div class="form-grop">
                            <label for="bookname">書籍名稱</label>
                            <input type="text" class="form-control" name="title" placeholder="請輸入書籍名稱">
                        </div>
                        <div class="form-grop">
                            <label for="">價格</label>
                            <input type="text" class="form-control" name="price" placeholder="請輸入價格">
                        </div>
                        <div class="form-grop">
                            <label for="">出版日期</label>
                            <input type="date" class="form-control" name="pub_date" placeholder="請輸入出版日期">
                        </div>
                        <div class="form-grop">
                            <label for="">出版社</label>
                            <input type="text" class="form-control" name="publish" placeholder="請輸入出版社">
                        </div>

                        <input type="submit" class="btn btn-success pull-right" >

                    </form>
                </div>
            </div>
        </div>
    </div>

</body>
</html>

checkbook.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <link rel="stylesheet" href="/static/bs/css/bootstrap.css">
    <title>Title</title>
    <style>
        .container{
            margin-top: 100px;
        }
    </style>
</head>
<body>

    <h3>查看書籍</h3>

    <div class="container">
        <div class="row">
            <div class="col-md-6 col-md-offset-3">
{#                <div class="thumbnail">#}
                <a href="/addbook/" class="btn btn-info">添加書籍</a>
                <table class="table 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.price }}</td>
                                <td>{{ book.pub_date|date:"Y-m-d" }}</td>
                                {# date過濾器#}
                                <td>{{ book.publish }}</td>
                               {#book.pk這裏的pk是主鍵,也可以寫成book.id#}
                                <td><a href="/checkbook/{{ book.pk }}/delete" class="btn btn-danger">刪除</a></td>
                                <td><a href="/checkbook/{{ book.pk }}/update" class="btn btn-info">編輯</a></td>
                            </tr>
                        {% endfor %}

                    </tbody>
                </table>
{#                </div>#}
            </div>
        </div>
    </div>

</body>
</html>

updatebook.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <link rel="stylesheet" href="/static/bs/css/bootstrap.css">
    <title>Title</title>
    <style>
        .container{
            margin-top: 100px;
        }
        .btn{
            margin-top: 10px;
        }
    </style>
</head>
<body>

    <h3>編輯書籍</h3>

    <div class="container">
        <div class="row project">
            <div class="col-md-4 col-md-offset-4">
                <div class="thumbnail" style="height: 288px;">
                    <form action="" method="post">
                       {#  為了通過防跨域請求  #}
                        {% csrf_token %}
                        <div class="form-grop">
                            <label for="bookname">書籍名稱</label>
                            <input type="text" class="form-control" name="title" value="{{ book_obj.title }}">
                        </div>
                        <div class="form-grop">
                            <label for="">價格</label>
                            <input type="text" class="form-control" name="price" value="{{ book_obj.price }}">
                        </div>
                        <div class="form-grop">
                            <label for="">出版日期</label>
                            <input type="date" class="form-control" name="pub_date" value="{{ book_obj.pub_date|date:"Y-m-d" }}">
                        </div>
                        <div class="form-grop">
                            <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>
    </div>

</body>
</html>

6.1 - 圖書管理系統