django与mysql实现简单的增删查改
模型定义
from django.db import models
class Grades(models.Model):
g_name = models.CharField(max_length=20)
create_date = models.DateTimeField()
girl_num = models.IntegerField()
boy_num = models.IntegerField()
isDelete = models.BooleanField(default=False)
def __str__(self):
return self.g_name
class Meta:
db_table = 'grades'
class Students(models.Model):
s_name = models.CharField(max_length=20)
s_gender = models.BooleanField(default=True)
s_age = models.IntegerField()
conetend = models.CharField(max_length=20)
isDlete = models.BooleanField(default=False)
# 关联外键
s_grade = models.ForeignKey("Grades")
def __str__(self):
return self.s_name
class Meta:
db_table = 'students'
url
from django.conf.urls import url
from user import views
urlpatterns = [
url(r'^show_info/',views.show_info,name='show_info'),
url(r'^add_stu/',views.add_stu,name='add_stu'),
url(r'^del_stu/(\d+)/',views.del_stu,name='del_stu'),
url(r'^mod_stu/(\d+)/',views.mod_stu,name='mod_stu'),
url(r'^sel_stu/',views.sel_stu,name='sel_stu'),
]
视图函数定义
from django.shortcuts import render
from django.http import HttpResponseRedirect
from django.urls import reverse
from user.models import Students,Grades
def show_info(request):
if request.method == 'GET':
stus = Students.objects.all()
return render(request,'info.html',{'stus':stus})
def add_stu(request):
if request.method == 'GET':
grades = Grades.objects.all()
return render(request,'add.html',{'grades':grades})
if request.method == 'POST':
name = request.POST.get('name')
age = request.POST.get('age')
gender = request.POST.get('sex')
content = request.POST.get('msg')
grade = request.POST.getlist('check')[0]
sgrade = Grades.objects.get(g_name=grade)
Students.objects.create(s_name=name,s_age=age,s_gender=gender,contend=content,s_grade=sgrade)
return HttpResponseRedirect(reverse('user:show_info'))
def del_stu(request,id):
if request.method == 'GET':
Students.objects.get(pk=id).delete()
return HttpResponseRedirect(reverse('user:show_info'))
def mod_stu(request,id):
if request.method == 'GET':
stu = Students.objects.get(pk=id)
grades = Grades.objects.all()
return render(request,'mod.html',{'stu':stu,'grades':grades})
if request.method == 'POST':
name = request.POST.get('name')
age = request.POST.get('age')
gender = request.POST.get('gender')
grade = request.POST.getlist('check')[0]
u_grade = Grades.objects.get(g_name=grade)
Students.objects.filter(pk=id).update(s_name=name,s_age=age,s_gender=gender,s_grade=u_grade)
return HttpResponseRedirect(reverse('user:show_info'))
def sel_stu(request):
if request.method == 'GET':
return render(request,'sel.html')
if request.method == 'POST':
name = request.POST.get('name')
try:
stu = Students.objects.get(s_name=name)
return render(request, 'sel.html', {'stu': stu})
except:
return render(request,'sel.html',{'error':'该学生不存在'})
页面html定义
添加页
{% extends 'base.html' %}
{% block content %}
<form action="" method="post">
{% csrf_token %}
姓名:<input type="text" name="name">
年龄:<input type="text" name="age">
性别:<input type="text" name="sex">
描述:<input type="text" name="msg">
{% for grade in grades %}
<input type="radio" name="check" value="{{grade.g_name}}">{{grade.g_name}}
{% endfor %}
<input type="submit" value="提交">
</form>
{% endblock %}
修改页
{% extends 'base.html' %}
{% block content %}
<form action="" method="post">
{% csrf_token %}
姓名: <input type="text" name="name" value="{{stu.s_name}}">
年龄: <input type="text" name="age" value="{{stu.s_age}}">
性别: <input type="text" name="gender" value="{{stu.s_gender}}">
{% for grade in grades %}
<input type='radio' name="check" value="{{grade.g_name}}">{{grade.g_name}}
{% endfor %}
<input type="submit" value="修改">
</form>
{% endblock %}
查询页
{% extends 'base.html' %}
{% block content %}
<form action="" method="post">
{% csrf_token %}
请输入学生姓名:<input type="text" name="name">
<input type="submit" value="提交">
<table border="1">
<br>
{% if stu %}
<thead>
<th align="center" style="color:red">序号</th>
<th align="center" style="color:red">姓名</th>
<th align="center" style="color:red">年龄</th>
<th align="center" style="color:red">性别</th>
<th align="center" style="color:red">简介</th>
<th align="center" style="color:red">班级</th>
</thead>
<tbody>
<tr>
<td align="center">{{stu.id}}</td>
<td align="center">{{stu.s_name}}</td>
<td align="center">{{stu.s_age}}</td>
<td align="center">{{stu.s_gender}}</td>
<td align="center">{{stu.contend}}</td>
<td align="center">{{stu.s_grade.g_name}}</td>
</tr>
</tbody>
</table>
{% endif %}
</form>
<h1 style="color:red">{{error}}</h1>
{% endblock %}
信息页
{% extends 'base.html' %}
{% block content %}
<table border="1">
<thead>
<th align="center" style="color:red">序号</th>
<th align="center" style="color:red">姓名</th>
<th align="center" style="color:red">年龄</th>
<th align="center" style="color:red">性别</th>
<th align="center" style="color:red">班级</th>
<th align="center" style="color:red">班级成立时间</th>
<th align="center" style="color:red">删除学生</th>
<th align="center" style="color:red">修改学生</th>
</thead>
<tbody>
{% for stu in stus %}
<tr>
<td align="center">{{forloop.counter}}</td>
<td align="center">{{stu.s_name}}</td>
<td align="center">{{stu.s_age}}</td>
<td align="center">{{stu.s_gender}}</td>
<td align="center">{{stu.s_grade.g_name}}</td>
<td align="center">{{stu.s_grade.create_date}}</td>
<td align="center"><a href="/user/del_stu/{{stu.id}}/">删除</a></td>
<td align="center"><a href="/user/mod_stu/{{stu.id}}/">修改</a></td>
</tr>
{% endfor %}
</tbody>
</table>
<a href="/user/add_stu/{{stu.id}}/">增加学生</a>
<a href="/user/sel_stu/{{stu.id}}/">查询学生</a>
{% endblock %}
django与mysql实现简单的增删查改的更多相关文章
- nodejs连接mysql并进行简单的增删查改
最近在入门nodejs,正好学习到了如何使用nodejs进行数据库的连接,觉得比较重要,便写一下随笔,简单地记录一下 使用在安装好node之后,我们可以使用npm命令,在项目的根目录,安装nodejs ...
- Java连接MySQL数据库及简单的增删查改操作
主要摘自 https://www.cnblogs.com/town123/p/8336244.html https://www.runoob.com/java/java-mysql-connect.h ...
- mybatis实现简单的增删查改
接触一个新技术,首先去了解它的一些基本概念,这项技术用在什么方面的.这样学习起来,方向性也会更强一些.我对于mybatis的理解是,它是一个封装了JDBC的java框架.所能实现的功能是对数据库进行增 ...
- EF简单的增删查改
Add /// <summary> /// /// </summary> public void Add() { TestDBEntities2 testdb = new Te ...
- Django实现数据库中表格的增删查改
1.urls.py """Django_demo1 URL Configuration The `urlpatterns` list routes URLs to vie ...
- 一般处理程序+htm C#l简单的增删查改
首先引用两个文件一个dll: 数据库表已创建 首先编写数据读取部分 /// <summary> /// 查询 /// </summary> /// <param name ...
- RavenDb学习(二)简单的增删查改
在上一节当中已经介绍了RavenDb的文档设计模式,这一节我们要具体讲一讲如何使用api去访问RavenDb .连接RavenDb var documentStore = new DocumentSt ...
- Hibernate 的事物简单的增删查改
Hibernate 是一个优秀的ORM框架体现在: 1. 面向对象设计的软件内部运行过程可以理解成就是在不断创建各种新对象.建立对象之间的关系,调用对象的方法来改变各个对象的状态和对象消亡的过程,不管 ...
- asp.net MVC最简单的增删查改!(详)
折腾了两天搞出来,但原理性的东西还不是很懂,废话不多说上图上代码 然后右键models,新建一个数据模型 注意我添加命名为lianxi 添加后如上 接下来在controllers添加控制器还有在Vie ...
随机推荐
- Using KafkaBolt to write to a kafka topic
https://community.hortonworks.com/questions/27187/using-kafkabolt-to-write-to-a-kafka-topic.html --- ...
- 用hadoop实现SimRank++算法(1)----权值转移矩阵的计算
本文主要针对广告检索领域的查询重写应用,依据查询-广告点击二部图,在MapReduce框架上实现SimRank++算法.关于SimRank++算法的背景和原理请參看前一篇文章<基于MapRedu ...
- KVO---视图间数据的传递:标签显示输入的内容【多个视图中】
RootViewController.m #import "ModalViewController.h" @interface RootViewController () @end ...
- oracle 数据库中数据导出到excel
确保安装了PLSQL Developer工具.连接数据库. FIle--new--SQL window 运行查询,选中要导出的数据,右键--copy to excel. 或者 运行查询后.右键--se ...
- SAP WEBSERVICE Soap中RPC-style和Document-style
RPC是以方法调用的方式描写叙述WebSerivce的,也就是说,你要说清楚调用的那个方法,以及各个參数的名称和值.要描写叙述这些东东.SOAP消息就要有一个统一的规范,指出那一部分是方法名.哪个部分 ...
- 《Java程序设计》第16周周五:数据库连接 与 随机数的使用
第一部分:实验项目 项目二:数据库初步. 目的:了解Java连接数据库的步骤与方法.以及MySQL数据库的安装与使用. 目标: (1)在机房安装上MySQL数据库. 安装成功 MySQL数据库 (2) ...
- 各种编程语言功能综合简要介绍(C,C++,JAVA,PHP,PYTHON,易语言)
各种编程语言功能综合简要介绍(C,C++,JAVA,PHP,PYTHON,易语言) 总结 a.一个语言或者一个东西能火是和这种语言进入某一子行业的契机有关.也就是说这个语言有没有解决社会急需的问题. ...
- python基本数据类型之元祖tuple
元祖tuple 是对列表的二次加工,书写格式为括号(),里面放元素 元组的一级元素不可被修改,且不能被增加和删除 一般写元组的时候,推荐在最后加入逗号, 能加则加 创建元组 ? 1 tu = (11 ...
- Hadoop MapReduce编程 API入门系列之wordcount版本2(六)
这篇博客,给大家,体会不一样的版本编程. 代码 package zhouls.bigdata.myMapReduce.wordcount4; import java.io.IOException; i ...
- android黑科技系列——微信抢红包插件原理解析和开发实现
一.前言 自从几年前微信添加抢红包的功能,微信的电商之旅算是正式开始正式火爆起来.但是作为Android开发者来说,我们在抢红包的同时意识到了很多问题,就是手动去抢红包的速度慢了,当然这些有很多原因导 ...