django - 语法正则表达式无效

标签 django regex

我正在尝试实现 solution到 错误

ImproperlyConfigured
TemplateResponseMixin requires either a definition of 'template_name' or an implementation of 'get_template_names()

这是我建议的 urls.py

from django.urls import include, path, re_path

urlpatterns = [
    re_path(r'^auth/registration/account-confirm-email/(?P<key>[\s\d\w().+-_',:&]+)/$', TemplateView.as_view(), name = 'account_confirm_email'),
    path('auth/registration/', include('dj_rest_auth.registration.urls')),
]

但我收到此错误。

re_path(r'^registration/account-confirm-email/(?P<key>[\s\d\w().+-_',:&]+)/$', TemplateView.as_view(), name = 'account_confirm_email'),
                                                                     ^
SyntaxError: invalid syntax

知道我可能会错过什么吗?谢谢!

最佳答案

语法突出显示已经显示了问题,您的正则表达式中存在一个“相当”,过早地结束了正则表达式。您可以使用双引号解决此问题:

从 django.urls 导入 include、path、re_path

urlpatterns = [
    re_path(
        r<b>"</b>^registration/account-confirm-email/(?P<key>[\s\d\w().+-_',: & ]+)/$<b>"</b>,
        TemplateView.as_view(),
        name='account_confirm_email'
    ),
    path('auth/registration/', include('dj_rest_auth.registration.urls')),
]

但这并不能修复第一个错误。这是提示你没有通过template_name参数 TemplateView :

urlpatterns = [
    re_path(
        r"^registration/account-confirm-email/(?P<key>[\s\d\w().+-_',: & ]+)/$",
        TemplateView.as_view(<b>template_name='<i>some_template.html</i>'</b>),
        name='account_confirm_email'
    ),
    path('auth/registration/', include('dj_rest_auth.registration.urls')),
]

@HakenLid says ,也许您还可以将正则表达式简化为 (?P<key>.+) ,这当然会稍微改变语义。

关于django - 语法正则表达式无效,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62244194/

相关文章:

django - Windows 上的最小生产 Django 服务器

django - Ingress 和 Ingress controller 如何将它们与 NodePort 服务一起使用?

c# - 重叠替换正则表达式?

regex - 不理解Powershell对象匹配方法的组/值/捕获属性

用于从字符串中匹配和提取整数的正则表达式

javascript - ajax 请求 django , jquery

django REST 框架 - 嵌套 ModelSerializer 的有限查询集?

python - 从提要内容中获取图标

Javascript Regex - 如何收集日期的前 2 个字母

regex - 将正则表达式从JS风格转换为Golang风格?