一、介绍
二、创建UserInfo表
class UserInfo(models.Model):
name = models.CharField(max_length=32)
pwd = models.CharField(max_length=32)
from app01.models import UserInfo
UserInfo.objects.create(name='alex',pwd='123')
<UserInfo: UserInfo object (1)>
UserInfo.objects.create(name='egon',pwd='123')
<UserInfo: UserInfo object (2)>
三、设置cookie和读取cookie
def login(request):
if request.method == "GET":
return render(request,'login.html')
else:
user = request.POST.get("user")
pwd = request.POST.get("pwd")
user_obj = UserInfo.objects.filter(name=user,pwd=pwd).first()
if user_obj:
obj = HttpResponse("登陆成功")
obj.set_cookie("is_login",True)
obj.set_cookie("username",user)
print(obj)
return obj
return HttpResponse('Error!')
def index(request):
if request.COOKIES.get("is_login"):
return render(request,'index.html')
else:
return render(request, 'login.html')
留言