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

Django中TemplateResponseMixin报错问题求助

问题:React注册后点击邮箱验证链接触发TemplateResponseMixin报错

我试了所有办法都搞不定,快烦死了!用React完成注册流程后,能收到包含验证链接的邮件,但点击链接时总会弹出TemplateResponseMixin相关报错。我怀疑是文件夹结构出了问题,但拿不准,恳请社区帮忙排查。完整代码可查看我的GitHub仓库。

views.py 代码:

from allauth.account.views import ConfirmEmailView
from allauth.account.models import EmailConfirmation, EmailAddress
from django.views.generic.base import TemplateResponseMixin
from django.http import JsonResponse, HttpResponse
from django.template.response import TemplateResponse
from django.views.generic import TemplateView
from django.shortcuts import render
from django.contrib.sites.models import Site
import logging

logger = logging.getLogger(__name__)

class CustomConfirmEmailView(ConfirmEmailView):
    template_name = 'account/email_confirmed.html'  

    def get(self, request, *args, **kwargs):
        print("DEBUG: CustomConfirmEmailView gestartet mit Key:", kwargs.get("key"))
        try:
            confirmation = self.get_object()
            print("DEBUG: Confirmation gefunden:", confirmation)
            confirmation.confirm(request)
            print("DEBUG: Benutzer erfolgreich bestätigt")

            user = confirmation.email_address.user
            user.is_active = True
            user.save()

            return JsonResponse({"message": "Email erfolgreich bestätigt"})
        except Exception as e:
            print(f"DEBUG: Fehler in CustomConfirmEmailView: {e}")
            return JsonResponse({"error": str(e)}, status=400)

def test_template_view(request):
    return render(request, 'account/email_confirmed.html', {"message": "Test erfolgreich"})

def account_inactive_view(request):
    logger.debug("Account Inactive View aufgerufen")
    return JsonResponse({"error": "Your account is inactive. Please confirm your email to activate it."}, status=403)

def index(request):
    return render(request, 'index.html')

urls.py 代码:

from django.contrib import admin
from django.urls import path, include
from project.views import CustomConfirmEmailView, account_inactive_view, test_template_view

urlpatterns = [
    path('admin/', admin.site.urls),
    path('', include('project.urls')),
    path('dj-rest-auth/', include('dj_rest_auth.urls')),
    path('dj-rest-auth/registration/', include('dj_rest_auth.registration.urls')),
    path(
        'dj-rest-auth/registration/account-confirm-email/<key>/',
        CustomConfirmEmailView.as_view(),
        name='account_confirm_email',
    ),
    path('account-inactive/', account_inactive_view, name='account_inactive'),
    path('test-template/', test_template_view, name='test_template'),
]

内容的提问来源于stack exchange,提问作者ChrisCross83

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.14 23:18:12