student学生信息表增删改
redirect 家的是url也就是urls页面的
href 、action也是,
修改删除需要获取id
if endif
for endfor连着用,不可少
urls:
from django.conf.urls import url from django.contrib import admin from app.views import classes from app.views import student urlpatterns = [ url(r'^admin/', admin.site.urls), url(r'^classes.html$',classes.get_classes),#不加^访问点击页面没反应 url(r'^add_classes.html$',classes.add_classes), url(r'^del_classes.html$', classes.del_classes), url(r'^edit_classes.html$',classes.edit_classes), url(r'^student$', student.get_student), url(r'^add_student$', student.add_student), url(r'^del_student$', student.del_student), url(r'^edit_student$',student.edit_student), ]
student.py
from django.shortcuts import render from django.shortcuts import redirect from app import models def get_student(req): stu_list=models.Student.objects.all() return render(req,'get_student.html',{'stulist':stu_list}) def add_student(req): if req.method=='GET': cs_list=models.Classes.objects.all() return render(req,'add_student.html',{'cslist':cs_list}) elif req.method=='POST': u=req.POST.get('username1') a=req.POST.get('age1') g=req.POST.get('gender1') c=req.POST.get('cs1') models.Student.objects.create( username=u, age=a, gender=g, #cs=c 可以看到cs是字符串类型,c是具体数值的id,不匹配,数据库一般保存是 # cs_id即cs.id cs_id=c ) return redirect('/student') def del_student(req): nid=req.GET.get('nid') models.Student.objects.filter(id=nid).delete() return redirect('/student') def edit_student(req): if req.method == 'GET': nid=req.GET.get('nid') obj=models.Student.objects.filter(id=nid).first() cs_list=models.Classes.objects.values('id','title') return render(req, 'edit_student.html', {'obj': obj,'cslist':cs_list}) elif req.method == 'POST': nid=req.GET.get('nid') u = req.POST.get('username1') a = req.POST.get('age1') g = req.POST.get('gender1') c = req.POST.get('cs1') models.Student.objects.filter(id=nid).update( username=u, age=a, gender=g, # cs=c 可以看到cs是字符串类型,c是具体数值的id,不匹配,数据库一般保存是 # cs_id即cs.id cs_id=c ) return redirect('/student')
model.py
from django.db import models # Create your models here. class Classes(models.Model):#班级表 title=models.CharField(max_length=32) m=models.ManyToManyField('Teacheres')#加上引号,全执行完后才来执行,表的多对多 class Teacheres(models.Model):#教师表 name=models.CharField(max_length=32) class Student(models.Model): username=models.CharField(max_length=32) age=models.IntegerField() #gender=models.BooleanField()布尔值 gender = models.NullBooleanField()#可以为空的布尔值 cs=models.ForeignKey(Classes)#这个classes不用加引号,因为在上面,顺序执行
get_student.html
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> </head> <body> <div><a href="/add_student">添加</a></div> <div> <table border="1"> <thead> <tr> <th>ID</th> <th>姓名</th> <th>年龄</th> <th>性别</th> <th>班级</th> <th>操作</th> </tr> </thead> <tbody> {% for row in stulist %} <tr> <td>{{ row.id }}</td> <td>{{ row.username }}</td> <td>{{ row.age }}</td> <td>{{ row.gender }}</td> <td>{{ row.cs.title }}</td><!-- 另一张表的内容--> <td><a href="del_student?nid={{ row.id }}">删除</a> | <a href="edit_student?nid={{ row.id }}">修改</a> </td> </tr> {% endfor %} </tbody> </table></div> </body> </html>
add_student.html
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> </head> <body> {% csrf_token %} <h3>添加学生</h3> <form action="/add_student"method="POST"> <p> <input type="text"name="username1"placeholder="学生姓名"></p> <p><input type="text"name="age1"placeholder="年龄"></p> {#<p><input type="text"name="gender1"placeholder="性别 "></p>#} <p>男:<input type="radio"name="gender1"value="1"> 女:<input type="radio"name="gender1"value="0"> </p> {#<p><input type="text"name="banji"placeholder="班级"></p>#} <p><select name="cs1"> {% for row in cslist %} <option value="{{ row.id}}">{{ row.title }}</option> {% endfor %} </select></p> <p><input type="submit"value="提交"></p> </form> </body> </html>
edit——student.html
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> </head> <body> {% csrf_token %} <form action="edit_student?nid={{ obj.id }}"method="POST"> <p> <input type="text"name="username1" value="{{ obj.username }}"></p> <p><input type="text"name="age1"value="{{ obj.age }}"></p> <p> {% if obj.gender %} 男:<input type="radio"name="gender1"checked="checked"value="1"> 女:<input type="radio"name="gender1"value="0"> {% else %} 男:<input type="radio"name="gender1"value="1"> 女:<input type="radio"name="gender1"value="0"checked="checked"> {% endif %} </p> <p><select name="cs1"> {% for row in cslist %} {% if row.id == obj.cs_id %} <option value="{{ row.id}}"selected="selected">{{ row.title }}</option> {% else %} <option value="{{ row.id}}">{{ row.title }}</option> {% endif %} {% endfor %} </select></p> <p><input type="submit"value="提交"></p> </form> </body> </html>