简体   繁体   English

SyntaxError: URLpattern 中的语法无效

[英]SyntaxError: invalid syntax in URLpattern

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谢谢

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:对于参数,它是identifier=expression ,所以:

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.post_reset_redirect已被删除的参数,但success_url执行相同的功能:它是哪一个重定向完成的URL,POST请求已成功处理后。

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.错误的语法可能源于这样一个事实:当您使用基于函数的视图时,您通过kwargs参数传递参数,该参数接受字典。

The class-based view however, obtains these parameter through the .as_view(..) call.然而,基于类的视图通过.as_view(..)调用获取这些参数。 Furthermore class-based views typically aim to generalize the process, and there the success_url , is used for FormView s.此外,基于类的视图通常旨在概括该过程,并且success_url用于FormView s。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM