You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用dj-rest-auth点击邮箱验证链接报错:需定义template_name

解决dj-rest-auth邮箱验证链接报错问题

你遇到的错误是因为视图渲染响应时无法正确找到模板,即便指定了template_name仍存在异常,可从以下几个方向排查修复:

  • 确认继承的视图类正确
    确保CustomConfirmEmailView继承的是dj_rest_auth.registration.views.ConfirmEmailView,而非django-allauth原生的allauth.account.views.ConfirmEmailView。dj-rest-auth对原视图做了适配,继承错误会导致模板逻辑异常。

  • 检查模板路径有效性
    验证accounts/register.html的路径是否合规:

    • 模板文件需放在项目或对应app的templates目录下,比如your_app/templates/accounts/register.html
    • 确保包含该模板的app已添加到settings.py的INSTALLED_APPS列表中
  • 手动实现get_template_names()方法
    若直接指定template_name仍无效,可重写get_template_names()方法强制返回模板路径,规避父类逻辑干扰:

    from dj_rest_auth.registration.views import ConfirmEmailView
    
    class CustomConfirmEmailView(ConfirmEmailView):
        def get_template_names(self):
            return ['accounts/register.html']
    
  • 确认路由配置正确
    在项目urls.py中替换dj-rest-auth默认的邮箱验证路由,指向你的自定义视图,注意要放在include('dj_rest_auth.registration.urls')之前以确保优先匹配:

    from django.urls import path, include
    from .views import CustomConfirmEmailView
    
    urlpatterns = [
        # 其他路由
        path('dj-rest-auth/registration/account-confirm-email/<str:key>/', CustomConfirmEmailView.as_view(), name='account_confirm_email'),
        path('dj-rest-auth/', include('dj_rest_auth.urls')),
        path('dj-rest-auth/registration/', include('dj_rest_auth.registration.urls')),
    ]
    

内容的提问来源于stack exchange,提问作者Vicente Antonio G. Reyes

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.13 15:37:06