Django is the popular web development framework of python. As per the record of github django users in July 2019 is 274,837 and as per the s...
Django is the popular web development framework of python. As per the record of github django users in July 2019 is 274,837 and as per the stackoverflow 202,768 questions asked related to django and one of them is about error on password reset email while deployed in pythonanywhere.
Well you can use either gmail smtp server or sendgrip for pythonanywhere for password reset with email.
EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
EMAIL_HOST = 'smtp.gmail.com'
EMAIL_USE_TLS = True
EMAIL_PORT = 587
EMAIL_HOST_USER = 'your_gmail'
EMAIL_HOST_PASSWORD = 'your_password'
Above is for gmail SMTP server and below one is for sendgrid.
SENDGRID_API_KEY = os.getenv('Enter your api key generate after login')
EMAIL_HOST = 'smtp.sendgrid.net'
EMAIL_HOST_USER = 'apikey'
EMAIL_HOST_PASSWORD = SENDGRID_API_KEY
EMAIL_PORT = 587
EMAIL_USE_TLS = True
Process for password reset by sending email in django
- Setting
- Templates
- Views
- Urls
Setting
Add above sendgrid code into your setting. For this visit sendgrid link and register then use smtp service. It ask you to create api key, for that write your api key name and click on generate api key then then key is provided to you.
Templates
You have to make four html and one txt file. These templates are used to render the forms and message related password reset and a text file contain the email subject text.
For password reset form make password_reset_form.html
{% extends '_base.html' %}
{% block title %}Password Reset forum{% endblock %}
{% block content %}
<div class="row">
<aside class="col-sm-4">
</aside> <!-- col.// -->
<aside class="col-sm-4">
<h2>Forget Password</h2>
<div class="card">
<article class="card-body">
<form method="post">
{% csrf_token %}
{% for field in form %}
{{ field.label_tag }}<br>
{{ field }}
{% for error in field.errors %}
<p style="color: red">{{ error }}</p>
{% endfor %}
{% endfor %}
<button type="submit">Submit</button>
</form>
</article>
</div>
</aside>
<aside class="col-sm-4">
</aside> <!-- col.// -->
</div>
{% endblock %}
For sending email make password_reset_email.html In this file change the url “custom_auth:password_reset_confirm” as per your project url.
{% autoescape off %}
Yes we all have problem of forgetting password but dont worry {{ user.get_username }}, We have solution of it. You can reset your password.
click the link below to reset your password:
{{ protocol }}://{{ domain }}{% url 'custom_auth: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 and set your new password.Have Fun!!!
Sincerely,
The Jagir KhulyoTeam
{% endautoescape %}
For subject of the email make password_reset_subject.txt Inside it write email subject like “Password Reset”
Then make password_reset_done.html which display reset done message
{% extends '_base.html' %}
{% block content %}
<p>
Hi, we email the password reset link to your email address, visit your mail once and follow the instruction.
You should receive them shortly.
</p>
<p>
If you don't receive an email, please make sure you've entered the address you registered with,
and check your spam folder.
</p>
{% endblock %}
Make password_reset_confirm.html too. This is used to set new password.
{% extends '_base.html' %}
{% block title %}Change password{% endblock %}
{% block content %}
<div class="row">
<aside class="col-sm-4">
</aside> <!-- col.// -->
<aside class="col-sm-4">
<h2>Change Password</h2>
<div class="card">
{% if validlink %}
<article class="card-body">
<form method="post">
{% csrf_token %}
{% for field in form %}
<p>
{{ field.label_tag }}<br>
{{ field }}
{% for error in field.errors %}
<p style="color: red">{{ error }}</p>
{% endfor %}
</p>
{% endfor %}
<button type="submit">Submit</button>
</form>
</article>
{% else %}
<article class="card-body">
<p>
The password reset link was invalid, possibly because it has already been used.
Please request a new password reset.
</p>
</article>
{% endif %}
</div>
</aside>
<aside class="col-sm-4">
</aside> <!-- col.// -->
</div>
{% endblock %}
View
We are going to implement class base view to reset password and we have to make three view class.
For password_reset view
class UserPasswordResetView(PasswordResetView):
template_name = 'password_reset_form.html'
success_url = reverse_lazy('custom_auth:password_reset_done')
subject_template_name = 'password_reset_subject.txt'
email_template_name = 'password_reset_email.html'
For displaying reset done message
class UserPasswordResetDoneView(PasswordResetDoneView):
template_name = 'password_reset_done.html'
For set new password and confirm
class UserPasswordResetConfirmView(PasswordResetConfirmView):
template_name = 'password_reset_confirm.html'
success_url = reverse_lazy('home:index')
form_valid_message = "Your password was changed!"
Urls
These urls must set in urls.py for communication between django view and templates. It defines where to go while clicking the links. Note do not change the <uidb64> if changed may generate error.
path('password_reset', UserPasswordResetView.as_view(),name='password_reset'),
path('password_reset/done', UserPasswordResetDoneView.as_view(), name='password_reset_done'),
path('reset/<uidb64>/<token>', UserPasswordResetConfirmView.as_view(), name='password_reset_confirm'),
After completing all these steps and process now push the code into git and pull in pythonanywhere via its bash console. Here you go run your site and reset you password hyy hyy enter correct email address otherwise you won’t receive email. 😉 😉 😉
Have fun!!!
COMMENTS