Reputation: 4983
When a user requests a password reset, he will be redirected to /password/reset/done/
which tells the user that 'We have sent you an e-mail...', but if you directly go to that page, you get the same information, which is a little bit confusing since no email's sent to anyone.
Is there a variable which can be used in password_reset_done.html
template like {{ messages }}
so that with if
conditions other content can show up for direct visitors? If not, is there a way to do this?
EDIT:
Please don't be hasty on labeling this as a duplicate question of How to use the built-in 'password_reset' view in Django?.
I do know how to show up that page /password/reset/done/
, which is basically a stage specifically used for telling a user that "we've sent you an email so that you can follow the link to reset your password...", while the question you are refering to is about how to correctly redirect a user when password reset is successful.
What I'm looking for is a way to either rediret it to another page or show some different stuff when a user is trying to directly surf that page(well people do that sometimes) which is supposed to be viewed after he successfully submitted a password reset request. I need some kind of hook to make it work.
Upvotes: 2
Views: 221
Reputation: 4983
After checking into the codes in /account/views.py
, I realized a message need to be added in previous /password/reset/
page so that it can be used in the next page which is /password/reset/done/
.
Here's an example:
Add this in the form_valid
method of class PasswordResetView
:
get_adapter().add_message(self.request,
messages.INFO,
'account/messages/'
'password_reset_mail_sent.txt',
{'email': form.cleaned_data["email"]})
Now I can use {{ email }}
in 'account/messages/password_reset_mail_sent.txt'
, and with {% if messages %}
in password_reset_done.html
I can serve direct visitors a different content.
Upvotes: 1
Reputation: 10609
You can use Django Messages Framework to notify the user with success or fail:
from django.contrib import messages
def my_view(request):
# your view logic goes here
if sucess_condition:
messages.success(request, 'Action accomplished with success.')
else:
messages.error(request, 'Action Failed.')
Check Messages Framework documentation for more details and customization.
Upvotes: 0