Django DoesNotExist - Matching Query doesn't exist












0















I'm working on a Django(1.4) and Python (2.7) project in which I have a custom model for user to get loggedin.



Here's my model:
from 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)


And here's my view:
From 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)


When I try to log in it returns an error:




(u'error is : ', DoesNotExist('User_table matching query does not exist.',))




I ahve taken a look into the database and confirm that I have the record which I'm trying to get from the view, but it still through this error.



What can be wrong here?



Help me, please!



Thanks in advance!










share|improve this question























  • I think there is an issue with your indentation. It's hard to understand your code. I think the except that seems to be at the same level than the if actually pairs with the try that seems not to have exception. Is that so?

    – Hugo Luis Villalobos Canto
    Nov 22 '18 at 3:13
















0















I'm working on a Django(1.4) and Python (2.7) project in which I have a custom model for user to get loggedin.



Here's my model:
from 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)


And here's my view:
From 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)


When I try to log in it returns an error:




(u'error is : ', DoesNotExist('User_table matching query does not exist.',))




I ahve taken a look into the database and confirm that I have the record which I'm trying to get from the view, but it still through this error.



What can be wrong here?



Help me, please!



Thanks in advance!










share|improve this question























  • I think there is an issue with your indentation. It's hard to understand your code. I think the except that seems to be at the same level than the if actually pairs with the try that seems not to have exception. Is that so?

    – Hugo Luis Villalobos Canto
    Nov 22 '18 at 3:13














0












0








0








I'm working on a Django(1.4) and Python (2.7) project in which I have a custom model for user to get loggedin.



Here's my model:
from 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)


And here's my view:
From 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)


When I try to log in it returns an error:




(u'error is : ', DoesNotExist('User_table matching query does not exist.',))




I ahve taken a look into the database and confirm that I have the record which I'm trying to get from the view, but it still through this error.



What can be wrong here?



Help me, please!



Thanks in advance!










share|improve this question














I'm working on a Django(1.4) and Python (2.7) project in which I have a custom model for user to get loggedin.



Here's my model:
from 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)


And here's my view:
From 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)


When I try to log in it returns an error:




(u'error is : ', DoesNotExist('User_table matching query does not exist.',))




I ahve taken a look into the database and confirm that I have the record which I'm trying to get from the view, but it still through this error.



What can be wrong here?



Help me, please!



Thanks in advance!







python django python-2.7 django-1.4






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 16 '18 at 2:05









Abdul RehmanAbdul Rehman

943323




943323













  • I think there is an issue with your indentation. It's hard to understand your code. I think the except that seems to be at the same level than the if actually pairs with the try that seems not to have exception. Is that so?

    – Hugo Luis Villalobos Canto
    Nov 22 '18 at 3:13



















  • I think there is an issue with your indentation. It's hard to understand your code. I think the except that seems to be at the same level than the if actually pairs with the try that seems not to have exception. Is that so?

    – Hugo Luis Villalobos Canto
    Nov 22 '18 at 3:13

















I think there is an issue with your indentation. It's hard to understand your code. I think the except that seems to be at the same level than the if actually pairs with the try that seems not to have exception. Is that so?

– Hugo Luis Villalobos Canto
Nov 22 '18 at 3:13





I think there is an issue with your indentation. It's hard to understand your code. I think the except that seems to be at the same level than the if actually pairs with the try that seems not to have exception. Is that so?

– Hugo Luis Villalobos Canto
Nov 22 '18 at 3:13












2 Answers
2






active

oldest

votes


















0














checklist:
1 not sure this kind of express is supported, i usually use someModel.objects.filter(id="asdf").filter(email="qwe@gmail.com")



2 no record matched



r u sure u can match something, the official doc says:




get() raises a DoesNotExist exception if an object wasn’t found for
the given parameters. This exception is an attribute of the model
class. Example:




django official doc



hope could help






share|improve this answer
























  • Hi @陈海栋, from where I can get the id because the user is not logged in.

    – Abdul Rehman
    Nov 16 '18 at 6:28











  • that's just an example of querying sequentially not at a same function call not sure if this work: User_table.objects.get(user_name=username, emailid=email); maybe u need : User_table.objects.filter(user_name=username).filter(emailid=email)

    – 陈海栋
    Nov 16 '18 at 10:20





















0














if you want to get the only specific record you can use Django shortcut get_object_or_404()
If you don't have specific entry in a database then Django will raise 404 exception.
If you want to explore more this topic enter link description here






