在/rest-auth/registration/account-confirm-email处出现了配置不当的错误。

14

我在用户注册和验证电子邮件时使用django-rest-auth。当用户注册时,我能够成功发送电子邮件。但是,在电子邮件验证时,我收到了以下跟踪错误:

File "/Library/Python/2.7/site-packages/django/core/handlers/base.py" in get_response
  111.                     response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/Library/Python/2.7/site-packages/django/views/generic/base.py" in view
  69.             return self.dispatch(request, *args, **kwargs)
File "/Library/Python/2.7/site-packages/django/views/generic/base.py" in dispatch
  87.         return handler(request, *args, **kwargs)
File "/Library/Python/2.7/site-packages/django/views/generic/base.py" in get
  155.         return self.render_to_response(context)
File "/Library/Python/2.7/site-packages/django/views/generic/base.py" in render_to_response
  130.             template=self.get_template_names(),
File "/Library/Python/2.7/site-packages/django/views/generic/base.py" in get_template_names
  142.                 "TemplateResponseMixin requires either a definition of "

Exception Type: ImproperlyConfigured at /rest-auth/registration/account-confirm-email/vjohhnrf6xpkmn1jxbzaopdn0g79tdyofumeeuyuehcuja8slyz7nzq1idyifcqk/
Exception Value: TemplateResponseMixin requires either a definition of 'template_name' or an implementation of 'get_template_names()'
任何解决此问题的想法吗?

你解决了这个错误吗? - Weit
@Weit 你可以尝试将allauth的整个模板目录内容添加到你自己的模板目录中吗? - Saurabh Verma
不,我尝试按照这里的方式进行操作:https://dev59.com/KJrga4cB1Zd3GeqPu_3m - Weit
5个回答

9

虽然Ricardo的答案是正确的,但它并没有帮助我解决我的问题。这是我需要为我的主urls.py做的事情:

from allauth.account.views import confirm_email
.....
url(r'^accounts-rest/registration/account-confirm-email/(?P<key>.+)/$', confirm_email, name='account_confirm_email'),

请确保url规范的开头与您用于allauth REST调用的路径相同。

当然,上述内容是针对使用内置视图处理确认的情况。


6
当您使用确认电子邮件时,您有两种方法可以使用。
一种是使用API指定的方法,另一种是创建自己的方法。默认情况下,它使用django-allauth和TemplateView,但也可以使用reverse进行设置。
如果您创建自己的方法,则可能需要覆盖account_confirm_email并将其发布到verification_mail中。
在urls.py中只定义了反向操作,因此根据您尝试执行的操作,您需要首先创建自己的account_confirm_email,获取所需的密钥并将其发布到verify-email中。关于这个问题,这里有更多信息。

3

对于新版的Django,re_path URL解析器方法可以正常地与此(?P.+) URL正则表达式一起使用。

from django.urls import re_path

re_path('rest-auth/registration/account-confirm-email/(?P<key>.+)/', CustomConfirmEmailView.as_view(), name='account_confirm_email')

我已经自定义了allauth的ConfirmEmailView get()方法以便正确重定向

from allauth.account.views import ConfirmEmailView
from django.contrib.auth import get_user_model

class CustomConfirmEmailView(ConfirmEmailView):
    def get(self, *args, **kwargs):
        try:
            self.object = self.get_object()
        except Http404:
            self.object = None
        user = get_user_model().objects.get(email=self.object.email_address.email)
        redirect_url = reverse('user', args=(user.id,))
        return redirect(redirect_url)

兄弟,我有一个类似但不同的问题。在我的情况下,用户成功通过验证,但如果验证电子邮件已过期,则会出现有关反向登录的错误。请问是否有一种方法可以像您所做的那样覆盖确认电子邮件视图,以便它可以指向失败路由,例如“api/loginfailure/”,就像下面的帖子中所述:https://stackoverflow.com/questions/60235183/django-rest-auth-handling-expired-confirmation-email - Opeyemi Odedeyi

1
我是Django的新手,也遇到了这个问题。我在settings.py中设置了EMAIL_BACKEND = "django.core.mail.backends.console.EmailBackend"。我在我的编辑器中打开了.venv/Lib/site-packages/dj_rest_auth/registration/urls.py源代码,并找到了以下带有注释的代码:
urlpatterns = [
    path('', RegisterView.as_view(), name='rest_register'),
    path('verify-email/', VerifyEmailView.as_view(), name='rest_verify_email'),
    path('resend-email/', ResendEmailVerificationView.as_view(), name="rest_resend_email"),

    # This url is used by django-allauth and empty TemplateView is
    # defined just to allow reverse() call inside app, for example when email
    # with verification link is being sent, then it's required to render email
    # content.

    # account_confirm_email - You should override this view to handle it in
    # your API client somehow and then, send post to /verify-email/ endpoint
    # with proper key.
    # If you don't want to use API on that step, then just use ConfirmEmailView
    # view from:
    # django-allauth https://github.com/pennersr/django-allauth/blob/master/allauth/account/views.py
    re_path(
        r'^account-confirm-email/(?P<key>[-:\w]+)/$', TemplateView.as_view(),
        name='account_confirm_email',
    ),
]

然后我在自己的项目级别的urls.py中覆盖了它:

from django.urls import path, include, re_path

from {my_app_name}.views import CustomEmailConfirmView

urlpatterns = [
    path("dj-rest-auth/registration/", include("dj_rest_auth.registration.urls")),
    path("dj-rest-auth/", include("dj_rest_auth.urls")),
    re_path(
        r'^account-confirm-email/(?P<key>[-:\w]+)/$',
        CustomEmailConfirmView.as_view(),
        name='account_confirm_email',
    ),
] 

我在views.py中创建了这个视图:

class CustomEmailConfirmView(APIView):
    def get(self, request, key):
        verify_email_url = 'http://localhost:8000/dj-rest-auth/registration/verify-email/'

        # make a POST request to the verify-email endpoint with the key
        response = requests.post(verify_email_url, {'key': key})
        if response.status_code == 200:
            return Response({'message': 'Email verified successfully'}, status=status.HTTP_200_OK)
        else:
            return Response({'message': 'Email verification failed'}, status=status.HTTP_400_BAD_REQUEST)

通过这个,我能够在 Django Rest Framework Browsable API 中获取一个 JSON 响应:
{
    "message": "Email verified successfully"
}

0

我也遇到了与教程这里是链接中所述相同的问题,它显示了以下错误:TemplateResponseMixin requires either a definition of 'template_name' or an implementation of 'get_template_names()'
解决方案:

我更改了模板文件的位置并在setting.py中更改了模板。

   1. In the App_Name file,I add the New folder Named:templates                                  
   2. In the settings.py: TEMPLATES = [{'DIRS': [BASE_DIR+"/templates",],}]           

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接