demo Django-基础书籍添加删除(单表)
2024-09-06 20:21:43
小demo使用----
1.pycharm-2019.2
2.python-3.7.2
3.mysql-5.7.25
4.django-2.2.4
前端实例实现效果
添加书籍
查看书籍
删除书籍
编辑书籍
部分代码:
部分设置,以及文件分布
数据库提前建好的情况下,建表格
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)
models.py
控制器
"""bookms URL Configuration The `urlpatterns` list routes URLs to views. For more information please see:
https://docs.djangoproject.com/en/2.2/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),
re_path(r"books/(\d+)/delete", views.delbook),
re_path(r"books/(\d+)/change", views.changebook),
]
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 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")
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})
views.py
<!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>查看书籍</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 }}/delete" class="btn btn-danger">删除</a></td>
<td><a href="/books/{{ book.pk }}/change" class="btn btn-info">编辑</a></td>
</tr>
{% endfor %} </tbody>
</table>
</div>
</div>
</div>
</body>
</html>
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>添加书籍</h3> <div class="container">
<div class="row">
<div class="col-md-6 col-md-offset-3">
<a href="/books" class="btn btn-primary">查看书籍</a>
<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>
addbook.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>编辑书籍</h3> <div class="container">
<div class="row">
<div class="col-md-6 col-md-offset-3">
<a href="/books" class="btn btn-primary">查看书籍</a>
<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>
changebook.html
最新文章
- iOS开发中手机号码和价格金额有效性判断及特殊字符的限制
- 一款全兼容的播放器 videojs
- HTML5新标签video在iOS上默认全屏播放
- using 语句中使用的类型必须可隐式转换为“System.IDisposable
- Manifesto of the Communist Party
- Python + OpenCV2 系列:3 - python 字符串,类,编码规范
- 升级xcode7的问题:使用shareSDK,坑的你两眼泪汪汪
- 《DSP using MATLAB》示例Example5.5
- [MSSQL2008]Spatial Data in SQL Server 2008 - 根据经纬度计算两点间距离
- uva 11210 Chinese Mahjong(暴力搜索)
- button按钮点击不刷新(前端交流学习:452892873)
- 使用cxf做webservice接口调用
- C#复习笔记(3)--C#2:解决C#1的问题(结束C#2的内容:最后一些特性)
- poj1179 环形+区间dp
- centos7 apache设置伪静态 开启rewrite_module
- bzoj 4591 超能粒子炮&#183;改 - Lucas
- Codeforces Beta Round #16 div 2 C.Monitor最大公约数
- 10-09 Linux的文件系统介绍以及各种设备的说明
- CSS- 文本超出指定宽度后隐藏并显示为省略号
- Mysql:1236常见错误
热门文章
- 教你如何快速上手markdown语法,编写技术博客(史上最全最简,用MarkDown写博客)
- 使用IDEA详解Spring中依赖注入的类型(上)
- 从Golang中open的实现方式看Golang的语言设计
- Html / XHtml 解析 - Parsing Html and XHtml
- css浮动(float)详解
- 如何用Python实现do...while语句
- 使用helm安装jenkin和gitlab
- Linux设备中的UUID
- [Python]pip 国内源
- Bookshelf 2 01背包