Django DidNotExist存在-匹配查询不存在

时间:2018-11-16 02:05:10

标签: python django python-2.7 django-1.4

我正在Django(1.4)和Python(2.7)项目中工作,在该项目中,我有一个自定义模型供用户登录。

这是我的模特: 来自models.py:

class User_table(models.Model):
    first_name = models.CharField(max_length=100)
    last_name = models.CharField(max_length=100)
    address = models.CharField(max_length=300)
    phone = models.CharField(max_length=20, null=True)
    emailid = models.EmailField()
    user_name = models.CharField(max_length=100)
    password = models.TextField()
    user_type = models.CharField(max_length=20)
    # True means drive is available for delivery
    status = models.BooleanField(default=True)
    vehicle_no = models.CharField(max_length=50, blank=True, null=True)
    gender = models.CharField(max_length=10)
    profile = models.ForeignKey(AddedFile, on_delete=models.CASCADE, null=True, related_name='profile')
    uploaded_document = models.ForeignKey(AddedFile, on_delete=models.CASCADE, null=True, related_name='document')
    approval_status = models.BooleanField(default=False)
    login_try = models.IntegerField(default=0)
    is_active = models.BooleanField(default=False)
    created_at = models.DateTimeField(auto_now_add=True)
    updated_at = models.DateTimeField(auto_now=True)

    def __str__(self):
        return str(self.id)

这是我的观点: 来自views.py:

def mainlogin(request):
    if request.method == "POST":
        username = request.POST['username']
        email = request.POST['email']
        password = request.POST['password']
        try:
            obj = User_table.objects.get(user_name=username, emailid=email)
        verify_password = ''
        try:
            verify_password = handler.verify(password, obj.password)
        except Exception as e:
            print(e)
        if verify_password is True:
            request.session['user_id'] = obj.id
            request.session['user_type'] = obj.user_type
            user_name = obj.first_name + ' ' + obj.last_name
            request.session['user_name'] = user_name
            if not obj.approval_status:
                return HttpResponse('Your account is not confirmed by administration.')
            obj.is_active = True
            obj.login_try = 0
            obj.save()
            return redirect(home)
        else:
            try:
                obj = User_table.objects.get(user_name=username, emailid=email);
                if obj:
                    s = obj.login_try
                    s = s + 1
                    obj.login_try = int(s)

                    if int(obj.login_try) >= 3:
                        obj.login_try = 3
                    obj.save()
                    if int(obj.login_try) == 3:
                        id = obj.id
                        key = get_random_string(length=10)
                        reset_link = 'It seems you forgot password or someone is trying to login you account. This is your password reset link please do not share this with other ' + settings.EMAIL_URL + 'reset_password/' + str(
                            id) + ' key is : ' + str(key)
                        send_mail('Reset link', reset_link, settings.EMAIL_HOST_USER, [obj.emailid, ])
                        obj.password = str(key)
                        obj.save()
                        return HttpResponse(
                            'It seems you forgot password or someone is trying to login you account.  Password Reset link has been sent to your email id')
            except Exception as e:
                print(e)
                pass
            return redirect(mainlogin)
    except Exception as e:
        print('error is  : ', e)
        return HttpResponse('An error has occurred.')
if request.method == "GET":
    try:
        return render(request, "login.html")
    except Exception as e:
        print(e)

当我尝试登录时返回错误:

  

(u'错误为:',DidNotExist('User_table匹配查询不存在',))

我已经查看了数据库,并确认我具有试图从视图中获取的记录,但是仍然可以通过此错误进行操作。

这有什么问题吗?

请帮帮我!

谢谢!

2 个答案:

答案 0 :(得分:0)

清单: 1不确定是否支持这种表达方式,我通常使用someModel.objects.filter(id="asdf").filter(email="qwe@gmail.com")

2没有匹配的记录

您确定您可以匹配某些东西,官方文档说:

  如果找不到用于

get()的对象,则会引发DidNotExist异常   给定的参数。此异常是模型的属性   类。示例:

django official doc

希望可以帮助

答案 1 :(得分:0)

如果要获取唯一的特定记录,可以使用Django快捷键get_object_or_404() 如果您在数据库中没有特定的条目,那么Django将引发404异常。 如果您想进一步了解本主题enter link description here