from django.db import models
# Create your models here.
class UserGroup(models.Model): # models.Model注意一定要有否则不能创建表
# AutoField(primary_key=True)表示自增, 固定写法
uid = models.AutoField(primary_key=True)
caption = models.CharField(max_length=32)
ctime = models.DateTimeField(auto_now_add=True, null=True)
uptime = models.DateTimeField(auto_now=True,null=True)
class UserInfo(models.Model):
# Django自动创建id列,自增,主键
# 用户名列,字符串类型,指定长度
# 字符串.数字.时间.二进制
username = models.CharField(max_length=32,blank=True,verbose_name="用户名")
password = models.CharField(max_length=60,help_text='pwd')
email = models.CharField(max_length=60)
test = models.EmailField(max_length=19,null=True)
# user_group_id 数字
user_group = models.ForeignKey('UserGroup',to_field='uid',default=1)
# 下面的数据会放到内存
user_type_choice = (
(1, '超级用户'),
(2, '普通用户'),
(3, '一般用户'),
)
user_type_id = models.IntegerField(choices=user_type_choice, default=1)
from django.shortcuts import render,HttpResponse,redirect
def login(request):
models.UserGroup.objects.create(caption='DBA')
# 判断用户获取数据方式是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()
for row in user_list:
print(row.id)
print(row.user_group.uid)
print(row.user_group.caption)
# 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')
<!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> |
<span>{{row.user_group.caption}} </span>
<a href="/cmdb/userdel-{{ row.id }}/">删除</a> |
<a href="/cmdb/useredit-{{ row.id }}/">编辑</a> |
</li>
{% endfor %}
</ul>
</div>
</div>
</body>
</html>