s14_示例:model操作
- 示例(基本增删改查和用户登陆、删除、查看详细、字段、参数、外键操作、外键增加):
# app01/urls.py
urlpatterns = [
url(r'^login/', views.login),
url(r'^orm/', views.orm),
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),
]
# app01/views.py
def login(request):
# models.UserInfo.objects.create(caption='DBA')
if request.method =='GET':
# GET 必须大写,否则无法进入
return render(request,'login.html')
elif request.method=='POST':
# 数据库中获取数据
u = request.POST.get('user')
p = request.POST.get('pwd')
# obj=models.UserInfo.objects.filter(username=u,password=p)
# obj=models.UserInfo.objects.filter(username=u,password=p).first() # 一个obj ****
# print(obj)
# obj=models.UserInfo.objects.filter(username=u,password=p).count() # 获取个数
obj=models.UserInfo.objects.filter(username=u,password=p).first()
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()
# QuerySet[obj(id,username..),obj,]
# QuerySet[obj(id,username,email,user_group_id,user_group_id(uid,caption)),obj,]
# for row in user_list:
# print(row.id)
# print(row.user_group)
# print(row.user_group.uid)
# print(row.user_group.caption)
# print(user_list.query)
# 查看SQL语句
group_list = models.UserGruop.objects.all()
return render(request,'user_info.html',{'user_list':user_list,'group_list':group_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')
print(nid,u,p)
models.UserInfo.objects.filter(id=nid).update(username=u,password=p)
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= 'alex',password = '123')
# obj.save()
# 查
# result = models.UserInfo.objects.all()
# result:QuerySet django提供的[]
# [obj(id,usernmae,password),obj.obj] obj是 UserInfo对象
# result = models.UserInfo.objects.filter(username='root')
# result = models.UserInfo.objects.filter(username='root',password='123')
# for row in result:
# print(row.id,row.username,row.password)
# print(result)
# 删除
# models.UserInfo.objects.all().delete()
# models.UserInfo.objects.filter(id=4).delete()
# models.UserInfo.objects.filter(username='alex').delete()
# 更新
# models.UserInfo.objects.all().update(password='6666')
# models.UserInfo.objects.filter(id=3).update(password='999')
# 一对多
# user_list =models.UserInfo.objects.all()
# models.UserInfo.objects.create(
# username='root1',
# password='123',
# email='root1@eee.com',
# test='dfdfdfd',
# user_group= models.UserInfo.objects.filter(id=1).first()
# )
models.UserInfo.objects.create(
username='root1',
password='123',
email='root1@eee.com',
test='dfdfdfd',
user_group_id = 1,
)
return HttpResponse('orm')
# models.py
from django.db import models
# Create your models here.
# app01_userinfo
class UserGruop(models.Model):
uid = models.AutoField(primary_key=True)
# 创建自增列
caption =models.CharField(max_length=32)
ctime=models.DateField(auto_now_add=True,null=True)
utime=models.DateField(auto_now=True,null=True)
# user_list = UserInfo.objects.all()
# for row in user_list:
# print(row.user_group_id)
# print(row.user_group)
# print(row.user_group.uid)
# print(row.user_group.cption)
class UserInfo(models.Model):
# 自动创建ID列,自增,主键
# 基本类型:字符串、数字、时间、二进制
username = models.CharField(max_length=32,blank=True,verbose_name='用户名')
password = models.CharField(max_length=64,help_text='pwd')
# 长度改小,则数据丢失
email = models.CharField(max_length=60,default='xxxx@164.com')
test = models.EmailField(max_length=19,null=True)
user_group = models.ForeignKey('UserGruop',to_field='uid',default=1)
# 不添加to_field默认使用主键
# 数据库中:user_group_id 数字
# (uid,caption,ctime,utime)
user_type_choices =(
(1,'超级用户'),
(2,'普通用户'),
(3,'普普通用户'),
)
user_type_id = models.IntegerField(choices=user_type_choices,default=1)
# test = models.URLField(max_length=19,null=True)
# test = models.GenericIPAddressField()
# gender = models.CharField(max_length=60,null=True)
# login.html
<form action="/cmdb/login/"method="POST" enctype="multipart/form-data">
<p><input type="text"name="user"placeholder="用户名"></p>
<p><input type="password"name="pwd"placeholder="密码"></p>
<p><input type="submit"value="提交"></p>
</form>
# index.html(含菜单)
<style>body{margin: 0;}
.menu{display: block;padding: 5px;}</style>
<div style="height: 48px;color: white;">welcome</div>
<div>
<div style="position: absolute;top:48px;bottom:0;left: 0;width: 200px;
<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"></div>
</div>
# user_info.html
<div style="height: 48px;color: white;">welcome</div>
<div>
<div style="position: absolute;top:48px;bottom:0;left: 0;width: 200px;
<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 action="/cmdb/user_info/"method="POST">
<input type="text"name="user">
<input type="text"name="pwd">
<select name="group_id" id="">
{% for item in group_list %}
<option value="{{ item.uid }}">{{ item.caption }}</option>
{% endfor %}
</select>
<input type="submit">
</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>
# user_detail.html(详细信息)
<div style="height: 48px;color: white;">welcome</div>
<div>
<div style="position: absolute;top:48px;bottom:0;left: 0;width: 200px;
<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>
<h5>{{ obj.id }}</h5>
<h5>{{ obj.username }}</h5>
<h5>{{ obj.password }}</h5>
</div>
</div>
# user_edit.html
<div style="height: 48px;color: white;">welcome</div>
<div>
<div style="position: absolute;top:48px;bottom:0;left: 0;width: 200px;
<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 action="/cmdb/useredit-{{ obj.id }}/"method="POST">
<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">
</form>
</div>
</div>
# admin.py
from django.contrib import admin
from app01 import models
# Register your models here.
admin.site.register(models.UserInfo)