Django Pass custom variables to template while using default views

Viewed 329

I am currently using a default django view (PasswordChangeView), but I have my own custom template that I want to use with it. I am achieving this by writing this:

path('changepassword/', PasswordChangeView.as_view(template_name='account/change_password.html', 
                                     success_url = '/changepassword/'), name='password_change'),

But in that template there is a variable like so: {{ var }}. Is there a way I can pass a value to that variable from urls.py? Or do I need to create an entire new custom view to pass this single variable?

1 Answers

Yes, you can pass extra parameters with:

path(
    'changepassword/',
    PasswordChangeView.as_view(
        template_name='account/change_password.html', 
        success_url = '/changepassword/'
    ),
    name='password_change',
    kwargs={'var': value_of_var}
),

Where you replace value_of_var with the value {{ var }} should use. This works because the URL parameters are also passed to as variables to the template.

Related