Search code examples
pythondjangodjango-rest-frameworkdjango-allauthdjango-rest-auth

django-rest-auth: Issue with Password Reset functionaliity


I have been trying to setup password reset functionality in DRF using django-rest-auth. Earlier I was getting error TemplateDoesNotExist:registration/password_reset_email.html which I resolved by adding the following code

serializer.py

from rest_auth.serializers import PasswordResetSerializer
from allauth.account.forms import ResetPasswordForm  

class PasswordSerializer(PasswordResetSerializer):
    password_reset_form_class = ResetPasswordForm

settings.py

REST_AUTH_SERIALIZERS = {
    'PASSWORD_RESET_SERIALIZER': 'api.serializers.PasswordSerializer',
}

However, Now I am getting into another issue - "NoReverseMatch: Reverse for 'account_reset_password_from_key' not found. 'account_reset_password_from_key' is not a valid view function or pattern name.". And haven't found any solution or workaround for this.

Any help would be appreciated.


Solution

  • So, finally I got the password reset functionality working. Here is how it goes -

    We just need one URL in our urls.py -

    urlpatterns = [
    url(r'^account/', include('allauth.urls')),  
    url(r'^rest-auth/', include('rest_auth.urls')),  
    
    # This is the only URL required for BASIC password reset functionality.
    # This URL creates the confirmation link which is sent via e-mail. All of the rest
    # password reset features get their reverse lookup via django-allauth and django-rest-auth.
    url(r'^password-reset/confirm/(?P<uidb64>[0-9A-Za-z_\-]+)/(?P<token>[0-9A-Za-z]{1,13}-[0-9A-Za-z]{1,20})/$', TemplateView.as_view(),  name='password_reset_confirm'), 
    
    url(r'^rest-auth/registration/account-confirm-email/(?P<key>[-:\w]+)/$', allauthemailconfirmation,
        name="account_confirm_email"),
    url(r'^rest-auth/registration/', include('rest_auth.registration.urls'), name='account_signup'),  
    ]
    

    Using this URL configuration raised TemplateDoesNotExist at /api/rest-auth/password/reset/ error first. After a lot of debugging, I found that the issue was raised for the template - registration/password_reset_email.html which resides under the Django Admin's template directory. This happened due to another Django app that I was using and it had disabled the django admin app.

    So, adding 'django.contrib.admin' under INSTALLED_APPS and removing the serializers resolved the issue.

    I hope this resolves issue for others as well.

    PS: Debugger is your best friend. ;)