手撸系列2用户的增删改查
第一种方法
userlist.html文件
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>用户列表</title> </head> <body> <table border="1"> <thead> <tr> <th>id</th> <th>name</th> <th>password</th> <th>addr</th> <th>iphone</th> <th>删除</th> <th>编辑</th> </tr> </thead> <tbody> {% for user in user_list %} <tr> <td>{{ user.id }}</td> <td>{{ user.name }}</td> <td>{{ user.password }}</td> <td>{{ user.address }}</td> <td>{{ user.phone }}</td> <td><a href="/userdelete?id={{ user.id }}">删除</a></td> <td><a href="/updateuser?id={{ user.id }}">编辑</a></td> </tr> {% endfor %} </tbody> <a href="/adduser/">新增用户</a> </table> </body> </html>
adduser.html文件
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>新增用户</title> </head> <body> <form action="" method="post"> <p>用户名:<input type="text" name="name"></p> <p>密码:<input type="password" name="password"></p> <p>地址<input type="text" name="addr"></p> <p>电话<input type="text" name="phone"></p> <input type="submit" value="提交 "> </form> </body> </html>
updateuser.html文件
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> </head> <body> <form action="" method="post"> <p><input type="hidden" name="id" value="{{ user.id }}"></p> <p>用户名:<input type="text" name="name" value="{{ user.name }}"></p> <p>密码:<input type="text" name="password" value="{{ user.password }}"></p> <p>地址:<input type="text" name="addr" value="{{ user.address }}"></p> <p>电话:<input type="text" name="phone" value="{{ user.phone }}"></p> <input type="submit" value="提交"> </form> </body> </html>
urls文件
from django.conf.urls import url from django.contrib import admin from app03 import views urlpatterns = [ url(r'^admin/', admin.site.urls), url(r'^userlist/',views.userlist), url(r'^userdelete/',views.userdelete), url(r'^adduser/',views.adduser), url(r'^updateuser/',views.updateuser), ]
views文件
from django.shortcuts import HttpResponse,render,redirect from app03 import models def userlist(request): if request.method=='GET': ret=models.User.objects.all() print(ret) for i in ret: print(type(i)) print(i.name) return render(request,'userlist.html',{'user_list':ret}) def userdelete(request): if request.method=='GET': id=request.GET.get('id') ret=models.User.objects.filter(id=id).delete() print(ret) return redirect('/userlist/') def adduser(request): if request.method=='GET': return render(request,'adduser.html') elif request.method=='POST': name=request.POST.get('name') pwd=request.POST.get('password') address=request.POST.get('addr') phone=request.POST.get('phone') #新增用户方式一 user=models.User.objects.create(name=name,password=pwd,address=address,phone=phone) #新增用户方式二 # user=models.User(name=name,password=pwd,address=address,phone=phone) # user.save() print(user.name) print(type(user)) return redirect('/userlist/') def updateuser(request): if request.method=="GET": id=request.GET.get('id') user=models.User.objects.filter(id=id).first() return render(request,'updateuser.html',{'user':user}) if request.method=="POST": id=request.POST.get('id') # id2=request.GET.get('id') # print(id) # print(id2) name=request.POST.get('name') pwd=request.POST.get('password') address=request.POST.get('addr') phone=request.POST.get('phone') models.User.objects.filter(id=id).update(name=name,password=pwd,address=address,phone=phone) return redirect('/userlist/')
models文件
from django.db import models # Create your models here. class User(models.Model): id=models.AutoField(primary_key=True) name=models.CharField(max_length=32) password=models.CharField(max_length=32) address=models.CharField(max_length=32) phone=models.CharField(max_length=64,default='120')
第二种方法
updateuser文件
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> </head> <body> <form action="/updateuser/?id={{ user.id }}" method="post"> <p><input type="hidden" name="id" value="{{ user.id }}"></p> <p>用户名:<input type="text" name="name" value="{{ user.name }}"></p> <p>密码:<input type="text" name="password" value="{{ user.password }}"></p> <p>地址:<input type="text" name="addr" value="{{ user.address }}"></p> <p>电话:<input type="text" name="phone" value="{{ user.phone }}"></p> <input type="submit" value="提交"> </form> </body> </html>