Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

matching query does not exist Error in Django

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 > 
like image 824
rv_k Avatar asked Apr 01 '11 03:04

rv_k


People also ask

Does not exist error Django?

The DoesNotExist exception is raised when an object is not found for the given parameters of a query. Django provides a DoesNotExist exception as an attribute of each model class to identify the class of object that could not be found and to allow you to catch a particular model class with try/except.

Which can be used to retrieve an object directly instead of a Queryset?

Retrieving Single Objects from QuerySets We can do this using the get() method. The get() returns the single object directly. Let's see the following example. As we can see in both examples, we get the single object not a queryset of a single object.


1 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.

like image 176
Sam Dolan Avatar answered Sep 17 '22 03:09

Sam Dolan