ImproperlyConfigured at /rest-auth/registration/account-confirm-email
Asked Answered
P

5

15

I'm using django-rest-auth for user signup and verify email. I'm able to successfully send the email when a user signs up. Howvever, on email verification, I'm getting this error with the following traceback:

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()'

Any idea on how to fix this ?

Packsaddle answered 19/4, 2015 at 3:12 Comment(3)
Did you solve the error?Despiteful
@Despiteful Can you try adding the content of entire templates directory from allauth into you own templates directory ?Packsaddle
No, #39638365 I try make it like hereDespiteful
U
9

While Ricardo's answer is correct, it didn't do much to help me solve my problem. This is what I needed to do for my master 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'),

Make sure that the beginning of the url spec starts with whatever path you are using for allauth REST calls.

Of course, the above is for using the built-in view handling the confirmation.

Unhealthy answered 4/1, 2018 at 7:32 Comment(0)
I
6

When you use confirmation email you have two ways to do it.

Using the specified by the API or creating yours. By default it uses django-allauth and a TemplateView on reverse can be used.

If you create yours, you may have to override account_confirm_email and then post to verification_mail.

In urls.py it is defined just reverse so, depending on what you are trying to do you will have first to create your own account_confirm_email, get the required key and post it to verify-email. Here there is more information about this bug.

Iolanthe answered 19/4, 2015 at 7:43 Comment(0)
C
3

For the new Django versions re_path url resolver method works properly with this (?P.+) url regex.

from django.urls import re_path

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

Also I have customized allauth ConfirmEmailView get() method in order to redirect properly

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)
Cryptozoic answered 13/12, 2019 at 17:39 Comment(1)
Bro, I have a similar different issue. in my case, the user successfully gets verified but in a case where the verification email has expired, I get an error about reverse login. Please, is there a way i can over-ride the confirm email view just like you did so it could point to a failure route, like "api/loginfailure/" just like in the post below #60235683Footboy
A
2

I am new to Django and struggled with this also. I have EMAIL_BACKEND = "django.core.mail.backends.console.EmailBackend" in settings.py. I went to the source code in my editor at .venv/Lib/site-packages/dj_rest_auth/registration/urls.py and found the following code with a comment:

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

So then I overrode this in my own project level 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',
    ),
] 

and I created this view in 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)

With this I was able to get a json response in the Django Rest Framework Browsable API:

{
    "message": "Email verified successfully"
}
Appearance answered 3/5, 2023 at 16:31 Comment(0)
F
0

I also have the problem as I read the tutorialHere is the link ,it shows the problem as TemplateResponseMixin requires either a definition of 'template_name' or an implementation of 'get_template_names()'
solution:

I change the location of templates filer and change the templates in the 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",],}]           
Forereach answered 12/9, 2017 at 8:4 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.