django 在/rest-auth/registration/account-confirm-email配置不正确

ix0qys7i  于 2023-05-08  发布在  Go
关注(0)|答案(5)|浏览(133)

我正在使用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()'

有办法解决吗?

0wi1tuuw

0wi1tuuw1#

虽然李嘉图的答案是正确的,但它并没有帮助我解决问题。这是我需要为我的主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调用的任何路径开始。
当然,以上是使用内置视图处理确认。

aydmsdu9

aydmsdu92#

当您使用确认电子邮件时,您有两种方法来完成。
使用API指定的或创建自己的。默认情况下,它使用django-allauth,可以使用反向的TemplateView。
如果您创建了自己的帐户,您可能需要覆盖account_confirm_email,然后发送到verification_mail。
在www.example.com中urls.py,它的定义是反向的,因此,根据您尝试做的事情,您必须首先创建自己的account_confirm_email,获取所需的密钥并将其发布到verify-email。Here有更多关于这个bug的信息。

uoifb46i

uoifb46i3#

对于新的Django版本,re_path url resolver方法可以正确地使用这个(?)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)
56lgkhnf

56lgkhnf4#

我在阅读教程时也遇到了这个问题。这是链接,它将问题显示为TemplateResponseMixin requires either a definition of 'template_name' or an implementation of 'get_template_names()'
解决方案:
我更改模板文件的位置,并在www.example.com中更改模板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",],}]
vcudknz3

vcudknz35#

我是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',
    ),
]

然后我在我自己的项目级别www.example.com中覆盖了它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',
    ),
]

我在www.example.com中创建了这个视图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"
}

相关问题