码迷,mamicode.com
首页 > 其他好文 > 详细

2.5.1 修改密码

时间:2020-11-21 11:42:26      阅读:9      评论:0      收藏:0      [点我收藏+]

标签:change   success   links   astyle   you   doc   img   text   load   

目录

1.定位自带密码设置函数/usr/local/python3/lib/python3.6/site-packages/django/contrib/auth/views.py

@sensitive_post_parameters()
@csrf_protect
@login_required
@deprecate_current_app
def password_change(request,
                    template_name=registration/password_change_form.html,
                    post_change_redirect=None,
                    password_change_form=PasswordChangeForm,
                    extra_context=None):
    warnings.warn("The password_change() view is superseded by the "
                  "class-based PasswordChangeView().",
                  RemovedInDjango21Warning, stacklevel=2)
    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()
            # Updating the password logs out all other sessions for the user
            # except the current one.
            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)

@login_required
@deprecate_current_app
def password_change_done(request,
                         template_name=registration/password_change_done.html,
                         extra_context=None):
    warnings.warn("The password_change_done() view is superseded by the "
                  "class-based PasswordChangeDoneView().",
                  RemovedInDjango21Warning, stacklevel=2)
    context = {
        title: _(Password change successful),
    }
    if extra_context is not None:
        context.update(extra_context)

    return TemplateResponse(request, template_name, context)

 

2. 编辑 account/urls.py  设置 password_change url

from django.conf.urls import url 
from . import views
from django.contrib.auth import views as auth_view

urlpatterns = [ 
    url(r^login$,views.user_login,name="user_login"),
    url(r^new_login$,auth_view.login,{ "template_name":"account/registration/login.html"},name="new_user_login"),
    url(r^logout/$,auth_view.logout,{ "template_name":"account/logout.html"},name="user_logout"),
    url(r^register/$,views.register,name="user_register"),
    url(r^password_change/$,auth_view.password_change,{"post_change_redirect":"/account/password_change_done/","template_name":"account/password_change_form.html"},name="password_change"),
    url(r^password_change_done/$,auth_view.password_change_done,{"template_name":"account/password_change_done.html"},name="password_change_done"),
]

 

3.新建并编辑 templates/account/password_change_form.html 和 templates/account/password_change_done.html

templates/account/password_change_form.html 内容如下:(PS:其实用的是 django自带的 /usr/local/python3/lib/python3.6/site-packages/django/contrib/admin/templates/registration ,书里面提供的前端,不会提示密码不合理的地方)

{% extends "admin/base_site.html" %}
{% load i18n static %}
{% block extrastyle %}{{ block.super }}<link rel="stylesheet" type="text/css" href="{% static "admin/css/forms.css" %}" />{% endblock %}
{% block userlinks %}{% url ‘django-admindocs-docroot‘ as docsroot %}{% if docsroot %}<a href="{{ docsroot }}">{% trans ‘Documentation‘ %}</a> / {% endif %} {% trans ‘Change password‘ %} / <a href="{% url ‘admin:logout‘ %}">{% trans ‘Log out‘ %}</a>{% endblock %}
{% block breadcrumbs %}
<div class="breadcrumbs">
<a href="{% url ‘admin:index‘ %}">{% trans ‘Home‘ %}</a>
&rsaquo; {% trans ‘Password change‘ %}
</div>
{% endblock %}

{% block title %}{{ title }}{% endblock %}
{% block content_title %}<h1>{{ title }}</h1>{% endblock %}

{% block content %}<div id="content-main">

<form method="post">{% csrf_token %}
<div>
{% if form.errors %}
    <p class="errornote">
    {% if form.errors.items|length == 1 %}{% trans "Please correct the error below." %}{% else %}{% trans "Please correct the errors below." %}{% endif %}
    </p>
{% endif %}


<p>{% trans "Please enter your old password, for security‘s sake, and then enter your new password twice so we can verify you typed it in correctly." %}</p>

<fieldset class="module aligned wide">

<div class="form-row">
    {{ form.old_password.errors }}
    {{ form.old_password.label_tag }} {{ form.old_password }}
</div>

<div class="form-row">
    {{ form.new_password1.errors }}
    {{ form.new_password1.label_tag }} {{ form.new_password1 }}
    {% if form.new_password1.help_text %}
    <div class="help">{{ form.new_password1.help_text|safe }}</div>
    {% endif %}
</div>

<div class="form-row">
{{ form.new_password2.errors }}
    {{ form.new_password2.label_tag }} {{ form.new_password2 }}
    {% if form.new_password2.help_text %}
    <div class="help">{{ form.new_password2.help_text|safe }}</div>
    {% endif %}
</div>

</fieldset>

<div class="submit-row">
    <input type="submit" value="{% trans ‘Change my password‘ %}" class="default" />
</div>

</div>
</form></div>

{% endblock %}

 

templates/account/password_change_done.html 内容如下:

{% extends "base.html" %}
{% block title%}password change done {% endblock %}
{% block content%}
    <div class="text-center row vertical-md-sm">
        <p>your password was changed</p>
    </div>
{% endblock %}

 

4.浏览器打开 http://192.168.56.101:8800/account/password_change/

技术图片

 

 

 

 

确认老密码,填写新密码后,点击 change my password,自动跳到完成界面

技术图片

 

2.5.1 修改密码

标签:change   success   links   astyle   you   doc   img   text   load   

原文地址:https://www.cnblogs.com/yaoyuanchun/p/13986441.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!