matching query does not exist Error in Django

Question:

I have implemented a password recovery functionality in django. With my method, the new password will be sent to the email id entered. It works fine when given the correct email (e-mail id which exists in the database). But when given an email id which is not in the database, it gives the error:
'DoesNotExist at /forgotPassword/ UniversityDetails matching query does not exist.'

How can I resolve this issue?

forgotPassword.html()

def forgotPassword(request):
    if request.POST:
        email=request.POST.get("email")
        user = UniversityDetails.objects.get(email=email)
        if(not user):
            print "No user"
            return render_to_response("forgotPassword.html")
        else:
            newPassword = user.password
            send_mail('Password Recovery', 'The password for your site is '+ newPassword, '[email protected]',
    ['[email protected]'], fail_silently=False)   
            return render_to_response("passwordRecovery.html")
    return render_to_response('forgotPassword.html')

html

<form name="forgotPassword" method="POST" id="myFormid" action="http://10.1.0.90:8080/forgotPassword/">
<div style="float:center;width:100%;color:#0000A0">
 Enter your E-mail ID</label><br/> <input type="text" name="email" size="25" /> 
 <input type="submit" value="Submit" />
 </div> 

</form >
Asked By: rv_k

||

Answers:

try:
    user = UniversityDetails.objects.get(email=email)
except UniversityDetails.DoesNotExist:
    user = None

I also see you’re storing your passwords in plaintext (a big security no-no!). Consider using the built-in auth system instead.

Answered By: Sam Dolan

I also had this problem.
It was caused by the development server not deleting the django session after a debug abort in Aptana, with subsequent database deletion.
(Meaning the id of a non-existent database record was still present in the session the next time the development server started)

To resolve this during development, I used

request.session.flush()
Answered By: Robse

In case anybody is here and the other two solutions do not make the trick, check that what you are using to filter is what you expect:

user = UniversityDetails.objects.get(email=email)

is email a str, or a None? or an int?

Answered By: Alejandro Varela

You may try this way. just use a function to get your object

def get_object(self, id):
    try:
        return UniversityDetails.objects.get(email__exact=email)
    except UniversityDetails.DoesNotExist:
        return False
Answered By: Md Mehedi Hasan

As mentioned in Django docs, when get method finds no entry or finds multiple entries, it raises an exception, this is the expected behavior:

get() raises MultipleObjectsReturned if more than one object was
found. The MultipleObjectsReturned exception is an attribute of the
model class.

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

Using exceptions is a way to handle this problem, but I actually don’t like the ugly try-except block. An alternative solution, and cleaner to me, is to use the combination of filter + first.

user = UniversityDetails.objects.filter(email=email).first()

When you do .first() to an empty queryset it returns None. This way you can have the same effect in a single line.

The only difference between catching the exception and using this method occurs when you have multiple entries, the former will raise an exception while the latter will set the first element, but as you are using get I may assume we won’t fall on this situation.

Note that first method was added on Django 1.6.

You can use this in your case, it will work fine.

user = UniversityDetails.objects.filter(email=email).first()
Answered By: vikasvmads

The problem is occurred when you try to match the email while the database is empty. So, if you want to solve this type of error try to put it into try except block. The example is –

try:
    user = UniversityDetails.objects.get(email=email)
except UniversityDetails.DoesNotExist:
    user = None
Answered By: Mehedi Abdullah