DEV Community

Cover image for Django - Reset Password

Django - Reset Password

Hana Belay on September 03, 2021

Hello everyone! In addition to login and logout views, django's auth app has views that can let users reset their password if they forget it. Let'...
Collapse
 
ezhilan3 profile image
EZHILAN • Edited

Hi, Thanks for this tutorial. I'm having some trouble receiving password reset email. I didn't get any error message in my console. I tested my SMTP server online and it works fine.

I'm getting the following output:
[05/Nov/2022 20:49:36] "GET /pr/ HTTP/1.1" 200 3657
[05/Nov/2022 20:49:39] "POST /pr/ HTTP/1.1" 302 0
[05/Nov/2022 20:49:39] "GET /pr/ HTTP/1.1" 200 4128

But didn't receive any mail.

Collapse
 
earthcomfy profile image
Hana Belay

Hi, thanks for the feedback.

Did you check your spam folder?

Collapse
 
ezhilan3 profile image
EZHILAN

Yes. It's not even there.

Collapse
 
dave_wyers_nz profile image
Dave Wyers

Hi Hana

You may have mentioned this is your other posts but I thought I would highlight it.

As we are extending existing system templates the order of the INSTALLED_APPS in the settings.py file matters.

Django will look for the first View, Form, or Template that matches, so your new template must come before the default version.

For example the Users app comes before the django.contrib.admin & django.contrib.auth apps, as below.

INSTALLED_APPS = [
# Project Apps
"Users",

# Default Apps
"django.contrib.admin",
"django.contrib.auth",
Enter fullscreen mode Exit fullscreen mode
Collapse
 
earthcomfy profile image
Hana Belay

Thank you for the comment. This section of the doc explains it clearly docs.djangoproject.com/en/4.1/ref/...

Collapse
 
dehghan profile image
mehdi

Hi, Thank you for this tutorial. I have the following error for reset password. Would you please help me:

SMTPAuthenticationError at /password-reset/
(535, b'5.7.8 Username and Password not accepted. Learn more at\n5.7.8 support.google.com/mail/?p=BadCred... u6-20020a056870304600b000eba1ab2e02sm4002395oau.18 - gsmtp')

Collapse
 
earthcomfy profile image
Hana Belay

Hi, Thanks for reading.

Check if you have turned on less secure apps on your Google account myaccount.google.com/lesssecureapps

and also make sure you have put the correct credentials in your settings.

Collapse
 
dehghan profile image
mehdi

I have turned on the less secure on my google account, so I'm sure about it. However, I didn't change anything inside the setting. I just want to run your app. Should I change something in the setting part in the following commands?

EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
EMAIL_HOST = 'smtp.gmail.com'
EMAIL_USE_TLS = True
EMAIL_PORT = 587
EMAIL_HOST_USER = str(os.getenv('EMAIL_USER'))
EMAIL_HOST_PASSWORD = str(os.getenv('EMAIL_PASSWORD'))

Thread Thread
 
earthcomfy profile image
Hana Belay

You have to include your email credentials in your environment variable.

Collapse
 
jarmas profile image
Javier Armas

Hi, thanks for the tutorial, I've followed the previous ones and helped me a lot!
I want to specify some things:
1) I think you've an error in the password_reset_subject template, you created it with a .txt extension instead of .html. Or is there something I'm misunderstanding?
2) It would be awesome if you can create a tutorial about Custom User models, how to create them, how to use them, how to use email instead of username for authentication. I've had a lot of problems with that, I'm new in Django and still struggling.
3) Can you tell me please how did you learn Python+Django? I mean, resources, guidelines and topic plan or something. I used Free code camp tutorials, also a book called Django for Beginners, but I still feel that I just know a litttle bit, there are a lot of things that I've never read/heard about and I don't know if it is part of the process of learning or what. Anyway it feels frustrating.
Thanks again

Collapse
 
earthcomfy profile image
Hana Belay

Hey, thanks for the feedback. Glad I could help.

  1. It's not an error. The subject of the email can be created with a .txt extension as mentioned here in the doc

  2. I'll try to write about those topics. If you want a demo of authentication using email instead of username, you can find here. You can also contact me about the problems you have faced.

  3. Don't get frustrated. I know the feeling :) What worked for me is, once you know the basics, move to doing projects on your own without following a tutorial on youtube. Instead use Google, docs, StackOverflow and blogs to solve a problem during your project. Good luck!

Collapse
 
jarmas profile image
Javier Armas

Thanks again, I will take your tips into account

Collapse
 
andrewsrea profile image
Andrew Rea • Edited

Hello! I don't think it's possible to send email to users through a Google account anymore. I saw this warning message from Google as I tried to set up an app password: "To help keep your account secure, from May 30, 2022, ​​Google no longer supports the use of third-party apps or devices which ask you to sign in to your Google Account using only your username and password."

Collapse
 
earthcomfy profile image
Hana Belay • Edited

