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!
checklist: 1 not sure this kind of express is supported, i usually use
someModel.objects.filter(id="asdf").filter(email="[email protected]")2 no record matched
Are you sure something should match? The docs say:
django official doc