1

I am creating a rest API and using Django-rest-auth to verify and create users. Everything works until I click the link to verify the user's email, but I get an error instead

error message upon following the link to verify the email

NoReverseMatch at /account-confirm-email/MjI:1iNmbO:BWhf4WhFzVR99YVYUqCB6X2CbcE/
Reverse for 'account_email' not found. 'account_email' is not a valid view function or pattern name.

settings.py

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.sites',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'rest_framework',
    'rest_framework.authtoken',
    'rest_auth',
    'rest_auth.registration',
    'allauth',
    'allauth.account',
    'allauth.socialaccount',
    'allauth.socialaccount.providers.google',
    'allauth.socialaccount.providers.facebook',
    'users',
]


ACCOUNT_USER_MODEL_USERNAME_FIELD = None
ACCOUNT_EMAIL_REQUIRED = True
ACCOUNT_USERNAME_REQUIRED = False
ACCOUNT_AUTHENTICATION_METHOD = 'email'
ACCOUNT_UNIQUE_EMAIL = True
ACCOUNT_LOGOUT_ON_GET = True

ACCOUNT_EMAIL_CONFIRMATION_EXPIRE_DAYS = 1
ACCOUNT_EMAIL_VERIFICATION = "mandatory"
ACCOUNT_LOGIN_ATTEMPTS_LIMIT = 5
ACCOUNT_LOGIN_ATTEMPTS_TIMEOUT = 86400 # 1 day in seconds
ACCOUNT_LOGOUT_REDIRECT_URL ='/accounts/login/'
LOGIN_REDIRECT_URL = '/accounts/profile'
SOCIALACCOUNT_EMAIL_VERIFICATION = 'none'

EMAIL_USE_TLS = True
EMAIL_HOST = 'smtp.gmail.com'
EMAIL_PORT = 587
EMAIL_HOST_USER = 'test@gmail.com'
EMAIL_HOST_PASSWORD = 'test'
DEFAULT_FROM_EMAIL = 'test@gmail.com'
DEFAULT_TO_EMAIL = EMAIL_HOST_USER

EMAIL_CONFIRMATION_AUTHENTICATED_REDIRECT_URL = '/'

urls.py

from rest_auth.registration.views import VerifyEmailView

urlpatterns = [
    path('admin/', admin.site.urls),
    url('api/rest-auth/', include('rest_auth.urls')),
    url('api/rest-auth/registration/', include('rest_auth.registration.urls')),
    re_path(r'^account-confirm-email/$', VerifyEmailView.as_view(), name='account_email_verification_sent'),
    re_path(r'^account-confirm-email/(?P<key>[-:\w]+)/$', VerifyEmailView.as_view(), name='account_confirm_email'),
]

A quick fix I made which solved the error a BIT but made it NOT a Rest API was adding a bunch of URLs as shown below

    from allauth.account import views

    url(r"^email/$", views.email, name="account_email"),
    url(r'^accounts/', include('allauth.urls')),

    url(r"^signup/$", views.signup, name="account_signup"),
    url(r"^login/$", views.login, name="account_login"),
    url(r"^logout/$", views.logout, name="account_logout"),

    url(r"^password/change/$", views.password_change,
        name="account_change_password"),
    url(r"^password/set/$", views.password_set, name="account_set_password"),

    url(r"^inactive/$", views.account_inactive, name="account_inactive"),

    # E-mail
    url(r"^email/$", views.email, name="account_email"),
    url(r"^confirm-email/$", views.email_verification_sent,
        name="account_email_verification_sent"),
    url(r"^confirm-email/(?P<key>[-:\w]+)/$", views.confirm_email,
        name="account_confirm_email"),

    # password reset
    url(r"^password/reset/$", views.password_reset,
        name="account_reset_password"),
    url(r"^password/reset/done/$", views.password_reset_done,
        name="account_reset_password_done"),
    url(r"^password/reset/key/(?P<uidb36>[0-9A-Za-z]+)-(?P<key>.+)/$",
        views.password_reset_from_key,
        name="account_reset_password_from_key"),
    url(r"^password/reset/key/done/$", views.password_reset_from_key_done,
        name="account_reset_password_from_key_done"),

as the above didn't fully solve the problem, please how can the account be verified without errors and still maintain being a rest API?

Opeyemi Odedeyi
  • 766
  • 1
  • 10
  • 38
  • Why is it looking for `account_email` instead of `account_confirm_email`? I’m talking about the first set of urls, not the fix. Seems like something is looking for `account_email`. Is there a template being called here? Does the email get confirmed? – onyeka Oct 27 '19 at 12:07
  • the email doesn't get confirmed, when the error shows. I doubt it is supposed to search for a template. – Opeyemi Odedeyi Oct 27 '19 at 15:52
  • It's not about doubt, the error indicates it's looking a particular view that presumably doesn't exist, something in the chain is trying to call it and can't find it. – onyeka Oct 27 '19 at 19:32
  • with a lot of searching I found something, on this link – Opeyemi Odedeyi Oct 27 '19 at 19:51
  • https://django-rest-auth.readthedocs.io/en/latest/faq.html – Opeyemi Odedeyi Oct 27 '19 at 19:51

3 Answers3

1

I ran in to this issue a few months back and had a bit of a challenge tracking the issue down.

The allauth.account app creates (or should create) two tables in your database: account_emailaddress and account_emailconfirmation. The reverse lookup is looking for an entry in these tables to verify the email address and then set the account to verified. I found out that during migration, these two tables were not created correctly. I'm not certain why that issue arose (to be honest, I'm still a little new at this stuff).

I had to comment out the allauth.account app, run a migration, then reinstate the app and run another migration. That solved the issue for me.

Sai Neelakantam
  • 919
  • 8
  • 15
Chris Enstrom
  • 11
  • 1
  • 5
0

Here's how I solved this issue.

in users.views I have a view like this

from rest_framework.decorators import api_view
from rest_framework.response import Response

@api_view()
def null_view(request):
    return Response(status=status.HTTP_400_BAD_REQUEST)

then import the null view in urls

path('rest-auth/registration/account-confirm-email/', null_view, name='account_confirm_email'),
  • 1
    thank you very much, this has been troubling me for a while. I will try it out and give feedback on the result, and mark it eventually. – Opeyemi Odedeyi Oct 25 '19 at 11:10
0

This is Michele from Udemy.

I checked your question and it seems somebody had the same problem and found a solution, here: Reverse for 'account_email_verification_sent' not found. 'account_email_verification_sent' is not a valid view function or pattern name

Specifically, the problem should be in the way the urls.py file has been handled: https://stackoverflow.com/a/48487631/10221214

I see that you have already done some work on your urls.py file (well done) but maybe instead of having to add so many url paths that can cause troubles (for example, you have two paths with name="account_email" which is wrong!) you can try by copying the ones that have been added in the other question I linked.

One more thing that you could do is to check the repository of the package you are using on GitHub, and from there just look for the error message you get and read the related code.

Please keep me updated!

Michele

pymike00
  • 1
  • 3