from django.shortcuts import render,redirect,HttpResponse
from django.views import View
from app01 import models
# Create your views here.
class login(View):
def get(self,request):
return render(request,'login.html')
def post(self,request):
u=request.POST.get('user')
p=request.POST.get('paw')
sex=request.POST.get('sex')
login=request.POST.get('login')
# models.girl.objects.filter()
flag=models.HASATTR[sex].objects.filter(name=u,password=p).first().id
print('id:',flag)
if flag:
# 1.生成随机字符串
# 2.通过cookie发送给客户端
# 3.在服务端保存,django会将session保存在数据库中(表名:django_session)session{ 随机字符串:{'user':'til','phont':123456},
# }
# 这里是设置session 可以设置多个
if login:
request.session.set_expiry(604800)
request.session['user']=u
request.session['sex']=sex
request.session['paw']=p
request.session['id'] = flag
return redirect('/index/')
else:
return redirect('/login/')
class index(View):
def get(self,request,state):
# 1.获取客户端cookie中的随机字符串
# 2.到服务器上保存的session中查找有没有对应的session随机字符串
# 3.去session查找有没有user
# 这里是获取session可以获取多个
name=request.session.get('user')
res = None
if state:
if hasattr(self,state):
res=getattr(self,state)(request)
if name:
return render(request,'index.html',{'name':name,'list':res})
else:
return redirect('/login/')
def post(self,request):
return HttpResponse('......')
def look_list(self,request):
sex=request.session.get('sex')
name_list=models.HASATTR[sex].objects.values('name')
return name_list
def look_love(self,request):
sex=request.session.get('sex')
id=request.session.get('id')
if sex=='1':
name_list = models.love.objects.filter(m=id).prefetch_related('m')
for row in name_list:
row.name = row.g.name
else:
name_list = models.love.objects.filter(g=id).prefetch_related('g')
for row in name_list:
row.name = row.m.name
return name_list