How to allow users to change their own passwords in Django?

前端 未结 9 1377
梦如初夏
梦如初夏 2021-01-30 03:57

Can any one point me to code where users can change their own passwords in Django?

相关标签:
9条回答
  • 2021-01-30 04:15

    Its without need to go to shell enter passwd and reenter passwd

     python manage.py changepassword <username> 
      or
    /manage.py changepassword <username>
    

    Using shell

    python manage.py shell
    from django.contrib.auth.models import User
    users=User.objects.filter(email='<user_email>') 
      #you can user username or etc to get users query set
      #you can also use get method to get users
    user=users[0]
    user.set_password('__enter passwd__')
    user.save()
    exit()
    
    0 讨论(0)
  • 2021-01-30 04:15

    Once the url pattern is added as shown in Ciro Santilli's answer, a quick way to allow users to change passwords is to give them "staff access" for the admin functions. If you don't add them to any groups or give them special permissions, they can still change their password by going to the example.com/admin page. The staff access lets them go to the page even if it is blank; in the upper right corner they can click "change password" and use the admin funtionality.

    0 讨论(0)
  • 2021-01-30 04:18

    You can also just use the django.contrib.auth.views.password_change view in your URLconf. It uses a default form and template; supplying your own is optional.

    0 讨论(0)
  • 2021-01-30 04:18

    urls.py:

    urlpatterns = [
        url(r'^accounts/', include('django.contrib.auth.urls')),
    

    Template:

    <a href="{% url 'password_change' %}">{% trans "Change password" %}</a>
    

    Documented at: https://docs.djangoproject.com/en/1.9/topics/auth/default/#using-the-views

    0 讨论(0)
  • 2021-01-30 04:31

    Per the documentation, use:

    from django.contrib.auth.hashers import makepassword
    

    The main reason to do this is that Django uses hashed passwords to store in the database.

    password=make_password(password,hasher='default')
    obj=User.objects.filter(empid=emp_id).update(username=username,password=password)
    

    I used this technique for the custom user model which is derived from the AbstractUser model. I am sorry if I technically misspelled the class and subclass, but the technique worked well.

    0 讨论(0)
  • 2021-01-30 04:33

    This tutorial shows how to do it with function based views:

    View file:

    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
    
    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('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
        })
    

    Url file:

    from django.conf.urls import url
    from myproject.accounts import views
    
    urlpatterns = [
        url(r'^password/$', views.change_password, name='change_password'),
    ]
    

    And finally, the template:

    <form method="post">
      {% csrf_token %}
      {{ form }}
      <button type="submit">Save changes</button>
    </form>
    
    0 讨论(0)
提交回复
热议问题