我正在尝试实现 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/