Reputation: 2780
I'm setting up a website with a fairly basic users system where you can do the usual stuff like login, change your password etc. I'm using an example Django project and changing it a bit here and there.
When the user is logged in I am able to access the user's email address and put it into the page by adding this to the template {{ request.user.email }}
.
But in a link emailed to user to reset their password where the user is not logged in the same thing doesn't work. I'm guessing it must be possible to access it somehow though.
This is the class view that I'm using:
class RestorePasswordConfirmView(PasswordResetConfirmView):
template_name = 'accounts/restore_password_confirm.html'
def form_valid(self, form):
form.save()
messages.success(self.request, _('Your password has been set. You may go ahead and log in now.'))
return redirect('/')
def get_context_data(self, **kwargs):
context = super(RestorePasswordConfirmView, self).get_context_data(**kwargs)
context = {**context, **host_(self.request)}
return context
Is there any way to access the user.email
variable?
Upvotes: 1
Views: 746
Reputation: 2474
In PasswordResetConfirmView
it's expected that {{request.user.email}}
won't work because there's no authenticated user set on request. {{request.user}}
will hold an AnonymousUser
object.
However, if we're looking at how PasswordResetConfirmView
is implemented, we'll notice that in PasswordResetConfirmView.dispatch()
Django sets the user on self
like this:
@method_decorator(sensitive_post_parameters())
@method_decorator(never_cache)
def dispatch(self, *args, **kwargs):
assert 'uidb64' in kwargs and 'token' in kwargs
self.validlink = False
self.user = self.get_user(kwargs['uidb64'])
This means that you can override get_context_data()
as you did above and make sure that you're also sending the user to the template:
def get_context_data(self, **kwargs):
context = super(RestorePasswordConfirmView, self).get_context_data(**kwargs)
context = {**context, **host_(self.request)}
context['user'] = self.user
return context
Upvotes: 3