0

I'm using dj_rest_auth for authentication and when I click on the verify email link, this error comes out enter image description here

I overriden the ConfirmEmailView of django-allauth but still getting the error My views are:

class CustomConfirmEmailView(ConfirmEmailView):
    template_name = 'accounts/register.html'

2 Answers2

1

You need to pass template_name

class CustomRegisterView(RegisterView):
    serializer_class = CustomRegisterSerializer
    template_name = 'path/to/filename.html'
    success_url = '/url_name/'
1

The error you're seeing typically arises when Django's class-based view, which uses the TemplateResponseMixin, doesn't know which template to use.

The ConfirmEmailView from django-allauth doesn't usually render a template by default; instead, it redirects after confirming the email. However, if you're trying to override its behavior to render a template, you're on the right track.

Given what you've provided:

  1. Ensure Template Exists: Double-check that the template 'accounts/register.html' exists in one of your TEMPLATES directories as defined in settings.py.

  2. Override the post Method: Since ConfirmEmailView doesn't usually render a template, you may need to override the post method to ensure that it does:

from allauth.account.views import ConfirmEmailView as BaseConfirmEmailView

class CustomConfirmEmailView(BaseConfirmEmailView):
    template_name = 'accounts/register.html'

    def post(self, *args, **kwargs):
        # Add your custom logic here if needed

        # Ensure the super's post method is called for the email confirmation
        response = super().post(*args, **kwargs)
        
        # If you want to then render your template instead of redirecting:
        return self.render_to_response({})
  1. URL Configuration: Ensure that your URLs are pointing to your CustomConfirmEmailView and not the original ConfirmEmailView. Make sure your URL patterns are set up correctly in your urls.py:
from django.urls import path
from .views import CustomConfirmEmailView

urlpatterns = [
    # ... your other url patterns
    path('account-confirm-email/<str:key>/', CustomConfirmEmailView.as_view(), name='account_confirm_email'),
    # ... your other url patterns
]
  1. Ensure App Order: Make sure that your app, which contains the overridden view, comes before 'allauth.account' in the INSTALLED_APPS setting. This ensures that when URL reverse lookups occur, your app's URLs take precedence.

If after these steps you're still facing issues, try to isolate the problem. Perhaps start with the default ConfirmEmailView and make incremental changes, checking the behavior at each step. This will help you pinpoint where the issue is coming from.

Mihail Andreev
  • 979
  • 4
  • 6