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 ?

3
  • Did you solve the error?
    – Weit
    Sep 22, 2016 at 11:16
  • @Weit Can you try adding the content of entire templates directory from allauth into you own templates directory ? Sep 22, 2016 at 11:41
  • No, stackoverflow.com/questions/39637865/… I try make it like here
    – Weit
    Sep 22, 2016 at 12:24

5 Answers 5

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.

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.

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)
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 stackoverflow.com/questions/60235183/… Feb 21, 2020 at 14:45
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"
}
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",],}]           

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.