
image.png
-
$ 效果图:

image.png
- 启动Django,打开浏览器访问
(http://127.0.0.1:8000/cmdb/user_info/)
(http://127.0.0.1:8000/cmdb/userdetail-1/)
(http://127.0.0.1:8000/cmdb/useredit-1/)
image.png
image.png

image.png
-
app01\ 目录

image.png

image.png
- views.py:
from django.shortcuts import render,HttpResponse,redirect
def login(request):
# 判断用户获取数据方式是GET,就返回什么数据
if request.method == "GET":
return render(request, 'login.html')
# 判断用户获取数据方式是POST,就判断用户提交的数据是否正确
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()
# print(obj) obj->None(就是没有这个用户)
if obj:
return redirect('/cmdb/index/')
else:
return render(request, 'login.html')
else:
# PUT,DELETE,HEAD,OPTION...
return redirect("/index/")
def index(request):
return render(request, 'index.html')
def user_info(request):
if request.method == "GET":
user_list = models.UserInfo.objects.all()
# print(user_list.query)
# QuerySet [obj,obj,]
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/') # 效果跟下面两个一样(在浏览器添加数据到数据库)
# user_list = models.UserInfo.objects.all()
# return render(request,'user_info.html', {'user_list':user_list})
def user_detail(request,nid):
obj = models.UserInfo.objects.filter(id=nid).first()
# 取单条数据,如果不存在,直接报错(不推荐)
# models.UserInfo.objects.get(id=nid)
return render(request, 'user_detail.html',{'obj':obj})
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":
nid = request.POST.get('id')
u = request.POST.get('username')
p = request.POST.get('password')
models.UserInfo.objects.filter(id=nid).update(username=u,password=p)
# 跳转到自己所想的页面(html)
return redirect("/cmdb/user_info/")
from app01 import models
def orm(request):
# 创建 操作数据操作表(推荐)*****************
# models.UserInfo.objects.create(username='root',password='123')
# 效果同上
# dic = {'username': 'eric', 'password':'666'}
# models.UserInfo.objects.create(**dic)
# 效果(作用)同上
# obj = models.UserInfo(username='root',password='123')
# obj.save()
# 查 ******************
# result = models.UserInfo.objects.all()
# filter查找哪一个
# result = models.UserInfo.objects.filter(username='root')
# result,QuerySet => Django => []
# [obj(id,username,password), obj(id,username,password), obj(id,username,password)]
# for row in result:
# print(row.id,row.username,row.password)
# print(result)
# 删除*********************all(), filter()
# models.UserInfo.objects.filter(username="root").delete()
# 更新**********************update(),all(), filter()
models.UserInfo.objects.filter(id=3).update(password='889')
return HttpResponse('orm')
from django.views import View
class Home(View):
# 调用父类中的dispatch(相当于助理,)
def dispatch(self, request, *args, **kwargs):
print('before')
result = super(Home,self).dispatch(request, *args, **kwargs)
print('after')
return result
def get(self,request):
print(request.method)
return render(request, 'home.html')
def post(self,request):
print(request.method, 'POST')
return render(request, 'home.html')

image.png
- urls.py:
from django.conf.urls import url,include
from django.contrib import admin
from app01 import views
urlpatterns = [
url(r'^login/', views.login),
url(r'^index/', views.index),
url(r'^user_info/', views.user_info),
url(r'^userdetail-(?P<nid>\d+)/', views.user_detail),
url(r'^userdel-(?P<nid>\d+)/', views.user_del),
url(r'^useredit-(?P<nid>\d+)/', views.user_edit),
url(r'orm/', views.orm),
]

image.png
- user_edit.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">
Guido(Python之父,仁慈的独裁者)
</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:48px;left:210px;bottom:0;right:0;overflow: auto">
<h1>编辑用户</h1>
<form method="post" action="/cmdb/useredit-{{obj.id}}/">
<!--id不能改-->
<input style="display: none" type="text" 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>
- 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">
Guido(Python之父,仁慈的独裁者)
</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:48px;left:210px;bottom:0;right:0;overflow: auto">
<h3>添加用户</h3>
<form method="POST" action="/cmdb/user_info/">
<input type="text" name="user">
<input type="text" 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>

