day04 基于python Django 编写简单的图书管理系统--单表

整个目录如下:

首先html文件有三个:查看、新增和编辑页面
查看页面:book

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

</head>
<body>
<h3>图书馆</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="/book/{{ book.pk }}/delete" class="btn btn-danger">删除</a></td>
                        <td><a href="/book/{{ book.pk }}/change" class="btn btn-info">编辑</a></td>
                    </tr>
                {% endfor %}
                </tbody>
            </table>
        </div>
    </div>

</div>


</body>
</html>

增加数据页面addbook

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

编辑页面changebook

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

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

<h3>编辑书籍</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>

路由设置

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

视图设置:

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


def book(request):
    '''
    查看图书馆书籍
    :param request:
    :return:
    '''
    book_list = Book.objects.all()


    return render(request,'book.html',locals())


def addbook(request):
    '''
    添加书籍
    :param request:
    :return:
    '''
    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.create(title=title,price=price,pub_date=date,publish=publish,state=1)
        return redirect('/book/')

    return render(request,'addbook.html')

def delbook(request,id):
    Book.objects.filter(id=id).delete()
    return redirect('/book/')

def changebook(request,id):
        '''
    编辑书籍
    :param request: 
    :param id: 
    :return: 
    '''
    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('/book/')

    return render(request,'changebook.html',locals())


模型设置

  from django.db import models

# Create your models here.


class User(models.Model):
    id = models.AutoField(primary_key=True,verbose_name='主键')
    username = models.CharField(max_length=32,verbose_name='用户名')
    # password = models.IntegerField()
    password = models.CharField(verbose_name='密码',max_length=64)

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

    def __str__(self):
        return self.title

posted @   simon_T  阅读(236)  评论(0编辑  收藏  举报
编辑推荐:
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· AI技术革命,工作效率10个最佳AI工具
点击右上角即可分享
微信分享提示