2
votes

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.

1
Have you added the URLs as noted in step 3 on django-rest-auth.readthedocs.io/en/latest/… ? If so have you name spaced the URLs?Stuart Dines
Yeah! I had added the mentioned URLs. The password reset flow seemed complex to me. However, after going through the code and debugging a lot I found the problem and now its working. Thanks for the suggestion though!5parkp1ug
Using your serializer.py and settings.py above, I get this error: /rest_auth/utils.py", line 10, in import_callable package, attr = path_or_callable.rsplit('.', 1) ValueError: not enough values to unpack (expected 2, got 1) . Any idea what I've done wrong?Little Brain

1 Answers

4
votes

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. ;)