图书管理系统系列之展示,修改,添加,删除
1|0settings.py配置
1.当前app的应用名称添加进去 INSTALLED_APPS = [ app01, #简写就行 ] 2.MIDDLEWARE中把csrf注释掉,防止post请求发送失败 3.DATABASES = { 'dafault':{ 'ENGINE':'django.db.backend.mysql', 'NAME':'booksys', #数据库名称 'HOST':'127.0.0.1',#ip 'PORT':3306, #端口 'USER':'root', 'PASSWORD':'666', 'CHARSET':'utf-8', } } 4.静态文件路径的配置 STATIC_URL = '/static/' STATICFILES_DIRS=[ os.path.join(BASE_DIR,'static'),#里面的static表示的是你自己创的静态文件名称 ]
2|0models.py文件
from django.db import models class Book(models.Model): id = models.AutoField(primary_key=True)#可以不写默认会自动创建 title = models.CharField(max_length=64,null=True) state = models.BooleanField(default=True) pub_date = models.DateField(null=True) price=models.DecimalField(max_digits=8,decimal_places=2,null=True) publish = models.CharField(max_length=32) def __str__(self): return self.title +'价格'+str(self.price)
3|0url配置
from django.conf.urls import url from django.contrib import admin from app01 import views urlpatterns = [ #展示 url(r'^book_list',views.book_list,name='book_list'), #添加 url(r'^add_book',views.add_book,name='add_book'), #修改 url(r'^edit_book/(\d+)/',views.edit_book,name='edit_book'), #删除 url(r'^delete_book/(\d+)/',views.delete_book,name='delete_book'), ]
4|0views视图函数
from django.shortcuts import render,HttpResponse,redirect from app01 import models from django.urls import reverse #url别名反向解析,通过name别名对应的数据,解析出我们的url路径 #展示 def book_list(request): books=models.Book.objects.all() return render(request,'book_list.html',{'books':books}) #添加 def add_book(request): if request.method == 'GET': return render(request,'add_book.html') else: print(request.POST)#获取post请求过来的数据QueryDict类型 title = request.POST.get('title') price = request.POST.get('price') pub_date = request.POST.get('pub_date') publish = request.POST.get('publish') #操作模型类,添加数据 ret = models.Book.objects.create( title=title, price=price, pub_date=pub_date, publish=publish, ) return redirect('/book_list/')#重定向 #编辑页面 def edit_book(request,id): obj_list = models.Book.objects.filter(pk=id)#pk就相当于数据库中字段id if request.method == 'GET': obj = obj_list.first() #获取queryset类型中第一个模型类对象 return render(request,'edit_book.html',{'obj':obj}) else: print(request.POST.dict())#dict()方法能将QueryDict类型数据转换为普通字典类型数据,这边用dict()方法是因为price是decimal类型数据, #print(type(request.POST))#querydict类型 obj_list.update(**request.POST.dict()) return redirect('book_list')#通过redirect来进行页面跳转时,redirect方法里面直接写url别名就可以,内部会自动帮我们完成url别名反向解析 def delete_book(request,id): models.Book.objects.filter(pk=id).delete() return redirect('book_list')
- booklist.html内容
{% load static %} #加载静态文件 <!DOCTYPE html> <html lang='en'> <head> <meta charset='utf-8'> <title>Title</title> <link rel='stylesheet' href="{% static 'bootstrap下的css样式路径'%}"> </head> <body> <div class="container"> <h1>书籍展示</h1> <div class="col-md-8 col-md-offset-2"> <a href={% url 'add_book' %} class='btn btn-primary'>添加书籍</a> <table style="margin-top: 10px;" class="table table-bordered table-striped table-hover"> <thead> <tr> <th>编号</th> <th>书籍名称</th> <th>价格</th> <th>出版日期</th> <th>出版社</th> <th>操作</th> </tr> </thead> <tbody> {% for book in books %} <tr> <td>{{ forloop.counter }}</td> <td>{{ book.title }}</td> <td>{{ book.price }}</td> <td>{{ book.pub_date|date:'Y-m-d' }}</td> <td>{{ book.publish }}</td> <td> <a href="{% url 'edit_book' book.id %}" class="btn btn-warning">编辑</a> <a href="{% url 'delete_book' book.pk %}" class="btn btn-danger">删除</a> </td> </tr> {% endfor %} </tbody> </table> </div> </div> </div> </body> </html>
- add_book.html内容
{% load static %} <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> <link rel="stylesheet" href="{% static 'plugins/bootstrap-3.3.7-dist/css/bootstrap.min.css' %}"> </head> <body> <div class="container"> <h1>添加书籍</h1> <div class="row"> <div class="col-md-8 col-md-offset-2"> <form action="/add_book/" method="post"> <div class="form-group"> <label for="title">书籍名称</label> <input type="text" class="form-control" id="title" placeholder="书籍名称" name="title"> </div> <div class="form-group"> <label for="price">价格</label> <input type="text" class="form-control" id="price" placeholder="书籍名称" name="price"> </div> <div class="form-group"> <label for="pub_date">出版日期</label> <input type="date" class="form-control" id="pub_date" placeholder="书籍名称" name="pub_date"> </div> <div class="form-group"> <label for="publish">出版社</label> <input type="text" class="form-control" id="publish" placeholder="书籍名称" name="publish"> </div> <button class="btn btn-success pull-right">提交</button> </form> </div> </div> </div> </body> </html>
- edit_book.html
{% load static %} <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> <link rel="stylesheet" href="{% static 'plugins/bootstrap-3.3.7-dist/css/bootstrap.min.css' %}"> </head> <body> <div class="container"> <h1>编辑书籍</h1> <div class="row"> <div class="col-md-8 col-md-offset-2"> {# <form action="/edit_book/{{ id }}/" method="post">#} {# <form action="{% url 'edit_book' id %}" method="post">#} {# <form action="{% url 'edit_book' obj.id %}" method="post">#} {# <form action="{% url 'edit_book' obj.pk %}" method="post">#} <div class="form-group"> <label for="title">书籍名称</label> <input type="text" class="form-control" id="title" placeholder="书籍名称" name="title" value="{{ obj.title }}"> </div> <div class="form-group"> <label for="price">价格</label> <input type="text" class="form-control" id="price" placeholder="价格" name="price" value="{{ obj.price }}"> </div> <div class="form-group"> <label for="pub_date">出版日期</label> <input type="date" class="form-control" id="pub_date" placeholder="出版日期" name="pub_date" value="{{ obj.pub_date|date:'Y-m-d' }}"> </div> <div class="form-group"> <label for="publish">出版社</label> <input type="text" class="form-control" id="publish" placeholder="出版社" name="publish" value="{{ obj.publish }}"> </div> <button class="btn btn-success pull-right">提交</button> </form> </div> </div> </div> </body> </html>
5|0知识点
request.POST.dict() #dict()方法可以将QueryDict类型数据转换为普通字典 obj_list.update( **request.POST.dict() #打散的形式传参 k=v )
__EOF__

本文作者:404 Not Found
本文链接:https://www.cnblogs.com/weiweivip666/p/13352587.html
关于博主:可能又在睡觉
版权声明:转载请注明出处
声援博主:如果看到我睡觉请喊我去学习
本文链接:https://www.cnblogs.com/weiweivip666/p/13352587.html
关于博主:可能又在睡觉
版权声明:转载请注明出处
声援博主:如果看到我睡觉请喊我去学习
-------------------------------------------
个性签名:代码过万,键盘敲烂!!!
如果觉得这篇文章对你有小小的帮助的话,记得在右下角点个“推荐”哦,博主在此感谢!
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· winform 绘制太阳,地球,月球 运作规律
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 上周热点回顾(3.3-3.9)
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人