Python django.contrib.auth.forms 模块,PasswordChangeForm() 实例源码

我们从Python开源项目中,提取了以下24个代码示例,用于说明如何使用django.contrib.auth.forms.PasswordChangeForm()

项目:LDERP    作者:Ignoramuss    | 项目源码 | 文件源码
def get_context_data(self, **kwargs):
        context = super(LoginSignupView, self).get_context_data(**kwargs)
        context.update({
            'signup_form': RegistrationForm(),
            'message':''
        })
        return context

# def change_password(request):
#     if request.method == 'POST':
#         form = PasswordChangeForm(request.user, request.POST)
#         if form.is_valid():
#             user = form.save()
#             update_session_auth_hash(request, user)  # Important!
#             messages.success(request, 'Your password was successfully updated!')
#             return redirect('login:change_password')
#         else:
#             messages.error(request, 'Please correct the error below.')
#     else:
#         form = PasswordChangeForm(request.user)
#     response = render(request, 'password_change.html', {
#         'form': form
#     })
#     response.set_cookie('password_changed', 'true')
#     return response
项目:club-suite    作者:fsxfreak    | 项目源码 | 文件源码
def post(self, request):

    if 'details' in request.POST:
      form = EditProfileForm(request.POST, instance=request.user)
      if form.is_valid():
        form.save()
      else:
        messages.add_message(request, messages.ERROR, 'Could not edit account details.')

      form2 = PasswordChangeForm(user=request.user)
      args = {'form': form, 'form2': form2 }
      return render(request, 'account.html', args)

    elif 'password' in request.POST:
      form2 = PasswordChangeForm(user=request.user, data=request.POST)
      if form2.is_valid():
        form2.save()
        update_session_auth_hash(request, form2.user)
        messages.add_message(request, messages.SUCCESS, 'Sucessfully changed password.')
      else:
        messages.add_message(request, messages.ERROR, 'Password change unsuccessful.')

      form = EditProfileForm(instance=request.user)
      args = {'form': form, 'form2': form2 }
      return render(request, 'account.html', args)
项目:django-basic    作者:igor-chepurnoi    | 项目源码 | 文件源码
def account(request):
    if request.method == 'POST':
        form = PasswordChangeForm(request.user, request.POST)
        if form.is_valid():
            user = form.save()
            update_session_auth_hash(request, user)
            messages.success(request, _('Your password was successfully updated!'))

            return redirect('account')

    else:
        form = PasswordChangeForm(request.user)

    return render(request, 'account/index.html', {
        'form': form,
    })
项目:c3nav    作者:c3nav    | 项目源码 | 文件源码
def change_password_view(request):
    if request.method == 'POST':
        form = PasswordChangeForm(user=request.user, data=request.POST)
        if form.is_valid():
            form.save()
            login(request, request.user)
            messages.success(request, _('Password successfully changed.'))
            return redirect('site.account')
    else:
        form = PasswordChangeForm(user=request.user)

    for field in form.fields.values():
        field.help_text = None

    return render(request, 'site/account_form.html', {
        'title': _('Change password'),
        'back_url': reverse('site.account'),
        'form': form
    })
项目:c3nav    作者:c3nav    | 项目源码 | 文件源码
def change_password_view(request):
    if request.method == 'POST':
        form = PasswordChangeForm(user=request.user, data=request.POST)
        if form.is_valid():
            form.save()
            login(request, request.user)
            messages.success(request, _('Password successfully changed.'))
            return redirect('editor.users.detail', pk=request.user.pk)

    else:
        form = PasswordChangeForm(user=request.user)

    for field in form.fields.values():
        field.help_text = None

    return render(request, 'editor/account_form.html', {
        'title': _('Change password'),
        'back_url': reverse('site.account'),
        'form': form
    })
项目:tissuelab    作者:VirtualPlants    | 项目源码 | 文件源码
def password_change(request,
                    template_name='registration/password_change_form.html',
                    post_change_redirect=None,
                    password_change_form=PasswordChangeForm,
                    current_app=None, extra_context=None):
    if post_change_redirect is None:
        post_change_redirect = reverse('password_change_done')
    else:
        post_change_redirect = resolve_url(post_change_redirect)
    if request.method == "POST":
        form = password_change_form(user=request.user, data=request.POST)
        if form.is_valid():
            form.save()
            return HttpResponseRedirect(post_change_redirect)
    else:
        form = password_change_form(user=request.user)
    context = {
        'form': form,
    }
    if extra_context is not None:
        context.update(extra_context)
    return TemplateResponse(request, template_name, context,
                            current_app=current_app)
项目:pontifex-project    作者:PontifexProject    | 项目源码 | 文件源码
def password_change(request):
    """
    Render Pontifex password change scheme
    """
    td = {
        "user": request.user,
    }
    if request.method == "POST":
        form = PasswordChangeForm(user=request.user, data=request.POST)
        td["form"] = form
        if form.is_valid():
            form.save()
            messages.success(request, "Password successfully changed.")
            return redirect("/users/profile/")
        else:
            for field, error_list in form.errors.iteritems():
                for msg in error_list:
                    messages.warning(request, msg)
            return render(request, "users/password_change.html", td)
    else:
        form = PasswordChangeForm(request.user)
        td["form"] = form
    return render(request, "users/password_change.html", td)
