Python django.contrib.auth.forms 模块,PasswordChangeForm() 实例源码
我们从Python开源项目中,提取了以下24个代码示例,用于说明如何使用django.contrib.auth.forms.PasswordChangeForm()。
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
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, 'Password change unsuccessful.')
form = EditProfileForm(instance=request.user)
args = {'form': form, args)
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,
})
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
})
def change_password_view(request):
if request.method == 'POST':
form = PasswordChangeForm(user=request.user, _('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',
'form': form
})
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)
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, td)
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, context)
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, PasswordChangeForm, '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
})
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)
def change_password(request):
if request.method == 'POST':
form = PasswordChangeForm(data=request.POST, 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)
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,
})
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})
def get(self, request):
form = EditProfileForm(instance=request.user)
form2 = PasswordChangeForm(user=request.user)
args = {'form': form, 'form2': form2 }
return render(request, args)
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, context)
def @H_399_3270@__init__(self, *args, **kwargs):
super(PasswordChangeForm, self).@H_399_3270@__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"),
)
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 change_password(request):
if request.method == 'POST':
form = PasswordChangeForm(request.user, 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
})
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})
def changePassword(request):
if request.method == "POST":
form = PasswordChangeForm(request.user, 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,Not accessible method")
def password_change(request,
template_name='home/password_change_form.html',
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, context)
def change_password(request):
if request.method == 'POST':
form = PasswordChangeForm(data=request.POST, 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)
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)
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。