from django.shortcuts import render,HttpResponse,redirect
import os,sys
from django.views import View
from cmdb import models
# Create your views here.
User_dict ={
"1":{"name":"大胖小子","age":18,"sex":"Man"},
"2":{"name":"金虎","age":6,"sex":"Man"},
"3":{"name":"妙音","age":5,"sex":"WoMan"},
"4":{"name":"飞云","age":6,"sex":"Man"},
}
#主页
def index(request):
return render(request,"index.html",{"User_dict":User_dict})
def login(request):
if request.method == "GET":
return render(request, "login.html")
elif request.method == "POST":
u=request.POST.get("user",None)
p=request.POST.get("password",None)
up=request.POST.get("update")
update_msg = "更新%s密码成功!"%u
if models.UserInfo.objects.filter(username=u) and models.UserInfo.objects.filter(password=p):
return render(request,'zhuye.html',{"user":u})
if up =="更新":
models.UserInfo.objects.filter(username=u).update(password=p)
return render(request,'login.html',{'update_msg':update_msg})
else:
error_msg="用户名或密码错误"
else:
return redirect("/index")
return render(request, "login.html",{"error_msg":error_msg})
def zhuce(request):
# if request.method == "GET":
# return render(request, "zhuce.html")
# elif request.method == "POST":
# #radio
# radio = request.POST.get("gender")
# #chickbox
# chickbox = request.POST.getlist("favor")
# #select
# select=request.POST.getlist("city")
# #file
# send=request.FILES.get("send")
# print(send)
# file_path=os.path.join("upload",send.name)
# with open(file_path,mode="wb") as f:
# for i in send.chunks():
# f.write(i)
# return render(request, "zhuce.html")
if request.method == "GET":
return render(request, "zhuce.html")
elif request.method == "POST":
username=request.POST.get("username")
password=request.POST.get("password")
models.UserInfo.objects.create(username=username,password=password)
zhuce_msg = "注册成功!"
return render(request, "zhuce.html",{"zhuce_msg":zhuce_msg})
class Home(View):
def get(self,request):
print(request.method)
return render(request, "home.html")
def post(self,request):
u = request.POST.get("user", None)
p = request.POST.get("pwd", None)
if u == "root" and p == "123":
return redirect('/index')
else:
error_msg = "用户名或密码错误"
return render(request,"home.html", {"error_msg": error_msg})
def detail(request,nid):#通过url的nid参数传递过来,不用去request中的nid去获取值了
detail_info=User_dict[nid]
return render(request,"detail.html",{"detail_info":detail_info})
def orm(request):
#创建
#对UserInfo类(数据库表)进行增加操作
username = request.POST.get("username")
password = request.POST.get("password")
# dic={'username':username,'password':password}
# models.UserInfo.objects.create(**dic)
#models.UserInfo.objects.create(username=username,password=password) #推荐用这种添加数据库数据
#obj = models.UserInfo(username="fuyuteng",password="123")
#obj.save()创建数据第二种方法
'''查找所有'''
#result=models.UserInfo.objects.all()
'''过滤查询username=root的所有符合查询的条件'''
result = models.UserInfo.objects.filter(username='root')
'''删除'''
delete=models.UserInfo.objects.filter(username=username,password=password).delete()#删除指定的 delete=models.UserInfo.objects.all().delete()#删除所有
'''update'''
models.UserInfo.objects.update(password=password)#更新所有password的密码
update=models.UserInfo.objects.filter(username=username).update(password=password)#更新指定用户名的密码
return render(request,'orm.html',{'result':result})