项目:Sarahah-Django    作者:zerossB    | 项目源码 | 文件源码
def settings(request, setting):
    template_name = "accounts/settings.html"
    template = "accounts/security/_default.html"
    context = {}
    if setting == "profile":
        form = EditUserForm(instance=request.user)
        context['form'] = form
        template = "accounts/security/_" + setting + ".html"
    elif setting == "changePassword":
        form = PasswordChangeForm(request.user)
        context['form'] = form
        template = "accounts/security/_" + setting + ".html"
    elif setting == "removeProfile":
        form = RemoveProfileForm()
        context['form'] = form
        messages.warning(
            request, "Are you sure that you want to delete your account? Deleting the account is irreversible!"
        )
        template = "accounts/security/_" + setting + ".html"
    else:
        messages.error(request, "Error 404, area does not exist")

    context['template'] = template
    context['setting'] = setting
    return render(request, template_name, context)
项目:maas    作者:maas    | 项目源码 | 文件源码
def userprefsview(request):
    user = request.user
    # Process the profile update form.
    profile_form, response = process_form(
        request, ProfileForm, reverse('prefs'), 'profile', "Profile updated.",
        {'instance': user})
    if response is not None:
        return response

    # Process the password change form.
    password_form, response = process_form(
        request, PasswordChangeForm, reverse('prefs'), 'password',
        "Password updated.", {'user': user})
    if response is not None:
        return response

    return render(
        request,
        'maasserver/prefs.html',
        {
            'profile_form': profile_form,
            'password_form': password_form
        })
项目:accounts    作者:mhadiahmed    | 项目源码 | 文件源码
def ChangePassword(request):
    if request.method == 'POST':
        form = PasswordChangeForm(data=request.POST, user=request.user)

        if form.is_valid():
            form.save()
            update_session_auth_hash(request,form.user)
            return redirect('profile')
        else:
            return redirect('change_password')
    else:
        form = PasswordChangeForm(user=request.user)
        context = {
        "forms":form,
        "title":"Change Password"
        }
        return render(request,"Change_pass.html",context)
项目:supreme-couscous    作者:Pulkit07    | 项目源码 | 文件源码
def change_password(request):
    if request.method == 'POST':
        form = PasswordChangeForm(data=request.POST, user=request.user)

        if form.is_valid():
            form.save()
            update_session_auth_hash(request, form.user)
            return redirect('/profile')
        else:
            return redirect('rocket/change_password')
    else:
        form = PasswordChangeForm(user=request.user)
        args = {'form': form}
        return render(request, 'rocket/change_password.html', args)
项目:Bitpoll    作者:fsinfuhh    | 项目源码 | 文件源码
def account_settings(request):
    password_change_form = PasswordChangeForm(request.user)
    nick_change_form = NickChangeForm(request.user)
    email_change_form = EmailChangeForm(request.user)

    form = request.POST.get('form')
    if form == 'change_pw':
        password_change_form = PasswordChangeForm(request.user, request.POST)
        if password_change_form.is_valid():
            password_change_form.save()
            messages.success(request, _("Password was changed."))
            return redirect('registration_account')
    elif form == 'change_nick':
        nick_change_form = NickChangeForm(request.user, request.POST)
        if nick_change_form.is_valid():
            nick_change_form.save()
            messages.success(request, _('Your nickname is now {}.').format(
                nick_change_form.cleaned_data['nickname']))
            return redirect('registration_account')
    elif form == 'change_email':
        email_change_form = EmailChangeForm(request.user, request.POST)
        if email_change_form.is_valid():
            return _verify_email(request,
                                 email_change_form.cleaned_data['email'])

    return TemplateResponse(request, 'registration/account.html', {
        'password_change_form': password_change_form,
        'nick_change_form': nick_change_form,
        'email_change_form': email_change_form,
        'username': request.user.username,
    })
项目:CoBL-public    作者:lingdb    | 项目源码 | 文件源码
def change_password(request):
    if request.method == 'POST':
        redirect_url = reverse("view-profile")
        form = PasswordChangeForm(request.user, request.POST)
        if "cancel" in form.data:  # has to be tested before data is cleaned
            return HttpResponseRedirect(redirect_url)
        if form.is_valid():
            form.save()
            msg = "Password changed"
            messages.add_message(request, messages.INFO, msg)
            return HttpResponseRedirect(redirect_url)
    else:
        form = PasswordChangeForm(request.user)
    return render_template(request, "profiles/change_password.html",
                           {"form": form})
项目:club-suite    作者:fsxfreak    | 项目源码 | 文件源码
def get(self, request):
    form = EditProfileForm(instance=request.user)
    form2 = PasswordChangeForm(user=request.user)
    args = {'form': form, 'form2': form2 }
    return render(request, 'account.html', args)
