创建类根据类自动创建数据表
app 下的models.py
settings:
installed_app
之后执行:
python manage.py makemigrations
python manage.py migrate
配置mysql数据库
python3必须在项目中的init中配置以下内容
根据类对数据库表中的数据进行各种操作
数据添加
from app01 import models
def orm(request):
# 创建第一种方法
# models.UserInfo.objects.create(
# username = 'root',
# password = '123'
# )
# 创建第二种方法
# obj = models.UserInfo(username = 'root1',password = '123')
# obj.save()
# 创建第三种方法
dic = {'username' : 'root2', 'password':666}
models.UserInfo.objects.create(**dic)
数据查询
from app01 import models
def orm(request):
# 查询所有数据
# result = models.UserInfo.objects.all()
# 按条件查询数据
result = models.UserInfo.objects.filter(username = 'root')
for row in result:
print(row.id, row.username)
删除数据
from app01 import models
def orm(request):
# 删除数据
models.UserInfo.objects.filter(id = 4).delete()
更新数据
from app01 import models
def orm(request):
#更新数据
models.UserInfo.objects.filter(id = 2).update(password="12312312")
一个增删改查的例子
url设计
urlpatterns = [
url(r'^index/', views.index, name="indexx"),
url(r'^login/', views.login),
url(r'^user_info', views.user_info),
url(r'^userdetail-(?P<nid>\d+)', views.userdetail),
url(r'^userdel-(?P<nid>\d+)', views.user_del),
url(r'^useredit-(?P<nid>\d+)', views.user_edit),
]
页面跳转代码(view.py)
from django.shortcuts import render, HttpResponse,redirect
from app01 import models
# Create your views here.
#主页
def index(request):
return render(request, 'index.html')
#登录
def login(request):
if request.method == "GET":
return render(request, 'login.html')
elif request.method == "POST":
# 数据库中执行 select * from user where username = 'x' and password='x'
u = request.POST.get('user')
p = request.POST.get('pwd')
obj = models.UserInfo.objects.filter(username = u, password = p).first()
count = models.UserInfo.objects.filter(username = u, password = p).count()
if obj:
return redirect('/cmdb/index/')
else:
return render(request, 'login.html')
else:
return redirect('/index/')
#用户列表
def user_info(request):
if request.method == "GET":
user_list = models.UserInfo.objects.all()
return render(request, 'user_info.html', {'user_list' : user_list})
elif request.method == "POST":
u = request.POST.get('user')
p = request.POST.get('pwd')
models.UserInfo.objects.create(username=u, password=p)
return redirect('/cmdb/user_info/')
#删除用户
def user_del(request, nid):
models.UserInfo.objects.filter(id=nid).delete()
return redirect('/cmdb/user_info/')
#用户修改
def user_edit(request, nid):
if request.method == "GET":
obj = models.UserInfo.objects.filter(id=nid).first()
return render(request, 'user_edit.html', {'obj' : obj})
elif request.method == "POST":
u = request.POST.get("username")
p = request.POST.get("password")
models.UserInfo.objects.filter(id=nid).update(username=u, password=p)
return redirect('/cmdb/user_info/')
#用户详情
def userdetail(request, nid):
obj = models.UserInfo.objects.filter(id=nid).first()
return render(request, 'user_detail.html', {'obj' : obj})
主页面(index.html)
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<style>
body{
margin: 0;
}
.menu{
display: block;
padding: 5px;
}
</style>
</head>
<body>
<div style="height: 48px; background-color: black; color: white">dancheng</div>
<div>
<div style="position: absolute; top:48px; bottom: 0; left:0; width: 200px; background-color: brown">
<a class="menu" href="/cmdb/user_info/">用户管理</a>
<a class="menu" href="/cmdb/user_group/">用户组管理</a>
</div>
<div style="position: absolute; top:36px; left:210px; bottom: 0; right: 0;overflow: auto;">
</div>
</div>
</body>
</html>
显示用户列表页面(user_info.html)
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<style>
body{
margin: 0;
}
.menu{
display: block;
padding: 5px;
}
</style>
</head>
<body>
<div style="height: 48px; background-color: black; color: white">dancheng</div>
<div>
<div style="position: absolute; top:48px; bottom: 0; left:0; width: 200px; background-color: brown">
<a class="menu" href="/cmdb/user_info/">用户管理</a>
<a class="menu" href="/cmdb/user_group/">用户组管理</a>
</div>
<div style="position: absolute; top:36px; left:210px; bottom: 0; right: 0;overflow: auto;">
<h3>添加用户</h3>
<form action="/cmdb/user_info" method="post">
<input type="text" name="user">
<input type="password" name="pwd">
<input type="submit" value="添加">
</form>
<h3>用户列表</h3>
<ul>
{% for row in user_list %}
<li>
<a href="/cmdb/userdetail-{{ row.id }}/">{{ row.username }}</a> |
<a href="/cmdb/userdel-{{ row.id }}/">删除</a> |
<a href="/cmdb/useredit-{{ row.id }}/">编辑</a>
</li>
{% endfor %}
</ul>
</div>
</div>
</body>
</html>
用户详情页面(user_detail.html)
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<style>
body{
margin: 0;
}
.menu{
display: block;
padding: 5px;
}
</style>
</head>
<body>
<div style="height: 48px; background-color: black; color: white">dancheng</div>
<div>
<div style="position: absolute; top:48px; bottom: 0; left:0; width: 200px; background-color: brown">
<a class="menu" href="/cmdb/user_info/">用户管理</a>
<a class="menu" href="/cmdb/user_group/">用户组管理</a>
</div>
<div style="position: absolute; top:36px; left:210px; bottom: 0; right: 0;overflow: auto;">
<h1>用户详细信息</h1>
<h5>{{ obj.id }}</h5>
<h5>{{ obj.username }}</h5>
<h5>{{ obj.password }}</h5>
</div>
</div>
</body>
</html>
用户修改页面(user_edit)
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<style>
body{
margin: 0;
}
.menu{
display: block;
padding: 5px;
}
</style>
</head>
<body>
<div style="height: 48px; background-color: black; color: white">dancheng</div>
<div>
<div style="position: absolute; top:48px; bottom: 0; left:0; width: 200px; background-color: brown">
<a class="menu" href="/cmdb/user_info/">用户管理</a>
<a class="menu" href="/cmdb/user_group/">用户组管理</a>
</div>
<div style="position: absolute; top:36px; left:210px; bottom: 0; right: 0;overflow: auto;">
<h1>编辑用户</h1>
<form action="/cmdb/useredit-{{ obj.id }}/" method="post">
<input type="hidden" name="id" value="{{ obj.id }}">
<input type="text" name="username" value="{{ obj.username }}">
<input type="text" name="password" value="{{ obj.password }}">
<input type="submit" value="修改">
</form>
</div>
</div>
</body>
</html>