link to django password reset not displaying - django

the link to password reset is not showing
console output of password reset
Hi there, Someone asked for a password reset for the email
address root#gmail.com, Follow the link below: http://127.0.0.1:8000 {% url
'password_reset_confirm' uidb64=uid token=token %}
HTML FILES
password_reset.html
<form action="" method="POST">
{% csrf_token %}
{{ form.as_p }}
<input type="submit" value="send password reset email" name="submit" />
</form>
password_reset_email.html
Hi there, Someone asked for a password reset for the email
address {{ email }}, Follow the link below: {{ protocol }}://
{{ domain}} {% url'password_reset_confirm' uidb64=uid token=token %}
password_reset_confirm.html
<div>
{% if validlink %}
<h2>change password for 0{{ form.user.username }}</h2>
<form action="" method="POST" novalidate>
{% csrf_token %}
{{ form.as_p }}
<input type="submit" value="changepassword" name="submit" />
</form>
{% else %}
<h3>Reset your password</h3>
<p>it looks like you clicked on an invalid passowrd reset link try again</p>
Request
{% endif %}
</div>
settings.py
EMAIL_BACKEND = "django.core.mail.backends.console.EmailBackend"
acoounts/urls.py
path("accounts/", include("django.contrib.auth.urls")),

Please check the following things.
{{ protocol }}://{{ domain}} {% url'password_reset_confirm' uidb64=uid token=token %}
Remove the space between {{ domain}} {% url''}
Check that your email template is getting value for protocol and domain
Inspect in the email and check what is the final link is.

I faced the same problem, all I had to do was put the code on the same line.
{{ protocol }}://{{ domain }}{% url "password_reset_confirm" uidb64=uid token=token %}
Make sure the code is not broken into a new line.

http://127.0.0.1:8000/accounts/password_reset/
Try this
you just have to add the URL which is link to login .

Related

Placement of {% if form.errors %} on login template

I'm not sure if I'm allowed to ask these types of questions here since it's not a problem per say, so please let me know. But I was wondering for the login.html template on Django:
{% extends 'learning_logs/base.html' %}
{% block content %}
{% if form.errors %}
<p>Your username and password didn't match. Please try again.</p>
{% endif %}
<form method = "post" action="{% url 'login' %}">
{% csrf_token %}
{{ form.as_p }}
<button name = "submit">log in</button>
<input type="hidden" name="next" value="{% url 'learning_logs:index' %}" />
</form>
{% endblock content %}
How come it checks for the form.errors before the it even processes the form? Thanks for any help on this question.
The template ordering doesn't define how the form logic works, only how certain items are displayed. You could put the {% if form.errors %} block after the <form> and there would be no difference to how the form is processed by Django. Furthermore, form.errors will only exist once a form has been validated with is_valid() and any errors exist.

CSRF verification failed. Request aborted. Even with {% crsf_token %}

