day 47 Django 4的简单应用 创建简单的图书管理 (单表的增删改查)
前情提要
Django 已经学了大半.. 很多东西已经能够使用在生产环境当中
一:模糊查询
二:单表删除
三:单表修改
四:图书管理
图书管理操作
视图结构
A:路由层
A :配置路由文件
参数解析:
B :视图层
from django.shortcuts import render, HttpResponse, redirect
from django.urls import reverse
from app01 import models # Create your views here.
def book_list(request):
if request.method == "GET":
book_list = models.Book.objects.all()
# print(book_list)
return render(request, "book_list.html", {
"booklist": book_list })
else:
add_title1 = request.POST.get("title1")
add_price1 = request.POST.get("price1") # price
add_publish1 = request.POST.get("publish1")
add_date1 = request.POST.get("date1")
models.Book.objects.create(title=add_title1,
price=add_price1,
publish=add_publish1,
pub_date=add_date1) print(add_title1, add_price1, add_publish1, )
return render(request, "book_list.html")
def bobook_list(request):
if request.method == "GET":
book_list = models.Book.objects.all()
# print(book_list)
return render(request, "bo_booklist.html", {
"booklist": book_list })
else:
add_title1 = request.POST.get("title1")
add_price1 = request.POST.get("price1") # price
add_publish1 = request.POST.get("publish1")
add_date1 = request.POST.get("date1")
models.Book.objects.create(title=add_title1,
price=add_price1,
publish=add_publish1,
pub_date=add_date1) print(add_title1, add_price1, add_publish1, )
return render(request, "bo_booklist.html") def add_book(request):
if request.method == "GET":
return render(request, "add_book.html")
else:
add_title = request.POST.get("title") # title
add_price = request.POST.get("price") # price
add_publish = request.POST.get("publish")
add_date = request.POST.get("pub_date")
models.Book.objects.create(title=add_title,
price=add_price,
publish=add_publish,
pub_date=add_date)
return redirect(reverse("booklist"))
def boadd_book(request):
if request.method == "GET":
return render(request, "boadd_book.html")
else:
add_title = request.POST.get("title") # title
add_price = request.POST.get("price") # price
add_publish = request.POST.get("publish")
add_date = request.POST.get("pub_date")
models.Book.objects.create(title=add_title,
price=add_price,
publish=add_publish,
pub_date=add_date)
return redirect(reverse("bobooklist")) def update_book(request, nid):
if request.method == "GET":
book = models.Book.objects.filter(nid=nid).first()
# print(book.title)
return render(request, "update_book.html", {"book": book})
else:
data = request.POST.dict()
del data['csrfmiddlewaretoken']
print(data)
models.Book.objects.filter(nid=nid).update(**data)
return redirect(reverse("booklist"))
def boupdate_book(request, nid):
if request.method == "GET":
book = models.Book.objects.filter(nid=nid).first()
# print(book.title)
return render(request, "boupdate.html", {"book": book})
else:
data = request.POST.dict()
del data['csrfmiddlewaretoken']
print(data)
models.Book.objects.filter(nid=nid).update(**data)
return redirect(reverse("bobooklist"))
def del_book(request,sid):
models.Book.objects.filter(nid=sid).delete()
return redirect(reverse("bobooklist"))
主要是单表练习.运用了跳转,. 逆向解析. 从html 获取内容,,,,将内容放到html,,从数据库获取内容,,将内容放到数据库,,
C模板层:
book_list
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<h1> 三味书屋</h1>
<button><a href="{% url "addbook" %}">新增书籍</a></button>
<table border="">
<tr>
<th>序号</th>
<th>书名</th>
<th>价格</th>
<th>出版商</th>
<th>日期</th>
<th>操作</th> </tr>
{% for book in booklist %}
<tr>
<td>{{forloop.counter}}</td>
<td>{{book.title}}</td>
<td>{{book.price}}</td>
<td>{{book.publish}}</td>
<td>{{ book.pub_date |date:"Y-m-d"}}</td>
<td><button value="" name=""><a href="{% url "updatebook" nid=book.nid %}">编辑</a></button>
<button value="" name=""><a href="{% url "delbook" book.nid %}">删除</a></button>
</td> </tr>
{% endfor %}
</table> <div>
<form action="{% url "booklist" %}" method="post">
{% csrf_token %}
<div> <span>书名 </span> <input type="text" name="title1" placeholder="请输入书名"><br>
<span>价格 </span> <input type="text" name="price1" placeholder="请输入价格"><br>
<span>出版商</span> <input type="text" name="publish1" placeholder="请输入出版商"><br>
<span>日期 </span> <input type="date" name="date1" placeholder="请输入日期"><br>
</div>
<div>
<button type="submit"><a href="">新增提交</a></button>
</div>
</form>
</div> </body>
</html>
add_book
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>add_book</title>
</head>
<body>
<div>
<form action="" method="post">
{%csrf_token %}
<div> <span>书名 </span> <input type="text" name="title" placeholder="请输入书名"><br>
<span>价格 </span> <input type="text" name="price" placeholder="请输入价格"><br>
<span>出版商</span> <input type="text" name="publish" placeholder="请输入出版商"><br>
<span>日期 </span> <input type="date" name="pub_date" placeholder="请输入日期"><br>
</div>
<div>
<button type="submit" name=""><a href="">新增提交</a></button>
</div>
</form>
</div> </body>
</html>
update_book
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<form action="" method="post">
{% csrf_token %}
<p>书名 <input type="text" placeholder="{{book.title}}" value="" name="title"></p>
<p>价格 <input type="text" placeholder="{{book.price}}" value="" name="price"></p>
<p>出版商 <input type="text" placeholder="{{book.publish}}" value="" name="publish"></p>
<p>日期 <input type="date" placeholder="{{book.pub_date}}" value="" name="pub_date"></p>
<button type="submit">修改</button>
</form> </body>
</html>
D: 参数文件配置
setting
"""
Django settings for dy47 project. Generated by 'django-admin startproject' using Django 2.1.. For more information on this file, see
https://docs.djangoproject.com/en/2.1/topics/settings/ For the full list of settings and their values, see
https://docs.djangoproject.com/en/2.1/ref/settings/
""" import os # Build paths inside the project like this: os.path.join(BASE_DIR, ...)
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) # Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/2.1/howto/deployment/checklist/ # SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = '($z1^$1*b9gi7ydy(=tr7n85v^v7&ks5_pb_kxj)6u3ef12qi@' # SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True ALLOWED_HOSTS = [] # Application definition INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'app01.apps.App01Config',
] MIDDLEWARE = [
'django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
] ROOT_URLCONF = 'dy47.urls' TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [os.path.join(BASE_DIR, 'templates')]
,
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
'django.template.context_processors.debug',
'django.template.context_processors.request',
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages',
],
},
},
] WSGI_APPLICATION = 'dy47.wsgi.application' # Database
# https://docs.djangoproject.com/en/2.1/ref/settings/#databases DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
"NAME": "book2",
"HOST": "127.0.0.1",
"PROT": ,
"USER": "root",
"PASSWORD": ""
}
} # Password validation
# https://docs.djangoproject.com/en/2.1/ref/settings/#auth-password-validators AUTH_PASSWORD_VALIDATORS = [
{
'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator',
},
{
'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator',
},
{
'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator',
},
{
'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator',
},
] # Internationalization
# https://docs.djangoproject.com/en/2.1/topics/i18n/ LANGUAGE_CODE = 'en-us' TIME_ZONE = 'UTC' USE_I18N = True USE_L10N = True USE_TZ = True # Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/2.1/howto/static-files/ STATIC_URL = '/static/'
STATICFILES_DIRS = [
os.path.join(BASE_DIR, "statics")
]
day 47 Django 4的简单应用 创建简单的图书管理 (单表的增删改查)的更多相关文章
- python全栈开发day61-django简单的出版社网站展示,添加,删除,编辑(单表的增删改查)
day61 django内容回顾: 1. 下载: pip install django==1.11.14 pip install -i 源 django==1.11.14 pycharm 2. 创建项 ...
- Django学习笔记(10)——Book单表的增删改查页面
一,项目题目:Book单表的增删改查页面 该项目主要练习使用Django开发一个Book单表的增删改查页面,通过这个项目巩固自己这段时间学习Django知识. 二,项目需求: 开发一个简单的Book增 ...
- django模型层 关于单表的增删改查
关于ORM MTV或者MVC框架中包括一个重要的部分,就是ORM,它实现了数据模型与数据库的解耦,即数据模型的设计不需要依赖于特定的数据库, 通过简单的配置就可以轻松更换数据库,这极大的减轻了开发人员 ...
- Django中对单表的增删改查
之前的简单预习,重点在后面 方式一: # create方法的返回值book_obj就是插入book表中的python葵花宝典这本书籍纪录对象 book_obj=Book.objects.creat ...
- django 利用ORM对单表进行增删改查
牛小妹上周末,一直在尝试如何把数据库的数据弄到界面上.毕竟是新手,搞不出来,文档也看不懂.不过没关系,才刚上大学.今晚我们就来解释下,要把数据搞到界面的第一步.先把数据放到库里,然后再把数据从库里拿出 ...
- Django学习笔记--数据库中的单表操作----增删改查
1.Django数据库中的增删改查 1.添加表和字段 # 创建的表的名字为app的名称拼接类名 class User(models.Model): # id字段 自增 是主键 id = models. ...
- $Django 模板层(模板导入,继承)、 单表*详(增删改查,基于双下划线的查询)、static之静态文件配置
0在python脚本中使用django环境 import osif __name__ == '__main__': os.environ.setdefault("DJANGO_SETT ...
- Django之单表的增删改查
books/urls.py """books URL Configuration The `urlpatterns` list routes URLs to vi ...
- Django --- 单表的增删改查
随机推荐
- 《Delphi XE6 android 编程入门教程》推荐
近5.6年已经没有看见关于delphi的新技术的书出来了(看来在国内delphi的使用量确实很低了), 高勇同学最近出了一本<Delphi XE6 android 编程入门教程>,上周刚拿 ...
- 2081.09.22 Kuma(非旋treap)
描述 有N张卡片,编号从0到n-1, 刚开始从0到n-1按顺序排好. 现有一个操作, 对于p. l,表示从第p张卡片之后的l张卡片拿到 最前面. 例如n=7的时候, 刚开始卡片序列为0 1 2 3 4 ...
- java.lang.NoClassDefFoundError Could not initialize class 异常的处理
class,forname的配置文件出问题核对url数据库中的名字和bean中名字不同没有把jar包变成build path
- org.eclipse.ui.PartInitException: Unable to open editor, unknown editor ID: org.xmen.ui.text.XMLTextEditor
无法打开struts模式的编译xml的编译器,然后打开.project文件,编辑最后一行,找到<natures>结点,增加一条<nature>com.genuitec.ecli ...
- Matlab 中以分数显示结果
转http://www.blogbus.com/shijuanfeng-logs/234881647.html Matlab,计算得到的结果一般是小数形式. 但为了更精确表示,我们有时候需要用到分数形 ...
- mac windows蓝牙问题
如果是win7.win8或win10三者的64位版本,可以下载驱动解决:http://file2.mydrivers.com/2014/notebook/apple_broadcom_bluetoot ...
- jquery使用ajax报错[Uncaught SyntaxError: Unexpected token :]
$.post('/ajax/validate.do',{"id": id},function(ret){ //ret }); 返回值明明是json,格式也是正确的,却解析不成功,在 ...
- Java内存模型解惑--观深入理解Java内存模型系列文章有感(二)
1.volatile关键字修饰的域的特性 当我们声明共享变量为volatile后,对这个变量的读/写将会很特别.理解volatile特性的一个好方法是:把对volatile变量的单个读/写,看成是使用 ...
- Delphi 动态与静态调用DLL(最好的资料)
摘要:本文阐述了 Windows 环境下动态链接库的概念和特点,对静态调用和动态调用两种调用方式作出了比较,并给出了 Delphi 中应用动态链接库的实例. 一.动态链接库的概念 动态链接库( ...
- 初识构建工具-gradle
构建工具的作用 依赖管理 测试,打包,发布 主流的构建工具 Ant:提供编译,测试,打包 Maven:在Ant的基础上提供了依赖管理和发布的功能 Gradle:在Maven的基础上使用Groovy管理 ...