3

I have Django 2.0.2 with custom User model. One of feature is give anonymous users way to create order without "register-first" on site.

Main idea is:

  • Anonymous user fill order form, enter e-mail address and click Create Order;
  • Django create User with entered e-mail and random generated password;
  • Next, Django (or Celery) send to e-mail link for reset password (like from standard reset form);
  • User check e-mail and click to reset link, re-enter his own password.

This was killed two functions by one: user register and create first order.

And question: how can I send reset password mail from my custom views? I understand, link will generate and send on PasswordResetView view, but how to call to them on custom view?

koddr
  • 764
  • 2
  • 9
  • 27

1 Answers1

3

To send the password reset link from a view you can fill out a PasswordResetForm which is what is used by the PasswordResetView https://docs.djangoproject.com/en/2.0/topics/auth/default/#django.contrib.auth.forms.PasswordResetForm

As described in another stackoverflow answer here https://stackoverflow.com/a/30068895/9394660 the form can be filled like this:

from django.contrib.auth.forms import PasswordResetForm

form = PasswordResetForm({'email': user.email})

if form.is_valid():
    request = HttpRequest()
    request.META['SERVER_NAME'] = 'www.mydomain.com'
    request.META['SERVER_PORT'] = '443'
    form.save(
        request= request,
        use_https=True,
        from_email="username@gmail.com", 
        email_template_name='registration/password_reset_email.html')

Note: if you are not using https replace the port with 80 and dont include use_https=True

Also depending on the situation you may already have a request and wouldn't need to create one

hwhite4
  • 695
  • 4
  • 6