1.urls.py

"""Django_demo1 URL Configuration

The `urlpatterns` list routes URLs to views. For more information please see:
https://docs.djangoproject.com/en/2.1/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
from app01.views import classes
from app01.views import students
# from django.urls import re_path urlpatterns = [
path('admin/', admin.site.urls),
path('classes.html/', classes.get_classes),
path('add_classes', classes.add_classes),
path('del_classes', classes.del_classes),
path('edit_classes', classes.edit_classes),
path('students.html/', students.get_students),
path('add_students', students.add_students),
path('del_students', students.del_students),
path('edit_students', students.edit_students), ]

2.models.py

from django.db import models

class Classes(models.Model):
'''
班级表
'''
title = models.CharField(max_length=32)
m = models.ManyToManyField('Teachers') class Teachers(models.Model):
'''
老师表
'''
name = models.CharField(max_length=32) '''
cid tid
1 1
2 1
1 1
'''
# class C2T(models.Model):
# cid = models.ForeignKey()
# tid = models.ForeignKey() class Student(models.Model):
username = models.CharField(max_length=32)
age = models.IntegerField()
gender = models.BooleanField()
cs = models.ForeignKey('Classes', on_delete=models.CASCADE)

3.classes.py

from django.shortcuts import render, redirect
from app01 import models def get_classes(request):
cls_list = models.Classes.objects.all()
return render(request, 'get_classes.html', {'cls_list': cls_list}) def add_classes(request):
if request.method == 'GET':
return render(request, 'add_classes.html')
if request.method == 'POST':
title = request.POST.get('title')
models.Classes.objects.create(title=title)
return redirect('/classes.html') def del_classes(request):
nid = request.GET.get('nid')
models.Classes.objects.filter(id=nid).delete()
return redirect('classes.html/') def edit_classes(request):
if request.method == 'GET':
nid = request.GET.get('nid')
obj = models.Classes.objects.filter(id=nid).first() return render(request, 'edit_classes.html', {'obj': obj})
if request.method == 'POST':
nid = request.POST.get('nid')
title = request.POST.get('title') models.Classes.objects.filter(id=nid).update(title=title)
return redirect('/classes.html')

4.students.py

from django.shortcuts import render, redirect
from app01 import models def get_students(request):
students_list = models.Student.objects.all()
return render(request, 'get_students.html', {'students_list': students_list}) def del_students(request):
nid = request.GET.get('nid')
models.Student.objects.filter(id=nid).delete()
return redirect('/students.html') def add_students(request):
if request.method == 'GET':
cs_list = models.Classes.objects.all()
return render(request, 'add_students.html', {'cs_list': cs_list})
if request.method == 'POST':
id = request.POST.get('id')
username = request.POST.get('username')
age = request.POST.get('age')
gender = request.POST.get('gender')
cs_id = request.POST.get('cs_id')
models.Student.objects.create(id=id, username=username, age=age, gender=gender, cs_id=cs_id)
return redirect('/students.html') def edit_students(request): if request.method == 'GET':
nid = request.GET.get('nid')
# print(nid)
class_list = models.Classes.objects.all()
obj = models.Student.objects.filter(id=nid).first() return render(request, 'edit_students.html', {'obj': obj, 'class_list': class_list})
if request.method == 'POST':
id = request.POST.get('id')
username = request.POST.get('username')
age = request.POST.get('age')
gender = request.POST.get('gender')
cs_id = request.POST.get('cs_id')
models.Student.objects.filter(id=id).update(id=id, username=username, age=age, gender=gender, cs_id=cs_id)
return redirect('/students.html')

5.add_classes.html

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<form action="add_classes" method="post">
{% csrf_token %}
<input type="text" name="title" >
<input type="submit" value="提交">
</form>
</body>
</html>

6.add_students.html

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<form action="/add_students" method="post">
{% csrf_token %}
<input type="text" name="id" placeholder="id">
<input type="text" name="username" placeholder="username">
<input type="text" name="age" placeholder="age">
男<input type="radio" name="gender" value="0">
女<input type="radio" name="gender" value="1">
<select name="cs_id" >
{% for row in cs_list %}
<option value="{{ row.id }}">{{ row.title }}</option>
{% endfor %}
</select>
{# <input type="text" name="cs_id" placeholder="cs_id">#}
<input type="submit" value="提交">
</form>
</body>
</html>

7.edit_classes.html

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<form action="edit_classes" method="post">
{% csrf_token %}
<input type="text" name="nid" value="{{ obj.id }}" style="display:none;" >
<input type="text" name="title" value="{{ obj.title }}">
<input type="submit" value="提交">
</form>
</body>
</html>

8.edit_classes.html

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<form action="/edit_students" method="post">
{% csrf_token %}
<input type="text" name="id" value="{{ obj.id }}">
<input type="text" name="username" value="{{ obj.username }}">
<input type="text" name="age" value="{{ obj.age }}"> 男<input type="radio" name="gender" value="0">
女<input type="radio" name="gender" value="1">
<select name="cs_id">
{% for row in class_list %}
<option value="{{ row.id }}">{{ row.title }}</option>
{% endfor %}
</select> <input type="submit" value="提交"> </form>
</body>
</html>

9.get_classes.html

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<div>
<a href="/add_classes">添加</a>
</div>
<div>
<table border="1">
<thead>
<tr>
<th>ID</th>
<th>名称</th>
<th>操作</th>
</tr>
</thead>
<tbody>
{% for row in cls_list %}
<tr>
<td>
{{ row.id }}
</td>
<td>
{{ row.title }}
</td>
<td>
{# <form action="/del_classes?{{ row.id }}" method="get">#}
{# <input type="submit" value="删除">#}
{# </form>#}
<a href="/del_classes?nid={{ row.id }}" >删除</a>
|
<a href="/edit_classes?nid={{ row.id }}">编辑</a>
</td>
</tr>
{% endfor %}
</tbody>
</table>
</div> </body>
</html>

10.get_students.html

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<div>
<a href="/add_students">添加</a>
</div>
<div>
<table border="1">
<tr>
<th>id</th>
<th>姓名</th>
<th>年龄</th>
<th>性别</th>
<th>班级</th>
<th>操作</th>
</tr>
<tbody>
{% for row in students_list %}
<tr>
<td>
{{ row.id }}
</td>
<td>
{{ row.username }}
</td>
<td>
{{ row.age }}
</td>
<td>
{% if row.gender == 0 %}

{% endif %}
{% if row.gender == 1 %}

{% endif %}
</td>
<td>
{{ row.cs.title }}
</td>
<td>
<a href="/del_students?nid={{ row.id }}">删除</a>
|
<a href="/edit_students?nid={{ row.id }}">编辑</a>
</td> </tr>
{% endfor %}
</tbody>
</table>
</div>
</body>
</html>

  

 

Django实现数据库中表格的增删查改的更多相关文章

  1. Java连接MySQL数据库及简单的增删查改操作

    主要摘自 https://www.cnblogs.com/town123/p/8336244.html https://www.runoob.com/java/java-mysql-connect.h ...

  2. Yii2使用数据库操作汇总(增删查改、事务)

    查询 //1.简单查询 $admin=Admin::model()->findAll($condition,$params); $admin=Admin::model()->findAll ...

  3. 使用webapi绑定layui数据表格完整增删查改记录

    因为每次给layui数据表格绑定数据或者类似操作的时候  总要重新做一遍 而且忘记很多东西 所以干脆写博客把相关东西记录下来 便于查阅和修正 以下是一个完整的数据表格i项目的增删改查案例 先来看后台 ...

  4. django与mysql实现简单的增删查改

    模型定义 from django.db import models class Grades(models.Model): g_name = models.CharField(max_length=2 ...

  5. jdbc 可处理数据库事物的通用增删查改函数

    首先弄清四种隔离级别的和三种数据并发 之间的关系 通用查询函数 //使用PreparedStatement实现对不同表的通用的返回一个对象的查询操作 //使用泛型机制,参数里先传入一个类的类型 pub ...

  6. Django笔记&教程 5-1 基础增删查改

    Django 自学笔记兼学习教程第5章第1节--基础增删查改 点击查看教程总目录 第四章介绍了模型类models.Model和创建模型,相当于介绍了数据库表和如何创建数据库表. 这一章将介绍如何使用模 ...

  7. 4.在MVC中使用仓储模式进行增删查改

    原文链接:http://www.c-sharpcorner.com/UploadFile/3d39b4/crud-using-the-repository-pattern-in-mvc/ 系列目录: ...

  8. MongoDB数据库(二):增删查改

    MongoDB数据库的增删查改 1.插入数据 语法: db.集合名称.insert(document) db.table_name.insert({name:'gj',gender:1}) db.ta ...

  9. C# SQLite 创建数据库的方法增删查改语法和命令

    SQLite介绍 SQLite是一个开源.免费的小型RDBMS(关系型数据库),能独立运行.无服务器.零配置.支持事物,用C实现,内存占用较小,支持绝大数的SQL92标准. SQLite数据库官方主页 ...

随机推荐

  1. Tsung安装指南

    1. 所需要软件包unixODBC-2.2.14.tar.gzotp_src_R13B02-1.tar.gztsung-1.3.1.tar.gzTemplate-Toolkit-2.22.tar.gz ...

  2. 利用python 学习数据分析 (学习三)

    内容学习自: Python for Data Analysis, 2nd Edition         就是这本 纯英文学的很累,对不对取决于百度翻译了 前情提要: 各种方法贴: https://w ...

  3. python 爬虫入门之爬小说

    ##第一步 导包from bs4 import BeautifulSoupimport requestsimport sys ##准备class downloder(object): def __in ...

  4. docker版redmine安装部署

    数据库准备 docker run -d --name some-postgres -e POSTGRES_PASSWORD=secret -e POSTGRES_USER=redmine postgr ...

  5. CODEVS-新斯诺克

    原题地址:新斯诺克 题目描述 Description 斯诺克又称英式台球,是一种流行的台球运动.在球桌上,台面四角以及两长边中心位置各有一个球洞,使用的球分别为1 个白球,15 个红球和6 个彩球(黄 ...

  6. 通过源码看原理之 selenium

    # selenium的历史1. selenium1.x:这个时候的selenium,使用的是JavaScript注入技术与浏览器打交道,需要Selenium RC启动一个Server,将操作Web元素 ...

  7. 索引(Awakening!)

    orz写个索引,方便日后复习和补充. 目前笔记还不是很多,而且写得比较烂,望各位到访的巨佬谅解. 大概可以算作一个归纳总结? ……没链接的还没开始写或者没写完,而且不知道什么时候才能写完(咕咕咕) 一 ...

  8. mysql中的CURRENT_TIMESTAMP

    MySQL的timestamp类型可以使用CURRENT_TIMESTAMP来指定默认值,当记录增.改时,该值会自动取当前时间,如下图所示: 增加或修改记录时,该值自动变化 但是这个跟MySQL的版本 ...

  9. 解决: selenium webdriver unable to find Mozilla geckodriver

    1 安装 sudo apt-get install libqt4-dev libqtwebkit-dev 2 gem install capybara-webkit 3 在rails-helper.r ...

  10. APU (美国AMD公司研发的加速处理器)

    APU(Accelerated Processing Unit)中文名字叫加速处理器,是AMD“融聚未来”理念的产品,它第一次将中央处理器和独显核心做在一个晶片上,它同时具有高性能处理器和最新独立显卡 ...