项目:simplemooc    作者:paulopinda    | 项目源码 | 文件源码
def edit_password(request):
    template_name = 'accounts/edit_password.html'
    context = {}

    if request.method == 'POST':
        form = PasswordChangeForm(data=request.POST, user=request.user)

        if form.is_valid():
            form.save()
            context['success'] = True
    else:
        form = PasswordChangeForm(user=request.user)

    context['form'] = form
    return render(request, template_name, context)
项目:elephant-sms-ews    作者:stultus    | 项目源码 | 文件源码
def __init__(self, *args, **kwargs):
        super(PasswordChangeForm, self).__init__(*args, **kwargs)
        self.helper = FormHelper()

        self.helper.layout = Layout(
            Field('old_password', placeholder="Enter old password",
                  autofocus=""),
            Field('new_password1', placeholder="Enter new password"),
            Field('new_password2', placeholder="Enter new password (again)"),
            Submit('pass_change', 'Change Password', css_class="btn-warning"),
            )
项目:intel-manager-for-lustre    作者:intel-hpdd    | 项目源码 | 文件源码
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
项目:atom-hw    作者:flame0    | 项目源码 | 文件源码
def change_password(request):
    if request.method == 'POST':
        form = PasswordChangeForm(request.user, request.POST)
        if form.is_valid():
            user = form.save()
            update_session_auth_hash(request, user)  # Important!
            messages.success(request, 'Your password was successfully updated!')
            return redirect('account:change_pass')
        else:
            messages.error(request, 'Please correct the error below.')
    else:
        form = PasswordChangeForm(request.user)
    return render(request, 'account/change_password.html', {
        'form': form
    })
项目:OMS    作者:ywzhou123    | 项目源码 | 文件源码
def password_change(request):
    if request.method == 'POST':
        form = PasswordChangeForm(user=request.user, data=request.POST)
        if form.is_valid():
            form.save()
            update_session_auth_hash(request, form.user)
            return HttpResponseRedirect(reverse('app:password_change_done'))
    else:
        form = PasswordChangeForm(user=request.user)
    return render(request, 'password_change.html', {'form':form})
项目:waves-demo    作者:lirmm    | 项目源码 | 文件源码
def __init__(self, *args, **kwargs):
        super(PasswordChangeForm, self).__init__(*args, **kwargs)
        self.helper = FormHelper()

        self.helper.layout = Layout(
            Field('old_password', placeholder="Enter old password",
                  autofocus=""),
            Field('new_password1', placeholder="Enter new password"),
            Field('new_password2', placeholder="Enter new password (again)"),
            Submit('pass_change', 'Change Password', css_class="btn-warning"),
        )
项目:Sarahah-Django    作者:zerossB    | 项目源码 | 文件源码
def changePassword(request):
    if request.method == "POST":
        form = PasswordChangeForm(request.user, request.POST)
        if form.is_valid():
            user = form.save()
            update_session_auth_hash(request, user)
            messages.success(
                request, 'Your password was successfully updated!')
            return redirect('account:dashboard')
        else:
            messages.error(request, 'Please correct the error below.')
    else:
        messages.error(request, "Error 404, Not accessible method")
项目:Sermul    作者:CHOQUERC    | 项目源码 | 文件源码
def password_change(request,
                    template_name='home/password_change_form.html',
                    post_change_redirect=None,
                    password_change_form=PasswordChangeForm,
                    empleado_slug=None,
                    extra_context=None):
    if post_change_redirect is None:
        post_change_redirect = reverse('home:login_user')
    else:
        post_change_redirect = resolve_url(post_change_redirect)
    if request.method == "POST":
        form = password_change_form(user=request.user, data=request.POST)
        if form.is_valid():
            form.save()
            # Updating the password logs out all other sessions for the user
            # except the current one if
            # django.contrib.auth.middleware.SessionAuthenticationMiddleware
            # is enabled.
            messages.success(request, _('Contrasena cambiado correctamente'),extra_tags='html_dante')
            update_session_auth_hash(request, form.user)
            return HttpResponseRedirect(post_change_redirect)
    else:
        form = password_change_form(user=request.user)
    context = {
        'form': form,
        'title': _('Password change'),
    }
    if extra_context is not None:
        context.update(extra_context)

    return TemplateResponse(request, template_name, context)
项目:Django-Tutorials    作者:maxg203    | 项目源码 | 文件源码
def change_password(request):
    if request.method == 'POST':
        form = PasswordChangeForm(data=request.POST, user=request.user)

        if form.is_valid():
            form.save()
            update_session_auth_hash(request, form.user)
            return redirect(reverse('accounts:view_profile'))
        else:
            return redirect(reverse('accounts:change_password'))
    else:
        form = PasswordChangeForm(user=request.user)

        args = {'form': form}
        return render(request, 'accounts/change_password.html', args)
项目:helfertool    作者:helfertool    | 项目源码 | 文件源码
def change_user(request):
    if request.user.has_usable_password():
        pw_form = PasswordChangeForm(data=request.POST or None,
                                     user=request.user)

        if pw_form.is_valid():
            pw_form.save()
            update_session_auth_hash(request, pw_form.user)
            messages.success(request, _("Changed password successfully"))
    else:
        # user from LDAP
        pw_form = None

    context = {'pw_form': pw_form}
    return render(request, 'registration/change_user.html', context)