Skip to content

Instantly share code, notes, and snippets.

@ErickMwazonga
Forked from vitorfs/change_password.html
Created December 6, 2017 20:52
Show Gist options
  • Save ErickMwazonga/f9545ec3c5bcba84b4523b127cd00f5e to your computer and use it in GitHub Desktop.
Save ErickMwazonga/f9545ec3c5bcba84b4523b127cd00f5e to your computer and use it in GitHub Desktop.
Django Change Password View
<form method="post">
{% csrf_token %}
{{ form }}
<button type="submit">Save changes</button>
</form>
from django.conf.urls import url
from myproject.accounts import views
urlpatterns = [
url(r'^password/$', views.change_password, name='change_password'),
]
from django.contrib import messages
from django.contrib.auth import update_session_auth_hash
from django.contrib.auth.forms import PasswordChangeForm
from django.shortcuts import render, redirect
from django.utils.translation import ugettext as _
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)
messages.success(request, _('Your password was successfully updated!'))
return redirect('accounts:change_password')
else:
messages.error(request, _('Please correct the error below.'))
else:
form = PasswordChangeForm(request.user)
return render(request, 'accounts/change_password.html', {
'form': form
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment