1

hi am getting a syntax error

url:

url(r'^reset-password/$',
    PasswordResetView.as_view(template_name='accounts/reset_password.html', 'post_reset_redirect': 'accounts:password_reset_done'), name='reset_password'),

What is the problem?

thanks

1
  • 1
    You used a colon in 'post_reset_redirect': 'accounts:password_reset_done', instead of post_reset_redirect='accounts:password_reset_done'. Commented Sep 25, 2018 at 20:08

1 Answer 1

3

The problem is that you mix dictionary syntax with parameter syntax:

url(
    r'^reset-password/$',
    PasswordResetView.as_view(
        template_name='accounts/reset_password.html',
        'post_reset_redirect': 'accounts:password_reset_done'
    ),
    name='reset_password'
)

This syntax with a colon, is used for dictionaries. For parameters, it is identifier=expression, so:

from django.urls import reverse_lazy

url(
    r'^reset-password/$',
    PasswordResetView.as_view(
        template_name='accounts/reset_password.html',
        success_url=reverse_lazy('accounts:password_reset_done')
    ),
    name='reset_password'
)

The post_reset_redirect has been removed as parameter, but the success_url performs the same functionality: it is the URL to which a redirect is done, after the POST request has been handled successfully.

The wrong syntax probably originates from the fact that when you used a function-based view, you passed parameters through the kwargs paramter, which accepted a dictionary.

The class-based view however, obtains these parameter through the .as_view(..) call. Furthermore class-based views typically aim to generalize the process, and there the success_url, is used for FormViews.

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

4 Comments

thanks im getting this error now: TypeError: PasswordResetView() received an invalid keyword 'post_reset_redirect'. as_view only accepts arguments that are already attributes of the class.
when I do that I get a syntax error with my urlpattern closing bracket ]...i tried )) at end of urlpattern but then i get 'NameError: name 'reverse_lazy' is not defined
@JosephS: no, yyou need to import it: from django.urls import reverse_lazy.
The brackets are - afaik - correct (note that we colose both of them, one at the line in boldface, and one at the next line).

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.