首页 > 解决方案 > django 电子邮件确认错误:TemplateResponseMixin 需要 'template_name' 或 'get_template_names()'

问题描述

我有一个带有 customUser 模型(电子邮件/密码)的 django 项目,我正在尝试让电子邮件验证链接正常工作。他们以前一直在工作,但现在由于某种原因,路径失败了。当您在我的网站上注册一个帐户时,您会收到一封带有注册确认 URL 的电子邮件,如下所示:

http://127.0.0.1:8000/rest-auth/registration/account-confirm-email/OA:1hxEiC:mbXKk8-is0YJz2FKHd_1d62KNv8/

但是,当您单击此 url 时,会导致出现错误页面并显示以下消息:

ImproperlyConfigured at /rest-auth/registration/account-confirm-email/OA:1hxEiC:mbXKk8-is0YJz2FKHd_1d62KNv8/
TemplateResponseMixin requires either a definition of 'template_name' or an implementation of 'get_template_names()'

我的主 urls.py 文件有一个 urlpatterns 列表,如下所示:

    urlpatterns = [
#admin page
path('admin/', admin.site.urls),
path('playlist/', include(('playlist.urls', 'playlist'), namespace='playlist')),
#users
path('users/', include('django.contrib.auth.urls')),
#accounts (allauth)
path('accounts/', include('allauth.urls')),
...
#django-rest-auth
url(r'^rest-auth/', include('rest_auth.urls')),
url(r'^rest-auth/registration/', include('rest_auth.registration.urls')),
#bug reports for this issue online point to a solution like so, but this isn't fixing the error
url(r"^rest-auth/registration/account-confirm-email/(?P<key>[\s\d\w().+-_',:&]+)/$", allauthemailconfirmation, name="account_confirm_email"),
#jwt
url(r'^refresh-token/', refresh_jwt_token),
]

有人可以帮我找出这个错误吗?我查看了许多其他在线发布的此问题的实例,并发现许多人通过使用正则表达式捕获注册确认路径来解决它,我已经尝试了在网上发布的解决方案中可以找到的所有正则表达式组合,但还没有有幸解决了这个问题。单击电子邮件确认链接时,我总是收到相同的错误。

非常感谢任何帮助,谢谢

标签: djangodjango-templatesdjango-urlsdjango-allauthdjango-email

解决方案


看来您查看需要定义allauthemailconfirmation的用途。你没有。所以在你看来,添加它。就像是TemplateResponseMixintemplate_name

class allauthemailconfirmation(TemplateResponseMixin, whateverClassesDeclared):
    template_name = 'path/to/filename.html'
    ...

推荐阅读