djago实现登录功能

######实现登录功能######

#templates下添加登录html模板,form表单中记得添加method和action
#表单中对应input添加name

#url中进行配置

url(r'^login$',views.LoginView.as_view(),name='login'),

#建议严格匹配login

#views.py中添加LoginView

#导入authenticate实现验证账密码功能
from django.contrib.auth import authenticate
#创建LoginView继承View
class LoginView(View):
#设置get方法
    def get(self,request):
        return render(request,'login.html')
#设置post方法
    def post(self,request):
        #todo 获取前端传递过来的用户名和密码
        username = request.POST.get('username')
        pwd = request.POST.get('pwd')
        #todo 进行数据校验
        if not all([username,pwd]):
            return HttpResponse('数据输入不完整')
        #todo 验证用户名和密码是否正确
        user = authenticate(username=username,password=pwd)

        #todo 判断是否记录用户名
        if user is not None:
            return HttpResponse('提交用户名成功')
        else:
            return HttpResponse('用户名或密码错误')

猜你喜欢

转载自blog.csdn.net/weixin_43544660/article/details/84885570