2

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 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

3 Comments

Sorry I wasn't specific enough in my question. This worked for fixed value variables, but is there any way to pass things based on 'request'? (such as request.user.is_anonymous, which normally I can use in a view.)
Like this: stackoverflow.com/questions/4838480/… but for the newer django version
@figbar: the request object is also passed to the template, you can thus for example write {% if request.user.is_authenticated %}...{% endif %} in the template.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.