Skip to content

Instantly share code, notes, and snippets.

@Rembane
Created July 16, 2012 12:24
Show Gist options
  • Save Rembane/3122413 to your computer and use it in GitHub Desktop.
Save Rembane/3122413 to your computer and use it in GitHub Desktop.
A view to reset a user's password.
def reset_password(request):
form = PasswordForm(request.POST or None)
fluff = { 'page_title' : u'Återställ lösenord',
'button_text' : 'Skicka nytt lösenord',
'form' : form
}
if request.method == 'POST':
if form.is_valid():
email = form.cleaned_data['email']
current_site = Site.objects.get_current()
user = User.objects.get(email__iexact=email)
password = User.objects.make_random_password()
user.set_password(password)
user.save()
mail_template = loader.get_template('registration/new_password_email.txt')
mail_context = RequestContext(request, {'username' : user.username, 'password' : password, 'domain' : current_site.domain})
# Sänd mail till användaren
send_mail(u'Prologs anmälningssystem - Glömt lösenord', mail_template.render(mail_context), 'noreply@%s' % current_site.domain, [email], fail_silently=True)
return HttpResponseRedirect(reverse('mypage'))
return direct_to_template(request, 'konan/basic_form.html', extra_context=fluff)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment