django框架The view account.views.register didn't return an HttpResponse object.错误的解决办法

原因:用django实现用户注册功能的view.py

def register(request):
    # 判断post/get
    print(request.method)
    if request.method == 'POST':
        new_user = UserInfo()
        new_user.uname = request.POST.get('username')
        try:
            olduser = UserInfo.objects.filter(uname=new_user.uname)
            if len(olduser) >0:
                return render(request,'register.html',{'message':'用户名已存在'})
        except ObjectDoesNotExist as e:
            logging.warning(e)
        if request.POST.get('upwd') != request.POST.get('cpwd'):
            return render(request,'register.html',{'message':'两次密码不一致'})
        new_user.upassword = make_password(request.POST.get('upwd'),'python','pbkdf_sha1')
        new_user.email = request.POST.get('email')
        new_user.phone = request.POST.get('phone')
        try:
            new_user.save()
        except ObjectDoesNotExist as e:
            logging.warning(e)
        return render(request,'login.html')

原因:对提交方法不是POST的方法没有添加分支进行判断

解决方法:添加else分支处理如下

def register(request):
    # 判断post/get
    print(request.method)
    if request.method == 'POST':
        new_user = UserInfo()
        new_user.uname = request.POST.get('username')
        try:
            olduser = UserInfo.objects.filter(uname=new_user.uname)
            if len(olduser) >0:
                return render(request,'register.html',{'message':'用户名已存在'})
        except ObjectDoesNotExist as e:
            logging.warning(e)
        if request.POST.get('upwd') != request.POST.get('cpwd'):
            return render(request,'register.html',{'message':'两次密码不一致'})
        new_user.upassword = make_password(request.POST.get('upwd'),'python','pbkdf_sha1')
        new_user.email = request.POST.get('email')
        new_user.phone = request.POST.get('phone')
        try:
            new_user.save()
        except ObjectDoesNotExist as e:
            logging.warning(e)
        return render(request,'login.html')
    else:
        return render(request, 'register.html')

 

你可能感兴趣的:(Python)