Django-session+CBV+ORM应用
Posted wtil
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Django-session+CBV+ORM应用相关的知识,希望对你有一定的参考价值。
男生女生查看
from django.contrib import admin from django.urls import path,re_path from app01 import views #在Django 2.0 之后在路由中写正则,需要使用re_path urlpatterns = [ path(‘admin/‘, admin.site.urls), path(‘login/‘,views.login.as_view()), re_path(‘index/(?P<state>look_list|look_love){0,}$‘,views.index.as_view()),#实现动态路由,并命名state ]
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
from django.db import models class man(models.Model): name=models.CharField(max_length=32) password = models.CharField(max_length=32) class girl(models.Model): name=models.CharField(max_length=32) password=models.CharField(max_length=32) class love(models.Model): g=models.ForeignKey(‘girl‘,null=True,on_delete=models.SET_NULL) m=models.ForeignKey(‘man‘,null=True,on_delete=models.SET_NULL) # Create your models here. HASATTR={‘1‘:man,‘0‘:girl}
SESSION_ENGINE = ‘django.contrib.sessions.backends.db‘ # 引擎(默认) SESSION_COOKIE_NAME = "sessionid" # Session的cookie保存在浏览器上时的key,即:sessionid=随机字符串(默认) SESSION_COOKIE_PATH ="/" # Session的cookie保存的路径(默认) SESSION_COOKIE_DOMAIN = None # Session的cookie保存的域名(默认) SESSION_COOKIE_SECURE = False # 是否Https传输cookie(默认) SESSION_COOKIE_HTTPONLY = True # 是否Session的cookie只支持http传输(默认) SESSION_COOKIE_AGE = 86400 # Session的cookie失效日期(2周)(默认) SESSION_EXPIRE_AT_BROWSER_CLOSE = False # 是否关闭浏览器使得Session过期(默认) SESSION_SAVE_EVERY_REQUEST = True # 是否每次请求都保存Session,默认修改之后才保存(默认)
以上是关于Django-session+CBV+ORM应用的主要内容,如果未能解决你的问题,请参考以下文章