django如何实现重置密码,刚接到这个需求的时候,也是想着自己撸,其实有很多方式可供选择,相信你现在去网上搜索的话,你也会搜索到很多实现方式,比如有的文档会写到用命令行方式修改,有的会提到django-password-reset模块, 有的会提到django-allauth模块,有的会提到自己动手撸,同时django本身内置了该功能,如果想尽快实现的话,可以选择内置的,短平快,简单粗暴。
danke-tools: django-admin.py startproject reset_user_password
danke-tools: cd reset_user_password
reset_user_password: ls
manage.py reset_user_password
reset_user_password: python manage.py migrate
Operations to perform:
Apply all migrations: admin, auth, contenttypes, sessions
Running migrations:
Applying contenttypes.0001_initial... OK
Applying auth.0001_initial... OK
Applying admin.0001_initial... OK
Applying admin.0002_logentry_remove_auto_add... OK
Applying admin.0003_logentry_add_action_flag_choices... OK
Applying contenttypes.0002_remove_content_type_name... OK
Applying auth.0002_alter_permission_name_max_length... OK
Applying auth.0003_alter_user_email_max_length... OK
Applying auth.0004_alter_user_username_opts... OK
Applying auth.0005_alter_user_last_login_null... OK
Applying auth.0006_require_contenttypes_0002... OK
Applying auth.0007_alter_validators_add_error_messages... OK
Applying auth.0008_alter_user_username_max_length... OK
Applying auth.0009_alter_user_last_name_max_length... OK
Applying auth.0010_alter_group_name_max_length... OK
Applying auth.0011_update_proxy_permissions... OK
Applying sessions.0001_initial... OK
reset_user_password: python manage.py makemigrations
No changes detected
reset_user_password: python manage.py runserver
Watching for file changes with StatReloader
Performing system checks...
System check identified no issues (0 silenced).
You have 17 unapplied migration(s). Your project may not work properly until you apply the migrations for app(s): admin, auth, contenttypes, sessions.
Run 'python manage.py migrate' to apply them.
June 12, 2020 - 14:59:38
Django version 2.2.10, using settings 'reset_user_password.settings'
Starting development server at http://127.0.0.1:8000/
reset_user_password: python manage.py createsuperuser
Username (leave blank to use 'local'): admin
Email address: zhuimaxx@gmail.com
Password:
Password (again):
The password is too similar to the email address.
This password is too short. It must contain at least 8 characters.
Bypass password validation and create user anyway? [y/N]: y
Superuser created successfully.
reset_user_password:
django内置了密码重置功能,其实我们只需要在urls.py文件里修改一行然后添加一行即可,修改后效果如下
"""reset_user_password URL Configuration
The `urlpatterns` list routes URLs to views. For more information please see:
https://docs.djangoproject.com/en/2.2/topics/http/urls/
Examples:
Function views
1. Add an import: from my_app import views
2. Add a URL to urlpatterns: path('', views.home, name='home')
Class-based views
1. Add an import: from other_app.views import Home
2. Add a URL to urlpatterns: path('', Home.as_view(), name='home')
Including another URLconf
1. Import the include() function: from django.urls import include, path
2. Add a URL to urlpatterns: path('blog/', include('blog.urls'))
"""
from django.contrib import admin
# 新增导入include
from django.urls import path, include
urlpatterns = [
path('admin/', admin.site.urls),
# 新增下面这行
path('accounts/', include('django.contrib.auth.urls')),
]
•企业内部如果使用了LDAP的话,如何快速实现用户自助修改密码,这个时候使用Django + Django signals可以轻松实现需求,前后不超过30
分钟。你不希望老有人过来打断你手头的工作让你帮忙重置密码吧。•自己写一些工具的时候
使用官方内置功能是为了短平快实现需求,如果是内部造轮子,还是要好好规划,避免急功冒进。
如果样式和邮件内容不能满足需求的话,可以进行view和template的重写来满足需求,代码量极小,希望本文对你能有所帮助。
尽可能多看官方文档,尽可能多看英文文档,这个时候你会感受到学好英语的重要性。