[增删改查]Python之使⽤Django+LayUI做后台管理
⼀、前⾔
Python 使⽤ Django 框架,进⾏ web 开发,⼀点都不⽐⽬前 JavaEE 最简便的框架 SpringBoot 差,甚⾄是更加便捷。
① 使⽤ python manage.py makemigrations、python manage.py migrate,即可实现 JavaEE ⽅⾯的 JPA框架 的代码操作数据库的功能
② 使⽤例如 python manage.py inspectdb > booktest/models.py(booktest/models.py 为⾃⼰的模型代码位置),即可实现 类似 MyBatis 的逆向⼯程,通过数据库中建⽴的表⾃动⽣成相应的 model 代码
③ 模型.objects 下已经实现了基本 CRUD 的⽅法,可以直接调⽤,类似 SpringBoot 中的 SpringData+JPA 的功能,且代码量极少。
④ ….等等
本⽂代码改⾃前⽂:
只不过交互从 React 改为强⼤的 Django模板引擎
下⾯来个案例,使⽤ Python 做⼩型项⽬须知
⼆、演⽰
2.0.⾸页
2.1.增加
2.2.修改
2.3.查询
2.4.删除
三、代码
3.1.M-studen/models.py
from django.db import models
class student_info(models.Model):
# 字符
t_name=models.CharField(max_length=20,default='linhongcun')
# 数字
t_age=models.IntegerField(default=21)  # 使⽤数字长度设置⽆效
# 图⽚
t_image=models.CharField(max_length=300,default='itaem.oss-cn-shenzhen.aliyuncs/20180509083509.jpg?Expires=4679469344&OSSAcce 3.2.V-student/views.py
from django.shortcuts import render,redirect
from django.views.decorators.csrf import csrf_exempt
f import settings    # 获取 settings.py ⾥边配置的信息
import os
from .models import *
# 1.1.前往 index 页(all)
def all_page(request):
data = student_info.objects.all()
content={'data': data}
return render(request, 'student/all.html', content)
# 1.2.前往 add 页
def add_page( request ):
return render(request, 'student/add.html')
# 2.增
@csrf_exempt
def add_student(request):
t_name = request.POST['tName']
t_age = request.POST['tAge']
t_image = request.FILES['tImage']
fname = os.path.join(settings.MEDIA_ROOT, t_image.name)
with open(fname, 'wb') as pic:
for c in t_image.chunks():
pic.write(c)
student=student_info()
student.t_name=t_name
student.t_age=t_age
# 存访问路径到数据库
student.t_image = os.path.join("/static/media/", t_image.name)
student.save()
return redirect('/allPage')
# 3.1.查 - name
def search_student(request):
t_name = request.GET['q']
student=student_info.objects.filter(t_name=t_name)
content={'data':student}
return render(request,'student/all.html', content)
# 3.2.查 - id
def search_student_id(request,student_id):
student=student_info.objects.filter(id=student_id)
content = {'data': student}
print(content)
return  render(request,'student/update.html',content)
# 4.改
@csrf_exempt
def update_student(request):
id=request.POST['t_id']
t_name = request.POST['tName']
t_age = request.POST['tAge']
# 缺陷:⽂件必传
t_image = request.FILES['tImage']
fname = os.path.join(settings.MEDIA_ROOT, t_image.name)
with open(fname, 'wb') as pic:
for c in t_image.chunks():
pic.write(c)
t_image = os.path.join("/static/media/", t_image.name)
student_info.objects.filter(id=id).update(t_name=t_name,t_age=t_age,t_image=t_image)
return redirect('/allPage')
# 5.删
def delete_student(request,student_id):
student_info.objects.filter(id=student_id).delete()
return redirect('/allPage')
3.3.T-templates/student
3.3.0.base.html 基页
<!DOCTYPE html>
<html lang="en">
<head>
django admin 自定义页面<meta charset="UTF-8">
<title>DjangoCURD</title>
{#  注意引⼊⽂件的⽅式  #}
{% load staticfiles %}
<link rel="stylesheet"href="/static/layui/css/layui.css">
{#  设置⽹页标题图标  #}
<link rel="icon"type="image/x-icon"href="/static/media/icon.ico" />
</head>
<body>
{% block content %}
{% endblock %}
</body>
</html>
3.3.1.all.html ⾸页

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。