I'm getting CSRF verification failed. Request aborted. after I try signing in, even though I've provided the {% csrf_token %} template tag.
templates/registration/login.html
{% extends "base.html" %}
{% block content %}
<div class="container">
<div class="card">
{% if form.errors %}
<p>Your username and password didn't match. Please try again.</p>
{% endif %}
{% if next %}
{% if user.is_authenticated %}
<p>Your account doesn't have access to this page. To proceed,
please login with an account that has access.</p>
{% else %}
<p>Please login to see this page.</p>
{% endif %}
{% endif %}
<form method="post" action="{% url 'login' %}">
{% csrf_token %}
{{ form|crispy }}
<button type="submit" class="btn btn-success"><i class="fas fa-sign-in-alt"></i> Login</button>
<input type="hidden" name="next" value="{{ next }}" />
</form>
{# Assumes you setup the password_reset view in your URLconf #}
<p>Lost password?</p>
</div>
</div>
{% endblock content %}

django password change completion fails

I have almost implemented the password reset process, but when I validate the password change I don't end up on the reset complete template and get the template says:
The password reset link was invalid...
However, the password change has been made and can log in with the new password. It looks the redirection is wrong but I can't figure out why.
Here is my urls.py:
path('reset/<uidb64>/<token>', auth_views.PasswordResetConfirmView.as_view(
template_name='news/user/password_reset_confirm.html',
success_url='../password-change/done'), name='password_reset_confirm'),
path('password-change/done', auth_views.PasswordResetCompleteView.as_view(template_name='news/user/password_reset_complete.html'), name='password_change_done'),
Here is my template:
{% block content %}
{% if validlink %}
<p>Please enter your new password twice so we can verify you typed it in correctly.</p>
<form method="post">{% csrf_token %}
<fieldset class="module aligned">
<div class="form-row field-password1">
{{ form.new_password1.errors }}
<label for="id_new_password1">New password:</label>
{{ form.new_password1 }}
</div>
<div class="form-row field-password2">
{{ form.new_password2.errors }}
<label for="id_new_password2">Confirm password:</label>
{{ form.new_password2 }}
</div>
<input type="submit" value="Change my password">
</fieldset>
</form>
{% else %}
<p>The password reset link was invalid, possibly because it has already been used. Please request a new password reset.</p>
{% endif %}
{% endblock %}
You're missing a final slash on the password_reset_confirm path, which is causing your POST to go to the wrong place. It should be:
path('reset/<uidb64>/<token>/', ...
Note that your success_url is also probably wrong, I doubt that .. will work the way you think it will.

How to handle errors when using PasswordResetConfirmView

I am using django.auth.contrib as my session based authentication framework.
I have successfully implemented a reset password feature via PasswordResetConfirmView which requires a valid token sent to a user's email to set a new password.
path('password_reset/confirm/<uidb64>/<token>/',
auth_views.PasswordResetConfirmView.as_view(
template_name='accounts/password_reset/password_reset_confirm.html'),
name='password_reset_confirm'),
This works fine if the token is valid. However, if the token is not valid (e.g. incorrect or expired), then I predictably get an error:
Reverse for 'password_reset_confirm' with arguments '('', '')' not
found. 1 pattern(s) tried:
['accounts/password_reset/confirm/(?P[^/]+)/(?P[^/]+)/$']
What's the best practice to handle this exception for the user? Ideally I want to catch it and intercept it with "Sorry, your token is invalid." Is there an existing way to do this within the django.contrib.auth framework? Looking for the best DRY approach.
Edit I am already using the validlink context in the template. The error appears to be prior to this:
{% block content %}
<h1>{{ title }}</h1>
{% if validlink %}
<p>Please enter your new password twice so we can verify you typed it in correctly.</p>
<form method="post">{% csrf_token %}
<fieldset>
<div>
{{ form.new_password1.errors }}
<label for="id_new_password1">New password:</label>
{{ form.new_password1 }}
</div>
<div>
{{ form.new_password2.errors }}
<label for="id_new_password2">Confirm password:</label>
{{ form.new_password2 }}
</div>
<input type="submit" value="Change my password">
</fieldset>
</form>
{% else %}
<p>The password reset link was invalid, possibly because it has already been used. Please request a new password reset.</p>
{% endif %}
{% endblock %}

NoReverseMatch on django generated views

I'm trying to create the views for all the authentication process in Django, but I have an issue with the reverse url tag in the views.
I have :
urls.py
url('^v1/back/', include('django.contrib.auth.urls'))
login.html
{% extends "base.html" %}
{% load i18n %}
{% block content %}
<form method="post" action=".">
{{ form.as_p }}
<input type="submit" value="{% trans 'Log in' %}" />
<input type="hidden" name="next" value="{{ next }}" />
</form>
<p>{% trans "Forgot password" %}? {% trans "Reset it" %}!</p>
<p>{% trans "Not member" %}? {% trans "Register" %}!</p>
{% endblock %}
And when I access localhost:8000/v1/back/login/ i have:
NoReverseMatch at /v1/back/login/
Reverse for 'django.contrib.auth.views.auth_password_reset' with
arguments '()' and keyword arguments '{}' not found. 0 pattern(s)
tried: []
The thing is, as, you can see, i try putting the complete path, it still does not work.
When I try to access another view like password change, it redirects me to :
http://192.168.56.103:8000/accounts/login/?next=/v1/back/password_change/
Which obviously does not work, and when i access password reset :
{% extends "base.html" %}
{% load i18n %}
{% block content %}
<form method="post" action=".">
{{ form.as_p }}
<input type="submit" value="{% trans 'Submit' %}" />
</form>
{% endblock %}
It has the django admin look and feel instead on my base.html.
I'm guessing there are lots of problem here, I'm trying to solve them one by one, but i don't know what i've done wrong on the url part.
Don't know if this is relevant, but a part of my application is served by Django Rest Framework, and i'm trying to put in place oAuth2.
The correct name for password reset is just password_reset, to reverse it use:
{% url 'password_reset' %}
To fix the login redirect, you have to adjust the LOGIN_URL in your settings.py
I suggest you remove the part regarding the template and post it as a separate question.