我们从Python开源项目中,提取了以下5个代码示例,用于说明如何使用django.contrib.auth.forms.PasswordResetForm()。
def reset_password(request): reset_type = 'reset_request' if request.method == 'POST': form = PasswordResetForm(request.POST) if form.is_valid(): try: user = get_user_model().objects.get(email=form.cleaned_data["email"]) except ObjectDoesNotExist: form.errors['email'] = ["No user with that email exists"] return render(request, 'password_reset.html', context={'form': form, 'type': reset_type}) if user: send_password_reset_email(user, request) return redirect(reverse('mhacks-password_reset_sent')) elif request.method == 'GET': form = PasswordResetForm() else: return HttpResponseNotAllowed(permitted_methods=['GET', 'POST']) if form: form.fields['email'].longest = True return render(request, 'password_reset.html', context={'form': form, 'type': reset_type})
def get_users(self, email): """ Make sure users are staff users. Additionally to the other PasswordResetForm conditions ensure that the user is a staff user before sending them a password reset email. :param email: Textual email address. :return: List of users. """ # Django 1.8 supports this feature. if hasattr(super(PasswordResetForm, self), 'get_users'): return ( u for u in super(PasswordResetForm, self).get_users(email) if u.is_staff and u.is_active ) # Django Django < 1.8 support we can do this manually. active_users = get_user_model()._default_manager.filter(email__iexact=email, is_active=True) return (u for u in active_users if u.has_usable_password() and u.is_staff and u.is_active)
def password_reset_form(*args, **kwargs): """Custom error form""" return PasswordResetForm(*args, error_class=DivErrorList, **kwargs)
def password_reset(request, is_admin_site=False, template_name='registration/password_reset_form.html', email_template_name='registration/password_reset_email.html', subject_template_name='registration/password_reset_subject.txt', password_reset_form=PasswordResetForm, token_generator=default_token_generator, post_reset_redirect=None, from_email=None, current_app=None, extra_context=None): if post_reset_redirect is None: post_reset_redirect = reverse('password_reset_done') else: post_reset_redirect = resolve_url(post_reset_redirect) if request.method == "POST": form = password_reset_form(request.POST) if form.is_valid(): opts = { 'use_https': request.is_secure(), 'token_generator': token_generator, 'from_email': from_email, 'email_template_name': email_template_name, 'subject_template_name': subject_template_name, 'request': request, } if is_admin_site: opts = dict(opts, domain_override=request.get_host()) form.save(**opts) return HttpResponseRedirect(post_reset_redirect) else: form = password_reset_form() context = { 'form': form, } if extra_context is not None: context.update(extra_context) return TemplateResponse(request, template_name, context, current_app=current_app)
def password_reset(request, template_name='registration/password_reset_form.html', email_template_name='registration/password_reset_email.html', subject_template_name='registration/password_reset_subject.txt', password_reset_form=PasswordResetForm, token_generator=default_token_generator, post_reset_redirect=None, from_email=None, extra_context=None, html_email_template_name=None, extra_email_context=None): """ Deprecated. Request for new password """ if post_reset_redirect is None: post_reset_redirect = reverse('password_reset_done') else: post_reset_redirect = resolve_url(post_reset_redirect) if request.method == "POST": form = password_reset_form(request.POST) errors = [] if form.is_valid(): if check_user_email(form.cleaned_data["email"]): opts = { 'use_https': request.is_secure(), 'token_generator': token_generator, 'from_email': from_email, 'email_template_name': email_template_name, 'subject_template_name': subject_template_name, 'request': request, 'html_email_template_name': html_email_template_name, 'extra_email_context': extra_email_context, } form.save(**opts) return HttpResponseRedirect(post_reset_redirect) else: errors.append('invalidEmail') return render(request, template_name, {'form': form, 'errors': errors}) else: form = password_reset_form() context = { 'form': form, 'title': _('Password reset'), } if extra_context is not None: context.update(extra_context) return TemplateResponse(request, template_name, context)