share|improve this answer























    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53330445%2fdjango-doesnotexist-matching-query-doesnt-exist%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    0














    checklist:
    1 not sure this kind of express is supported, i usually use someModel.objects.filter(id="asdf").filter(email="qwe@gmail.com")



    2 no record matched



    r u sure u can match something, the official doc says:




    get() raises a DoesNotExist exception if an object wasn’t found for
    the given parameters. This exception is an attribute of the model
    class. Example:




    django official doc



    hope could help






    share|improve this answer
























    • Hi @陈海栋, from where I can get the id because the user is not logged in.

      – Abdul Rehman
      Nov 16 '18 at 6:28











    • that's just an example of querying sequentially not at a same function call not sure if this work: User_table.objects.get(user_name=username, emailid=email); maybe u need : User_table.objects.filter(user_name=username).filter(emailid=email)

      – 陈海栋
      Nov 16 '18 at 10:20


















    0














    checklist:
    1 not sure this kind of express is supported, i usually use someModel.objects.filter(id="asdf").filter(email="qwe@gmail.com")



    2 no record matched



    r u sure u can match something, the official doc says:




    get() raises a DoesNotExist exception if an object wasn’t found for
    the given parameters. This exception is an attribute of the model
    class. Example:




    django official doc



    hope could help






    share|improve this answer
























    • Hi @陈海栋, from where I can get the id because the user is not logged in.

      – Abdul Rehman
      Nov 16 '18 at 6:28











    • that's just an example of querying sequentially not at a same function call not sure if this work: User_table.objects.get(user_name=username, emailid=email); maybe u need : User_table.objects.filter(user_name=username).filter(emailid=email)

      – 陈海栋
      Nov 16 '18 at 10:20
















    0












    0








    0







    checklist:
    1 not sure this kind of express is supported, i usually use someModel.objects.filter(id="asdf").filter(email="qwe@gmail.com")



    2 no record matched



    r u sure u can match something, the official doc says:




    get() raises a DoesNotExist exception if an object wasn’t found for
    the given parameters. This exception is an attribute of the model
    class. Example:




    django official doc



    hope could help






    share|improve this answer













    checklist:
    1 not sure this kind of express is supported, i usually use someModel.objects.filter(id="asdf").filter(email="qwe@gmail.com")



    2 no record matched



    r u sure u can match something, the official doc says:




    get() raises a DoesNotExist exception if an object wasn’t found for
    the given parameters. This exception is an attribute of the model
    class. Example:




    django official doc



    hope could help







    share|improve this answer












    share|improve this answer



    share|improve this answer










    answered Nov 16 '18 at 2:24









    陈海栋陈海栋

    504




    504













    • Hi @陈海栋, from where I can get the id because the user is not logged in.

      – Abdul Rehman
      Nov 16 '18 at 6:28











    • that's just an example of querying sequentially not at a same function call not sure if this work: User_table.objects.get(user_name=username, emailid=email); maybe u need : User_table.objects.filter(user_name=username).filter(emailid=email)

      – 陈海栋
      Nov 16 '18 at 10:20





















    • Hi @陈海栋, from where I can get the id because the user is not logged in.

      – Abdul Rehman
      Nov 16 '18 at 6:28











    • that's just an example of querying sequentially not at a same function call not sure if this work: User_table.objects.get(user_name=username, emailid=email); maybe u need : User_table.objects.filter(user_name=username).filter(emailid=email)

      – 陈海栋
      Nov 16 '18 at 10:20



















    Hi @陈海栋, from where I can get the id because the user is not logged in.

    – Abdul Rehman
    Nov 16 '18 at 6:28





    Hi @陈海栋, from where I can get the id because the user is not logged in.

    – Abdul Rehman
    Nov 16 '18 at 6:28













    that's just an example of querying sequentially not at a same function call not sure if this work: User_table.objects.get(user_name=username, emailid=email); maybe u need : User_table.objects.filter(user_name=username).filter(emailid=email)

    – 陈海栋
    Nov 16 '18 at 10:20







    that's just an example of querying sequentially not at a same function call not sure if this work: User_table.objects.get(user_name=username, emailid=email); maybe u need : User_table.objects.filter(user_name=username).filter(emailid=email)

    – 陈海栋
    Nov 16 '18 at 10:20















    0














    if you want to get the only specific record you can use Django shortcut get_object_or_404()
    If you don't have specific entry in a database then Django will raise 404 exception.
    If you want to explore more this topic enter link description here






    share|improve this answer




























      0














      if you want to get the only specific record you can use Django shortcut get_object_or_404()
      If you don't have specific entry in a database then Django will raise 404 exception.
      If you want to explore more this topic enter link description here






      share|improve this answer


























        0












        0








        0







        if you want to get the only specific record you can use Django shortcut get_object_or_404()
        If you don't have specific entry in a database then Django will raise 404 exception.
        If you want to explore more this topic enter link description here






        share|improve this answer













        if you want to get the only specific record you can use Django shortcut get_object_or_404()
        If you don't have specific entry in a database then Django will raise 404 exception.
        If you want to explore more this topic enter link description here







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 16 '18 at 6:24









        Hardik GajjarHardik Gajjar

        769922




        769922






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53330445%2fdjango-doesnotexist-matching-query-doesnt-exist%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Guess what letter conforming each word

            Port of Spain

            Run scheduled task as local user group (not BUILTIN)