python-day70--django-Mysql-单表增删改查
项目名:bookmanage
app01文件夹 内的 __init__.py
import pymysql pymysql.install_as_MySQLdb()
app01文件夹 内的models.py
from django.db import models # Create your models here.
class Info(models.Model):
id=models.AutoField(primary_key=True)
name=models.CharField(max_length=32)
author=models.CharField(max_length=32)
pubDate=models.DateField()
price=models.DecimalField(max_digits=5, decimal_places=2)
app01文件夹 内的views.py
from django.shortcuts import render,redirect
from app01 import models
# Create your views here.
def chaxun(request):
userDict=models.Info.objects.all()
return render(request,'chaxun.html',{'userDict':userDict}) def shanchu(request,idd):
models.Info.objects.filter(id=idd).delete()
return redirect('/chaxun/') def tianjia(request):
if request.method=='POST':
name=request.POST.get('name')
author=request.POST.get('author')
date=request.POST.get('date')
price=request.POST.get('price') models.Info.objects.create(name=name,author=author,pubDate=date,price=price)
return redirect('/chaxun/') return render(request,'tianjia.html') def bianji(request):
if request.method=='POST':
idi=request.POST.get('id')
name=request.POST.get('name')
author=request.POST.get('author')
date=request.POST.get('date')
price=request.POST.get('price') models.Info.objects.filter(id=idi).update(name=name,author=author,pubDate=date,price=price)
return redirect('/chaxun/') idi=request.GET.get('id')
user_info=models.Info.objects.filter(id=idi)[0]
id=user_info.id
name=user_info.name
author=user_info.author
date=user_info.pubDate
price=user_info.price
return render(request,'bianji.html',{'id':id,'name':name,'author':author,'date':date,'price':price})
bookmanage文件夹 内的 settings.py
"""
Django settings for bookmanage project. Generated by 'django-admin startproject' using Django 1.11.6. For more information on this file, see
https://docs.djangoproject.com/en/1.11/topics/settings/ For the full list of settings and their values, see
https://docs.djangoproject.com/en/1.11/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/1.11/howto/deployment/checklist/ # SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = 'bh#p^+)-^@yc*&v7+_$k2%-#yms-^omlm*ai*cem-ahj!&4o)7' # 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',
] 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 = 'bookmanage.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 = 'bookmanage.wsgi.application' # Database
# https://docs.djangoproject.com/en/1.11/ref/settings/#databases DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'bookinfo', # 你的数据库名称
'USER': 'root', #你的数据库用户名
'PASSWORD': '963.', #你的数据库密码
'HOST': '', #你的数据库主机,留空默认为localhost
'PORT': '', #你的数据库端口
}
} # Password validation
# https://docs.djangoproject.com/en/1.11/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/1.11/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/1.11/howto/static-files/ STATIC_URL = '/static/'
STATICFILES_DIRS = [
os.path.join(BASE_DIR, 'static'),
] TEMPLATE_DIRS = (
os.path.join(BASE_DIR, 'templates'),
'app01',
) LOGGING = {
'version': 1,
'disable_existing_loggers': False,
'handlers': {
'console': {
'level': 'DEBUG',
'class': 'logging.StreamHandler',
},
},
'loggers': {
'django.db.backends': {
'handlers': ['console'],
'propagate': True,
'level': 'DEBUG',
},
}
}
bookmanage文件夹 内的 urls.py
"""biaoge URL Configuration The `urlpatterns` list routes URLs to views. For more information please see:
https://docs.djangoproject.com/en/1.11/topics/http/urls/
Examples:
Function views
1. Add an import: from my_app import views
2. Add a URL to urlpatterns: url(r'^$', views.home, name='home')
Class-based views
1. Add an import: from other_app.views import Home
2. Add a URL to urlpatterns: url(r'^$', Home.as_view(), name='home')
Including another URLconf
1. Import the include() function: from django.conf.urls import url, include
2. Add a URL to urlpatterns: url(r'^blog/', include('blog.urls'))
"""
from django.conf.urls import url
from django.contrib import admin
from app01 import views
urlpatterns = [
url(r'^chaxun/', views.chaxun),
url(r'^shanchu/(\d+)', views.shanchu),
url(r'^bianji/', views.bianji),
url(r'^tianjia/', views.tianjia),
]
static 文件夹内放 bootstrap及 jQuery 文件
templates文件夹内的 模板
<!DOCTYPE html>
<html>
<head lang="en">
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title></title>
<link rel="stylesheet" href="/static/bootstrap-3.3.7/css/bootstrap.min.css"/>
<script src="/static/jquery-3.2.1.js"></script>
<script src="/static/bootstrap-3.3.7/js/bootstrap.min.js"></script>
<style>
.ccc {
margin-top: 70px;
} .clc {
height: 30px;
margin-left: -15px;
text-align: center;
line-height: 30px;
border-radius: 5px;
} .menu {
margin-top: 7px; } .panel {
height: 580px;
} .c2 {
position: fixed;
left: 1290px;
top: 580px;
} .ccc .row .sidebar {
padding-top: 20px;
height: 600px;
background-color: #f5f5f5;
margin-top: -20px; }
</style>
</head>
<body>
<nav class="navbar navbar-inverse navbar-fixed-top">
<div class="container-fluid">
<div class="navbar-header">
<button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#navbar"
aria-expanded="false" aria-controls="navbar">
<span class="sr-only">Toggle navigation</span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
<a class="navbar-brand" href="">图书管理系统</a>
</div>
<div id="navbar" class="navbar-collapse collapse">
<ul class="nav navbar-nav navbar-right">
<li><a href="">设置</a></li>
<li><a href="">帮助</a></li>
<li><a href="">退出</a></li>
</ul>
<form class="navbar-form navbar-right">
<input type="text" class="form-control" placeholder="搜索...">
</form>
</div>
</div>
</nav>
<div class="container-fluid ccc">
<div class="row">
<div class="col-sm-3 col-md-2 sidebar">
<div class="menu">
<div class="head bg-primary clc" id="clc1">菜单一</div>
<ul class="nav nav-sidebar hide" id="clc4">
<li class=""><a href="">Overview <span
class="sr-only">(current)</span></a>
</li>
<li><a href="">Reports</a></li>
<li><a href="">Analytics</a></li>
<li><a href="">Export</a></li>
</ul>
</div>
<div class="menu">
<div class="head bg-primary clc" id="clc2">菜单二</div>
<ul class="nav nav-sidebar hide" id="clc5">
<li><a href="">Nav item</a></li>
<li><a href="">Nav item again</a></li>
<li><a href="">One more nav</a></li>
<li><a href="">Another nav item</a></li>
<li><a href="">More navigation</a></li>
</ul>
</div>
<div class="menu">
<div class="head bg-primary clc" id="clc3">菜单三</div>
<ul class="nav nav-sidebar hide" id="clc6">
<li><a href="">Nav item again</a></li>
<li><a href="">One more nav</a></li>
<li><a href="">Another nav item</a></li>
</ul>
</div>
</div> <div class="col-sm-9 col-md-10 main">
<div class="panel panel-primary">
<div class="panel-heading">书籍信息</div>
<div class="panel-body">
<!-- Button trigger modal -->
<a href="/tianjia/"><button type="button" class="btn btn-primary" data-toggle="modal" data-target="#myModal">
添加
</button></a> <table class="table table-hover">
{# <tr>#}
<thead>
<th>序号</th>
<th>书名</th>
<th>作者</th>
<th>出版日期</th>
<th>价钱</th>
</thead>
{# </tr>#}
{% block tbody %} {% endblock tbody %}
<div class="row c2">
<nav aria-label="Page navigation " class="pull-right c5">
<ul class="pagination">
<li>
<a href="#" aria-label="Previous">
<span aria-hidden="true">«</span>
</a>
</li>
<li><a href="#">1</a></li>
<li><a href="#">2</a></li>
<li><a href="#">3</a></li>
<li><a href="#">4</a></li>
<li><a href="#">5</a></li>
<li>
<a href="#" aria-label="Next">
<span aria-hidden="true">»</span>
</a>
</li>
</ul>
</nav>
</div>
</table>
</div>
</div>
</div>
</div>
<div class="row">
<div class="col-md-6 col-md-offset-3" style="margin-top: -40px">
<hr/>
<div style="text-align: center">
<a href="">关于我们 |</a>
<a href="">联系我们 |</a>
<a href="">意见与反馈 |</a>
<a href="">友情链接 |</a>
<a href="">公告</a> <div>
<div style="font-size: 13px">
版权所有:北京Cool科技有限公司
</div>
</div>
</div> </div>
</div>
</div>
<script> $('.clc').on('click', function () {
$(this).parent().siblings().children('ul').addClass('hide');
$(this).next().toggleClass('hide');
}) </script>
</body>
</html>
base.html
<!DOCTYPE html>
<html>
<head lang="en">
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title></title>
<link rel="stylesheet" href="/static/bootstrap-3.3.7/css/bootstrap.min.css"/>
<script src="/static/jquery-3.2.1.js"></script>
<script src="/static/bootstrap-3.3.7/js/bootstrap.min.js"></script>
</head>
<body>
<div class="container">
<div class="row col-md-4 col-md-offset-4" style="margin-top: 100px">
{% block form %}
{% endblock form %}
</div>
</div> </body>
</html>
base2.html
{% extends 'base2.html' %}
{% block form %}
<form action="/bianji/" method="post">
{% csrf_token %}
<input type="hidden" name="id" value="{{ id }}"/>
<div class="form-group">
<label for="usernaem">书名</label>
<input type="text" class="form-control item" id="usernaem" name="name" value="{{ name }}">
</div> <div class="form-group">
<label for="age">作者</label>
<input type="text" class="form-control item" id="age" name="author" value="{{ author }}">
</div>
<div class="form-group">
<label for="gender">出版时间</label>
<input type="date" class="form-control item" id="gender" name="date" value="{{ date|date:'Y-m-d' }}">
</div>
<div class="form-group">
<label for="gender1">价格</label>
<input type="text" class="form-control item" id="gender1" name="price" value="{{ price }}">
</div>
<div class="pull-right"><a href="/chaxun/"><button type="button" class="btn btn-default" data-dismiss="modal">取消</button></a>
<input type="submit" class="btn btn-primary"/> </div>
</form>
{% endblock form %}
bianji.html
{% extends "base.html" %}
{% block tbody %}
<tbody>
{% for user_info in userDict %}
<tr>
<td>{{ forloop.counter }}</td>
<td>{{ user_info.name }}</td>
<td>{{ user_info.author }}</td>
<td>{{ user_info.pubDate|date:'Y-m-d' }}</td>
<td>{{ user_info.price }}</td>
<td>
<div class="pull-left">
<a href="/shanchu/{{ user_info.id }}">
<button class="btn btn-danger" >删除</button>
</a>
</div>
<a href="/bianji/?id={{ user_info.id }}">
<button class="btn btn-success" style="margin-left: 5px">编辑</button>
</a>
</td>
</tr>
</tbody>
{% endfor %}
{% endblock tbody %}
chaxun.html
{% extends 'base2.html' %}
{% block form %}
<form action="/tianjia/" method="post">
{% csrf_token %}
<div class="form-group">
<label for="usernaem">书名</label>
<input type="text" class="form-control item" id="usernaem" name="name" placeholder="name">
</div> <div class="form-group">
<label for="age">作者</label>
<input type="text" class="form-control item" id="age" name="author" placeholder="author">
</div>
<div class="form-group">
<label for="gender">出版时间</label>
<input type="date" class="form-control item" id="gender" name="date">
</div>
<div class="form-group">
<label for="gender1">价格</label>
<input type="text" class="form-control item" id="gender1" name="price" placeholder="price">
</div>
<div class="pull-right"><a href="/chaxun/"><button type="button" class="btn btn-default" data-dismiss="modal">取消</button></a>
<input type="submit" class="btn btn-primary"/>
</div>
</form>
{% endblock form %}
tianjia.html
python-day70--django-Mysql-单表增删改查的更多相关文章
- Django框架(八)--单表增删改查,在Python脚本中调用Django环境
一.数据库连接配置 如果连接的是pycharm默认的Sqlite,不用改动,使用默认配置即可 如果连接mysql,需要在配置文件中的setting中进行配置: 将DATABASES={} 更新为 DA ...
- Django框架(九)—— 单表增删改查,在Python脚本中调用Django环境
目录 单表增删改查,在Python脚本中调用Django环境 一.数据库连接配置 二.orm创建表和字段 三.单表增删改查 1.增加数据 2.删除数据 3.修改数据 4.查询数据 四.在Python脚 ...
- $Django orm增删改字段、建表 ,单表增删改查,Django请求生命周期
1 orm介绍 ORM是什么 ORM 是 python编程语言后端web框架 Django的核心思想,“Object Relational Mapping”,即对象-关系映射,简称ORM. 一 ...
- Django ORM基本的单表增删改查
创建表 步骤: 1.app下models.py里创建类(继承models.Model) from django.db import models class UserInfo(models.Model ...
- mysql数据库单表增删改查命令
数据库DB-database-mysql 课程安排 第一天: 1.数据库定义以及设计 2.mysql服务端的安装 3.mysql-dos操作 库的操作 表的操作 4.mysql客户端navicate工 ...
- MySQL初步理解,简易单表增删改查
什么是数据库? 存储数据的仓库,本质是一个文件系统,封装了算法和文件之前数据的存储模式 阶段1:集合 数组 变量 缺点:数据存储在内存中,不能实现数据的持久化存储 阶段2:IO流 结合文件 .txt ...
- Python学习---django之ORM的增删改查180125
模型常用的字段类型参数 <1> CharField #字符串字段, 用于较短的字符串. #CharField 要求必须有一个参数 maxlength, 用于从数 ...
- Django之数据表增删改查
Django数据增删改查: 上课代码 from django.shortcuts import render,HttpResponse # Create your views here. from a ...
- ORM之单表增删改查
ORM之单表增删改查 在函数前,先导入要操作的数据库表模块,model from model所在的路径文件夹 import model 在views文件中,加的路径: #就一个app01功能的文件 ...
- django(七)之数据库表的单表-增删改查QuerySet,双下划线
https://www.cnblogs.com/haiyan123/p/7738435.html https://www.cnblogs.com/yuanchenqi/articles/6083427 ...
随机推荐
- python之路----面向对象的继承特性
继承 什么是继承 继承是一种创建新类的方式,在python中,新建的类可以继承一个或多个父类,父类又可称为基类或超类,新建的类称为派生类或子类 python中类的继承分为:单继承和多继承 class ...
- SACD ISO镜像中提取DSDIFF(DFF)、DSF文件
听语音 | 浏览:5620 | 更新:2015-08-25 11:46 | 标签:硬件 1 2 3 4 5 分步阅读 现在有一种比较流行的无损音乐传输介质是SACD ...
- Linux系统下(x64)安装jdk 1.6(jdk-6u45-linux-x64.bin)
Linux系统下(x64)安装jdk 1.6(jdk-6u45-linux-x64.bin) 一,查看是否安装jdk: # rpm -qa | grep jdk 或者 #rpm -q jdk 或者 # ...
- Docker搭建RabbitMQ集群
Docker搭建RabbitMQ集群 Docker安装 见官网 RabbitMQ镜像下载及配置 见此博文 集群搭建 首先,我们需要启动运行RabbitMQ docker run -d --hostna ...
- 20145225 《网络对抗》逆向及Bof基础实践
实践目标 本次实践的对象是一个名为pwn1的linux可执行文件. 该程序正常执行流程是:main调用foo函数,foo函数会简单回显任何用户输入的字符串. 该程序同时包含另一个代码片段,getShe ...
- 20165211 获奖感想及java课程总结
20165211 获奖感想及java课程总结 理论脱离实践是最大的不幸.--达芬奇 这句话,是我在学习Java之前,假期内写的20165211 学习基础和C语言调查里的所引用的一句话,是当时我对Jav ...
- 重写(override)与重载(overload)的区别
一.重写(override) override是重写(覆盖)了一个方法,以实现不同的功能.一般是用于子类在继承父类时,重写(重新实现)父类中的方法. 重写(覆盖)的规则: 1.重写方法的参数列表必须完 ...
- 分析linux内核中的slub内存管理算法
1. 分析的linux内核源码版本为4.18.0 2. 与slub相关的内核配置项为CONFIG_SLUB 3. 一切都从一个结构体数组kmalloc_caches开始,它的原型如下: ] __ro_ ...
- java必背面试题
JAVA必背面试题和项目面试通关要点 一 数据库 1.常问数据库查询.修改(SQL查询包含筛选查询.聚合查询和链接查询和优化问题,手写SQL语句,例如四个球队比赛,用SQL显示所有比赛组合:举例2:选 ...
- 【TCP/IP详解 卷一:协议】第二十二章 TCP的坚持定时器
这两章来到了TCP的定时器部分,在 TCP的超时与重传 和 TCP的三握四挥 我们介绍了 TCP的重传定时器 和 TCP的2MSL定时器. 本随笔介绍 防止返回ACK丢失的死锁情况 的 坚持定时器 和 ...