直接上代码
视图:
1 from django.shortcuts import render,render_to_response, redirect 2 from submit import models 3 4 # Create your views here. 5 6 7 def submit(request): 8 if request.method == "POST": 9 username = request.POST.get("username", None)10 password = request.POST.get("password", None)11 models.UserInfo.objects.create(username=username, password=password)12 user_list = models.UserInfo.objects.all()13 return render(request, "submit.html", { "data": user_list})14 15 16 def search(request):17 #if request.method == "POST":18 username = request.GET.get("username", None)19 results = models.UserInfo.objects.filter(username__icontains=username)20 21 return render(request, "search.html", { "result": results})22 23 24 def delete(request):25 if request.method == "GET":26 nid = request.GET.get("id")27 models.UserInfo.objects.filter(id=nid).delete()28 return render(request, "submit.html")29 30 31 def edit(request):32 if request.method == "GET":33 id = request.GET.get("id", None)34 username = request.GET.get("username", None)35 password = request.GET.get("password", None)36 models.UserInfo.objects.filter(id=id).delete()37 return render(request, "edit.html", { "username": username, "password": password})
数据库设计
1 from django.db import models2 3 # Create your models here.4 5 6 class UserInfo(models.Model):7 username = models.CharField(max_length="50")8 password = models.CharField(max_length="50")
模板
submit.html
1 2 3 4 5156 7 8 添加数据
9
查询数据
16请输入用户名
17 21输出数据
22edit.html
1 2 3 4 518 196 7 8 edit
9
search.html
1 2 3 4 5136 7 8 请输入用户名
9
查询结果
14 36 37路由:
url.py
前台页面