Django Password Resest Via Email
<p>I'm trying to implement password reset by sending an email to the user with a link which will redirect him/her to a new password form. </p>

<p>I took by example <a href="http://stackoverflow.com/questions/18928144/django-user-registration-password-reset-via-email">this question</a> and <a href="http://ruddra.com/2015/09/18/implementation-of-forgot-reset-password-feature-in-django/" rel="nofollow">this site</a>.</p>

<p>But my problem is a bit different. I don't have a local database containing the users, so I cannot perform operations over their attributes. I receive user data via an API (user id, user email, user password).</p>

<p>So, which is the best way to <strong>generate a unique link</strong> to send via email to user so that this link would <strong>tell me who the user is</strong> and allow me to <strong>reset his/her password</strong>? And also, how could I redirect it in <strong>urls.py</strong>?
I wish that this link could be used only a single time.</p>

<p>My views.py is like this:</p>

<pre><code>def password_reset_form(request):

if request.method == 'GET':
    form = PasswordResetForm()
else:
    form = PasswordResetForm(request.POST) 

    if form.is_valid():
        email = form.cleaned_data['email']

        content_dict = {
            'email': email,
            'domain': temp_data.DOMAIN,
            'site_name': temp_data.SITE_NAME,
            'protocol': temp_data.PROTOCOL,
            }

        subject = content_dict.get('site_name')+ ' - Password Reset'
        content = render_to_string('portal/password_reset_email.html', content_dict)
        send_mail(subject, content, temp_data.FIBRE_CONTACT_EMAIL, [email])

        return render(request, 'portal/password_reset_done.html', {'form': form,})

return render(request, 'portal/password_reset_form.html', {'form': form,})
</code></pre>

<p>And the template the e-mail I'm sending is:</p>

<pre><code>{% autoescape off %}

You're receiving this e-mail because we got a request to reset the password for your user account at {{ site_name }}.

Please go to the following page and choose a new password:

{% block reset_link %}
{{ protocol }}://{{ domain }}/[***some unique link***]
{% endblock %}

If you didn't request a password reset, let us know. 

Thank you.

The {{ site_name }} team.

{% endautoescape %}
</code></pre>

<p>Thanks, guys.</p>

<p><strong>Don't reinvent the wheel.</strong> You should use <a href="https://github.com/pennersr/django-allauth" rel="nofollow">django-allauth</a> for this kind of stuff. The library is well maintained and under active development.</p>

python django django-forms