前情提要   

  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>书名&nbsp;&nbsp;&nbsp;</span> <input type="text" name="title1" placeholder="请输入书名"><br>
<span>价格&nbsp;&nbsp;&nbsp;</span> <input type="text" name="price1" placeholder="请输入价格"><br>
<span>出版商</span> <input type="text" name="publish1" placeholder="请输入出版商"><br>
<span>日期&nbsp;&nbsp;&nbsp;</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>书名&nbsp;&nbsp;&nbsp;</span> <input type="text" name="title" placeholder="请输入书名"><br>
<span>价格&nbsp;&nbsp;&nbsp;</span> <input type="text" name="price" placeholder="请输入价格"><br>
<span>出版商</span> <input type="text" name="publish" placeholder="请输入出版商"><br>
<span>日期&nbsp;&nbsp;&nbsp;</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的简单应用 创建简单的图书管理 (单表的增删改查)的更多相关文章

  1. python全栈开发day61-django简单的出版社网站展示,添加,删除,编辑(单表的增删改查)

    day61 django内容回顾: 1. 下载: pip install django==1.11.14 pip install -i 源 django==1.11.14 pycharm 2. 创建项 ...

  2. Django学习笔记(10)——Book单表的增删改查页面

    一,项目题目:Book单表的增删改查页面 该项目主要练习使用Django开发一个Book单表的增删改查页面,通过这个项目巩固自己这段时间学习Django知识. 二,项目需求: 开发一个简单的Book增 ...

  3. django模型层 关于单表的增删改查

    关于ORM MTV或者MVC框架中包括一个重要的部分,就是ORM,它实现了数据模型与数据库的解耦,即数据模型的设计不需要依赖于特定的数据库, 通过简单的配置就可以轻松更换数据库,这极大的减轻了开发人员 ...

  4. Django中对单表的增删改查

    之前的简单预习,重点在后面 方式一: # create方法的返回值book_obj就是插入book表中的python葵花宝典这本书籍纪录对象   book_obj=Book.objects.creat ...

  5. django 利用ORM对单表进行增删改查

    牛小妹上周末,一直在尝试如何把数据库的数据弄到界面上.毕竟是新手,搞不出来,文档也看不懂.不过没关系,才刚上大学.今晚我们就来解释下,要把数据搞到界面的第一步.先把数据放到库里,然后再把数据从库里拿出 ...

  6. Django学习笔记--数据库中的单表操作----增删改查

    1.Django数据库中的增删改查 1.添加表和字段 # 创建的表的名字为app的名称拼接类名 class User(models.Model): # id字段 自增 是主键 id = models. ...

  7. $Django 模板层(模板导入,继承)、 单表*详(增删改查,基于双下划线的查询)、static之静态文件配置

    0在python脚本中使用django环境 import osif __name__ == '__main__':    os.environ.setdefault("DJANGO_SETT ...

  8. Django之单表的增删改查

      books/urls.py   """books URL Configuration The `urlpatterns` list routes URLs to vi ...

  9. Django --- 单表的增删改查

随机推荐

  1. 2014.1.14 struts 的default.properties 配置文件详述

    转自  http://justsee.iteye.com/blog/723993 Struts 2框架有两个核心配置文件:struts.xml和struts.properties 其中struts.x ...

  2. 常用C字符串函数

    static void str_repalce(char *src,char *from,char *to) {     char *p,*q;     int lenFrom;     int le ...

  3. 2018.10.02 NOIP模拟 序列维护(线段树+广义欧拉定理)

    传送门 一道比较好的线段树. 考试时线性筛打错了于是弃疗. 60分暴力中有20分的快速幂乘爆了于是最后40分滚粗. 正解并不难想. 每次区间加打懒标记就行了. 区间查询要用到广义欧拉定理. 我们会发现 ...

  4. POJ 1458 Common Subsequence (DP+LCS,最长公共子序列)

    题意:给定两个字符串,让你找出它们之间最长公共子序列(LCS)的长度. 析:很明显是个DP,就是LCS,一点都没变.设两个序列分别为,A1,A2,...和B1,B2..,d(i, j)表示两个字符串L ...

  5. webservice大文件怎么传输

    版权所有 2009-2018荆门泽优软件有限公司 保留所有权利 官方网站:http://www.ncmem.com/ 产品首页:http://www.ncmem.com/webapp/up6.2/in ...

  6. MFC框架仿真<三>R T T I

    RTTI,简单的说,就是判定A类是否为B类的基类.将书本的内容最大程度的简化,如下图的类层次,现在解决的问题就是:判定“梨”是否是“红富士”的基类.

  7. MOD13A1: MODIS/Terra Vegetation Indices 16-Day L3 Global 500 m SIN Grid V006

    https://lpdaac.usgs.gov/node/838 Description The MOD13A1 Version 6 product provides a Vegetation Ind ...

  8. java基础-day2

    第02天 java基础知识 今日内容介绍 u Eclipse的安装.配置及使用 u 运算符 u 键盘录入 第1章   Eclipse开发工具 1.1  Eclipse概述和安装 Eclipse是一个I ...

  9. java线程一

    我们可以在计算机上运行各种计算机软件程序.每一个运行的程序可能包括多个独立运行的线程(Thread).线程(Thread)是一份独立运行的程序,有自己专用的运行栈.线程有可能和其他线程共享一些资源,比 ...

  10. <context:component-scan>自动扫描

    主要讲解自动扫描的<context:component-scan>中的2个子标签的使用方法 在Spring MVC中的配置中一般会遇到这两个标签,作为<context:compone ...