Django REST Password Reset

1.5.0 · active · verified Thu Apr 16

django-rest-passwordreset is an extension for Django REST Framework that provides a configurable password reset strategy. It handles token generation, validation, and password setting endpoints. The current version is 1.5.0, and it maintains an active release cadence, frequently updating to support newer Django and DRF versions.

Common errors

Warnings

Install

Imports

Quickstart

To set up django-rest-passwordreset, you need to add it to your `INSTALLED_APPS`, include its URLs in your project's `urls.py`, and crucially, implement a receiver function for the `reset_password_token_created` signal to send the password reset email. Without this signal handler, no emails will be sent, and users won't receive their reset links. Replace the example URL and email content with your actual frontend reset page URL and email templates.

# settings.py
INSTALLED_APPS = [
    # ...
    'rest_framework',
    'django_rest_passwordreset',
]

# urls.py
from django.urls import path, include
from django.dispatch import receiver
from django.template.loader import render_to_string
from django.core.mail import EmailMultiAlternatives
from django_rest_passwordreset.signals import reset_password_token_created

urlpatterns = [
    # ...
    path('api/password_reset/', include('django_rest_passwordreset.urls', namespace='password_reset')),
]

# signals.py (or anywhere appropriate in your app)
@receiver(reset_password_token_created)
def password_reset_token_created(sender, instance, reset_password_token, *args, **kwargs):
    """ 
    Handles password reset tokens
    When a token is created, an e-mail needs to be sent to the user
    """
    # Example: Render HTML email content and send
    context = {
        'current_user': reset_password_token.user,
        'username': reset_password_token.user.username,
        'email': reset_password_token.user.email,
        'reset_password_url': "{}?token={}".format(
            instance.request.build_absolute_uri('/reset-password/confirm/'),
            reset_password_token.key
        )
    }

    # In a real app, you'd render a proper template
    email_html_message = render_to_string('email/user_reset_password.html', context)
    email_plaintext_message = render_to_string('email/user_reset_password.txt', context)

    msg = EmailMultiAlternatives(
        # title:
        f"Password Reset for {reset_password_token.user.username}",
        # message:
        email_plaintext_message,
        # from:
        os.environ.get('DEFAULT_FROM_EMAIL', 'noreply@example.com'), 
        # to:
        [reset_password_token.user.email]
    )
    msg.attach_alternative(email_html_message, "text/html")
    msg.send()

# Example template content for 'email/user_reset_password.html' and '.txt' would be required.
# For running this quickstart example, ensure you have an SMTP server configured for Django.

view raw JSON →