语法正则表达式无效



我正在尝试实现解决方案 错误

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>[sdw().+-_',:&]+)/$', 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>[sdw().+-_',:&]+)/$', TemplateView.as_view(), name = 'account_confirm_email'),
^
SyntaxError: invalid syntax

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

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

从 django.urls 导入包括、路径re_path

urlpatterns = [
re_path(
r"^registration/account-confirm-email/(?P<key>[sdw().+-_',: & ]+)/$",
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>[sdw().+-_',: & ]+)/$",
TemplateView.as_view(template_name='some_template.html'),
name='account_confirm_email'
),
path('auth/registration/', include('dj_rest_auth.registration.urls')),
]

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

最新更新