Search code examples
djangodjango-authentication

Django:Pass extra_context to reset_password view and receive the extra_context in given template


I am trying to redirect the password reset view to another template from the url directly as:

url(r'^accounts/password/reset/', password_reset, {'template_name':'users/login.html', 'extra_context':{'reset':'1'}, 'post_reset_redirect':'/users/accounts/login'}, name='password_reset'),

I have passed a context I need to use in my template.

In my 'users/login.html' template, I tried using:

            console.log('{{reset}}');

But the console does not display anything.

Is something missing? Or I am accessing the context in a wrong way?

URL patterns:

urlpatterns = [

url(r'^accounts/login/', web_login, name='web_login'),
url(r'^accounts/password/reset/', password_reset, {'template_name':'users/login.html', 'extra_context':{'reset':'1'}, 'post_reset_redirect':'/users/accounts/login'}, name='password_reset'),
url(r'^signup/', web_signup, name='web_signup'),
url(r'^create_role/', create_role, name='create_role'),
url(r'^activate/(?P<uidb64>[0-9A-Za-z_\-]+)/(?P<token>[0-9A-Za-z]{1,13}-[0-9A-Za-z]{1,20})/$',
    views.activate, name='activate'),]

Template: users/login.html

<div class="tab-pane fade" id="pills-thankyou" role="tabpanel" aria-labelledby="pills-thankyou-tab">
    <h5 class="mb-3">
        Success!
    </h5>
    <p>
        We have sent you an email with reset instructions.
    </p>
    <p>
        If the email does not arrive soon, check your spam folder. It was sent from <strong>support@fieldsight.org</strong>.
    </p>
    <button type="button" id="btn-backtologin" class="btn btn-secondary"><i class="la la-long-arrow-left"></i> Back to Login</button>
</div>
<div class="tab-pane fade" id="pills-reset" role="tabpanel" aria-labelledby="pills-reset-tab">
    <h5 class="mb-3">
        Forgot your password?
    </h5>
    <p>
        Don't worry. Resetting your password is easy, just tell us the email address you registered with <strong>Fieldsight</strong>.
    </p>
    <form method="post" action="/users/accounts/password/reset/">
        {% csrf_token %}
        <div class="form-group">
            <label for="inputResetEmail">Email address</label>
            <input type="email" name="email" class="form-control" id="inputResetEmail" required>
        </div>
        <button type="submit" class="btn btn-secondary" id="btn-resetpassword"><i class="la la-send" ></i> Submit</button>
        <button type="button" id="btn-backtologinfromreset" class="btn btn-secondary"><i class="la la-long-arrow-left"></i> Back to Login</button>
    </form>
</div>

JavaScript:

if('{{reset}}'=='1'){
    $('#pills-reset').removeClass('active show');
    $('#pills-thankyou').tab('show');

    fn_login_eqheight();
    fn_login_vertically_center();
    }
console.log('{{reset}}');

I even tried using {{ form.reset }} but the result was empty.

In my HTML console:

if(''=='1'){
    $('#pills-reset').removeClass('active show');
    $('#pills-thankyou').tab('show');

    fn_login_eqheight();
    fn_login_vertically_center();
}
console.log('');

Solution

  • After looking for some solutions, I came up with the idea that the 'post_reset_redirect':'/users/accounts/login' in the url is redirects to my web_login view function.

    Then I was able to catch the extra_context passed from the password_reset view in my web_login view using:

    reset = request.GET.get('reset')
    

    Then I passed this variable in my template after which the required functionality was achieved. Thank you everyone for spending your time looking into this problem.