Django实战,小网站实现增删改查

直接上代码

视图:

 from django.shortcuts import render,render_to_response, redirect
from submit import models # Create your views here. def submit(request):
if request.method == "POST":
username = request.POST.get("username", None)
password = request.POST.get("password", None)
models.UserInfo.objects.create(username=username, password=password)
user_list = models.UserInfo.objects.all()
return render(request, "submit.html", {"data": user_list}) def search(request):
#if request.method == "POST":
username = request.GET.get("username", None)
results = models.UserInfo.objects.filter(username__icontains=username) return render(request, "search.html", {"result": results}) def delete(request):
if request.method == "GET":
nid = request.GET.get("id")
models.UserInfo.objects.filter(id=nid).delete()
return render(request, "submit.html") def edit(request):
if request.method == "GET":
id = request.GET.get("id", None)
username = request.GET.get("username", None)
password = request.GET.get("password", None)
models.UserInfo.objects.filter(id=id).delete()
return render(request, "edit.html", {"username": username, "password": password})

数据库设计

 from django.db import models

 # Create your models here.

 class UserInfo(models.Model):
username = models.CharField(max_length="")
password = models.CharField(max_length="")

模板

submit.html

 <!DOCTYPE html>
<html>
<head lang="en">
<meta charset="UTF-8">
<title></title>
</head>
<body>
<h1>添加数据</h1>
<form action="/submit/" method="post">
<input type="text" name="username"/>
<input type="password" name="password"/>
<input type="submit" value="提交"/> </form>
<h1>查询数据</h1>
<h1>请输入用户名</h1>
<form action="/search/" method="get">
<input type="text" name="username"/>
<input type="submit" value="查询"/>
</form>
<h1>输出数据</h1>
<form action="/submit/" method="get">
<table border="">
<thead>
<td>ID</td>
<td>用户名</td>
<td>密码</td>
<td>操作</td>
{% for line in data %}
<tr>
<td>{{ line.id }}</td>
<td>{{ line.username }}</td>
<td>{{ line.password }}</td>
<td> <a href="/edit?id={{ line.id }}&username={{ line.username }}&password={{ line.password }}">编辑</a>
<a href="/delete?id={{ line.id }}">删除</a>
</td>
</tr>
{% endfor %}
</body>
</html>

submit.html

edit.html

 <!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/html">
<head lang="en">
<meta charset="UTF-8">
<title></title>
</head>
<body>
<h1>edit</h1>
<form action="/submit/" method="post"> <h6>用户名</h6><input type="text" name="username" value={{ username }}>
<br/>
<h6>密码</h6><input type="text" name="password" value={{ password }}>
<br/>
<input type="submit" value=" 确认修改"/> </form>
</body>
</html>

edit.html

search.html
 <!DOCTYPE html>
<html>
<head lang="en">
<meta charset="UTF-8">
<title></title>
</head>
<body>
<h1>请输入用户名</h1>
<form action="/search/" method="post">
<input type="text" name="username"/>
<input type="submit" value="查询"/>
</form>
<h1>查询结果</h1>
<form action="/search/" method="get">
<table border="">
<thead>
<td>ID</td>
<td>用户名</td>
<td>密码</td>
<td>操作</td>
{% for rts in result %}
<tr>
<td>{{ rts.id }}</td>
<td>{{ rts.username }}</td>
<td>{{ rts.password }}</td>
<td> <a href="/edit?id={{ rts.id }}&username={{ rts.username }}&password={{ rts.password }}">编辑</a>
<a href="/delete?id={{ rts.id }}">删除</a>
</td>
</tr>
{% endfor %}
</thead>
</table>
</form>
</body>
</html>

search.html

路由:

url.py

前台页面

Django实战,小网站实现增删改查

上一篇:极限编程,最强N皇后JAVA解题代码,4秒出15皇后,33秒出16皇后


下一篇:剑指Offer - 九度1369 - 字符串的排列