Hey Andrew, Thanks for the feedback and you are right. I will update my article.

Collapse
 
mohanrajlearn profile image
Mohanraj

I am getting below error: Environment:

Request Method: POST
Request URL: 127.0.0.1:8000/password_reset/

Django Version: 4.1.13
Python Version: 3.10.12
Installed Applications:
['django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'upload_file_app',
'tailwind',
'theme',
'django_browser_reload']
Installed Middleware:
['django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
'django_browser_reload.middleware.BrowserReloadMiddleware']

Traceback (most recent call last):
File "/home/mohanrajjj/.local/lib/python3.10/site-packages/djongo/cursor.py", line 51, in execute
self.result = Query(
File "/home/mohanrajjj/.local/lib/python3.10/site-packages/djongo/sql2mongo/query.py", line 783, in init
self.query = self.parse()
File "/home/mohanrajjj/.local/lib/python3.10/site-packages/djongo/sql2mongo/query.py", line 875, in parse
raise e
File "/home/mohanrajjj/.local/lib/python3.10/site-packages/djongo/sql2mongo/query.py", line 856, in parse
return handler(self, statement)
File "/home/mohanrajjj/.local/lib/python3.10/site-packages/djongo/sql2mongo/query.py", line 932, in _select
return SelectQuery(self.db, self.connection_properties, sm, self._params)
File "/home/mohanrajjj/.local/lib/python3.10/site-packages/djongo/sql2mongo/query.py", line 116, in __init
_
super().init(*args)
File "/home/mohanrajjj/.local/lib/python3.10/site-packages/djongo/sql2mongo/query.py", line 62, in init
self.parse()
File "/home/mohanrajjj/.local/lib/python3.10/site-packages/djongo/sql2mongo/query.py", line 152, in parse
self.where = WhereConverter(self, statement)
File "/home/mohanrajjj/.local/lib/python3.10/site-packages/djongo/sql2mongo/converters.py", line 27, in init
self.parse()
File "/home/mohanrajjj/.local/lib/python3.10/site-packages/djongo/sql2mongo/converters.py", line 119, in parse
self.op = WhereOp(
File "/home/mohanrajjj/.local/lib/python3.10/site-packages/djongo/sql2mongo/operators.py", line 476, in init
self.evaluate()
File "/home/mohanrajjj/.local/lib/python3.10/site-packages/djongo/sql2mongo/operators.py", line 465, in evaluate
op.evaluate()
File "/home/mohanrajjj/.local/lib/python3.10/site-packages/djongo/sql2mongo/operators.py", line 465, in evaluate
op.evaluate()
File "/home/mohanrajjj/.local/lib/python3.10/site-packages/djongo/sql2mongo/operators.py", line 279, in evaluate
raise SQLDecodeError

The above exception (

Keyword: None
Sub SQL: None
FAILED SQL: ('SELECT "auth_user"."id", "auth_user"."password", "auth_user"."last_login", "auth_user"."is_superuser", "auth_user"."username", "auth_user"."first_name", "auth_user"."last_name", "auth_user"."email", "auth_user"."is_staff", "auth_user"."is_active", "auth_user"."date_joined" FROM "auth_user" WHERE ("auth_user"."email" iLIKE %(0)s AND "auth_user"."is_active")',)
Params: (('mohanrajjj.learn@gmail.com',),)
Version: 1.3.4) was the direct cause of the following exception:
Enter fullscreen mode Exit fullscreen mode

File "/home/mohanrajjj/.local/lib/python3.10/site-packages/django/db/backends/utils.py", line 89, in _execute
return self.cursor.execute(sql, params)
File "/home/mohanrajjj/.local/lib/python3.10/site-packages/djongo/cursor.py", line 59, in execute
raise db_exe from e

The above exception () was the direct cause of the following exception:
File "/home/mohanrajjj/.local/lib/python3.10/site-packages/django/core/handlers/exception.py", line 56, in inner
response = get_response(request)
File "/home/mohanrajjj/.local/lib/python3.10/site-packages/django/core/handlers/base.py", line 197, in get_response
response = wrapped_callback(request, callback_args, **callback_kwargs)
File "/home/mohanrajjj/.local/lib/python3.10/site-packages/django/views/generic/base.py", line 103, in view
return self.dispatch(request, *args, **kwargs)
File "/home/mohanrajjj/.local/lib/python3.10/site-packages/django/utils/decorators.py", line 46, in _wrapper
return bound_method(*args, **kwargs)
File "/home/mohanrajjj/.local/lib/python3.10/site-packages/django/utils/decorators.py", line 134, in _wrapped_view
response = view_func(request, *args, **kwargs)
File "/home/mohanrajjj/.local/lib/python3.10/site-packages/django/contrib/auth/views.py", line 242, in dispatch
return super().dispatch(*args, **kwargs)
File "/home/mohanrajjj/.local/lib/python3.10/site-packages/django/views/generic/base.py", line 142, in dispatch
return handler(request, *args, **kwargs)
File "/home/mohanrajjj/.local/lib/python3.10/site-packages/django/views/generic/edit.py", line 153, in post
return self.form_valid(form)
File "/home/mohanrajjj/.local/lib/python3.10/site-packages/django/contrib/messages/views.py", line 12, in form_valid
response = super().form_valid(form)
File "/home/mohanrajjj/.local/lib/python3.10/site-packages/django/contrib/auth/views.py", line 255, in form_valid
form.save(
*opts)
File "/home/mohanrajjj/.local/lib/python3.10/site-packages/django/contrib/auth/forms.py", line 339, in save
for user in self.get_users(email):
File "/home/mohanrajjj/.local/lib/python3.10/site-packages/django/contrib/auth/forms.py", line 308, in get_users
return (
File "/home/mohanrajjj/.local/lib/python3.10/site-packages/django/db/models/query.py", line 394, in __iter
_
self.fetch_all()
File "/home/mohanrajjj/.local/lib/python3.10/site-packages/django/db/models/query.py", line 1867, in _fetch_all
self._result_cache = list(self._iterable_class(self))
File "/home/mohanrajjj/.local/lib/python3.10/site-packages/django/db/models/query.py", line 87, in __iter
_
results = compiler.execute_sql(
File "/home/mohanrajjj/.local/lib/python3.10/site-packages/django/db/models/sql/compiler.py", line 1398, in execute_sql
cursor.execute(sql, params)
File "/home/mohanrajjj/.local/lib/python3.10/site-packages/django/db/backends/utils.py", line 102, in execute
return super().execute(sql, params)
File "/home/mohanrajjj/.local/lib/python3.10/site-packages/django/db/backends/utils.py", line 67, in execute
return self.execute_with_wrappers(
File "/home/mohanrajjj/.local/lib/python3.10/site-packages/django/db/backends/utils.py", line 80, in _execute_with_wrappers
return executor(sql, params, many, context)
File "/home/mohanrajjj/.local/lib/python3.10/site-packages/django/db/backends/utils.py", line 84, in _execute
with self.db.wrap_database_errors:
File "/home/mohanrajjj/.local/lib/python3.10/site-packages/django/db/utils.py", line 91, in __exit
_
raise dj_exc_value.with_traceback(traceback) from exc_value
File "/home/mohanrajjj/.local/lib/python3.10/site-packages/django/db/backends/utils.py", line 89, in _execute
return self.cursor.execute(sql, params)
File "/home/mohanrajjj/.local/lib/python3.10/site-packages/djongo/cursor.py", line 59, in execute
raise db_exe from e

Exception Type: DatabaseError at /password_reset/
Exception Value:

Collapse
 
oriohac profile image
Orioha Chikezirim • Edited

I encountered this error:
Exception Type: TemplateDoesNotExist
Exception Value: password_reset_email.html
and from your article there was no template set for 'password_reset_email.html', any assistance will be appreciated. @earthcomfy

Collapse
 
earthcomfy profile image
Hana Belay • Edited

Hi. password_reset_email.html file already exists in the article. I think you missed it. Here it is:

password_reset_email.html

{% autoescape off %}
  To initiate the password reset process for your {{ user.email }} Django Registration/Login App Account,
  click the link below:

  {{ protocol }}://{{ domain }}{% url 'password_reset_confirm' uidb64=uid token=token %}

  If clicking the link above doesn't work, please copy and paste the URL in a new browser
  window instead.

  Sincerely,
  The Developer
{% endautoescape %}
Enter fullscreen mode Exit fullscreen mode

If you encounter any issues throughout the tutorial, you can check out the code in the GitHub repository

Collapse
 
oriohac profile image
Orioha Chikezirim

what I mean is your urls.py but I've sorted that out.

Collapse
 
arielb506 profile image
Ariel Barboza

Its weird, I cannot get Django to use the templates im passing on the class view, I can do it for log in and password change, but not reset password

Collapse
 
earthcomfy profile image
Hana Belay

Hi. Can I see your code and the error you are getting?

Collapse
 
arielb506 profile image
Ariel Barboza

I actually was able to make it show up with a bit of a different approach, it did not like the templates being called 'registration/...' that was an oversight

Collapse
 
abdiomari profile image
Abdi Omari

I am not sure what the instructions on google mean that i should replace my password with the 16 digit code i generated
If this means using it on my environment variable then i have done it but I am not getting the email to recover password
the app loads nicely with no errors
can i get more guidance on how to set up the email generation

Collapse
 
ranjeetsinh profile image
Ranjeetsinh Jagdale • Edited

I am trying to implement the same but for some reason when I enter the password-reset url, it is asking me to login and the whole process won't happen untill and unless I login.

Collapse
 
themiguelgt profile image
TheMiguelGt

because dont redirect password_reset_complete???

Image description