flaab
flaab

Reputation: 573

How to call an auth view from another view?

I want to execute a custom logic block in a view before the PasswordResetView is called. This is what I am trying to do, which of course always fails regardless the code, I must be taking a non adequate route to achieving this. I need to save some information, do some internal notifications and only call the password reset view if a condition is met.

views.py

from django.contrib.auth.views import PasswordResetView
def user_password_reset(request):
    # Do something here before loading the passwordresetview
    # Logic here 
    return PasswordResetView.as_view(template_name = 'account/password_reset_form.html',
                                     email_template_name='account/password_reset_email.html',
                                     html_email_template_name='account/password_reset_email.html',
                                     success_url = reverse_lazy('account:password_reset_done'))

Which is the standard way of achieving this?

Many thanks in advance.

Upvotes: 1

Views: 164

Answers (1)

willeM_ Van Onsem
willeM_ Van Onsem

Reputation: 476729

You can call the function that is returned by the .as_view():

from django.contrib.auth.views import PasswordResetView

def user_password_reset(request):
    # Do something here before loading the passwordresetview
    # Logic here 
    return PasswordResetView.as_view(
        template_name='account/password_reset_form.html',
        email_template_name='account/password_reset_email.html',
        html_email_template_name='account/password_reset_email.html',
        success_url = reverse_lazy('account:password_reset_done')
    )(request)

That being said, it might make more sense to just subclass the PasswordResetView, and for example overwrite the .dispatch(..) method.

Upvotes: 2

Related Questions