我们从Python开源项目中,提取了以下9个代码示例,用于说明如何使用django.contrib.auth.forms.SetPasswordForm()。
def update_password(request, uid, token): user = validate_signed_token(uid, token) if not user: return HttpResponseForbidden() # Just straight up forbid this request, looking fishy already! if request.method == 'POST': form = SetPasswordForm(user, data=request.POST) if form.is_valid(): form.save() Token.objects.filter(user_id__exact=user.pk).delete() return redirect(reverse('mhacks-login') + '?username=' + user.email) elif request.method == 'GET': form = SetPasswordForm(user) else: return HttpResponseNotAllowed(permitted_methods=['GET', 'POST']) form.fields['new_password2'].label = 'Confirm New Password' form.fields['new_password2'].longest = True return render(request, 'password_reset.html', {'form': form, 'type': 'reset', 'uid': uid, 'token': token})
def password_reset_confirm(request, key): template_name = 'accounts/password_reset_confirm.html' context = {} reset = get_object_or_404(PasswordReset, key=key) form = SetPasswordForm(user=reset.user, data=request.POST or None) if form.is_valid(): form.save() context['success'] = True context['form'] = form return render(request, template_name, context)
def __init__(self, *args, **kwargs): super(SetPasswordForm, self).__init__(*args, **kwargs) self.helper = FormHelper() self.helper.layout = Layout( Field('new_password1', placeholder="Enter new password", autofocus=""), Field('new_password2', placeholder="Enter new password (again)"), Submit('pass_change', 'Change Password', css_class="btn-warning"), )
def __init__(self, user, *args, **kwargs): super(SetPasswordForm, self).__init__(user, *args, **kwargs) del self.fields['new_password2']
def is_valid(self, bundle, request=None): data = bundle.data or {} if request.method == "PUT": errors = {} try: user = get_object_or_404(User, pk=data['id']) except KeyError: errors['id'] = ['id attribute is mandatory'] else: change_pw_fields = ['new_password1', 'new_password2'] if any((True for k in change_pw_fields if data[k] is not None)): from django.contrib.auth.forms import PasswordChangeForm, SetPasswordForm # Non-superusers always require old_password # Superusers require old_password when editing themselves if not request.user.is_superuser or request.user.id == user.id: form = PasswordChangeForm(user, data) else: form = SetPasswordForm(user, data) if not form.is_valid(): errors.update(form.errors) return errors form = ChromaUserChangeForm(data, instance=user) if not form.is_valid(): errors.update(form.errors) return errors elif request.method == "POST": form = UserCreationForm(data) if form.is_valid(): return {} else: return form.errors else: raise NotImplementedError
def password_reset_confirm(request, uidb64=None, token=None, template_name='registration/password_reset_confirm.html', token_generator=default_token_generator, set_password_form=SetPasswordForm, post_reset_redirect=None, current_app=None, extra_context=None): """ View that checks the hash in a password reset link and presents a form for entering a new password. """ UserModel = get_user_model() assert uidb64 is not None and token is not None # checked by URLconf if post_reset_redirect is None: post_reset_redirect = reverse('password_reset_complete') else: post_reset_redirect = resolve_url(post_reset_redirect) try: uid = urlsafe_base64_decode(uidb64) user = UserModel._default_manager.get(pk=uid) except (TypeError, ValueError, OverflowError, UserModel.DoesNotExist): user = None if user is not None and token_generator.check_token(user, token): validlink = True if request.method == 'POST': form = set_password_form(user, request.POST) if form.is_valid(): form.save() return HttpResponseRedirect(post_reset_redirect) else: form = set_password_form(None) else: validlink = False form = None context = { 'form': form, 'validlink': validlink, } if extra_context is not None: context.update(extra_context) return TemplateResponse(request, template_name, context, current_app=current_app)