django与mysql实现简单的增删查改
Posted xcool
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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实现简单的增删查改的主要内容,如果未能解决你的问题,请参考以下文章
Linux下编写简单C程序连接MySQL数据库,实现简单的增删查改操作