django - 如何在不明确填写 PasswordResetForm 的情况下向用户发送电子邮件以重置密码?
问题描述
我正在使用 1.11.2 版本制作 Django Web 应用程序,其中管理员使用默认 django 身份验证视图和自定义 html 模板注册用户。我希望当管理员单击“注册”并提交详细信息(如用户名、电子邮件、密码和确认密码)同时向用户的电子邮件 ID 发送一封电子邮件,其中有一个重置密码的链接,以便用户可以重置他/她的密码。除非我使用忘记密码,否则我不想明确使用 password_reset_view 和 password_reset_done 视图选项。
这是我编码的内容。
视图.py
from django.contrib.auth import get_user_model
from django.contrib.auth.mixins import LoginRequiredMixin
from django.contrib.auth.models import Permission, User
from django.contrib.auth.views import RegisterView
class RegisterView(SuccessMessageMixin, CreateView):
form_class = RegisterForm
template_name = 'registration/register.html'
success_message = "Your account was created successfully."
def dispatch(self, *args, **kwargs):
return super(RegisterView, self).dispatch(*args, **kwargs)
注册.html
{% extends "base.html" %}
{% block nav_people %}
class="active"
{% endblock %}
{% block content %}
{% load static %}
<!DOCTYPE html>
<html lang=en>
<head>
<link rel="stylesheet" href="{% static "dashboard/css/login.css" %}"/>
</head>
<body>
<title>Register</title>
<div class="reg">
<div class="regbox">
<h1>Register</h1>
<br>
<form class='text-left' method="post" action="{% url 'register' %}">
{% csrf_token %}
{{ form.as_p }}
<input type="submit" value="Register" />
<input type="hidden" name="next" value="{{ next }}" />
</form>
</div>
</div>
</body>
</html>
{% endblock %}
表格.py
class RegisterForm(forms.ModelForm):
"""A form for creating new users. Includes all the required
fields, plus a repeated password."""
password1 = forms.CharField(label='Password', widget=forms.PasswordInput)
password2 = forms.CharField(label='Password confirmation',
widget=forms.PasswordInput)
class Meta:
model = User
fields = ('username', 'email',)
def clean_email(self):
email = self.cleaned_data.get("email")
qs = User.objects.filter(email__iexact=email)
if qs.exists():
raise forms.ValidationError("Cannot use this email. It's already
registered")
return email
def clean_password2(self):
# Check that the two password entries match
password1 = self.cleaned_data.get("password1")
password2 = self.cleaned_data.get("password2")
if password1 and password2 and password1 != password2:
raise forms.ValidationError("Passwords don't match")
return password2
def save(self, commit=True):
# Save the provided password in hashed format
user = super(RegisterForm, self).save(commit=False)
user.set_password(self.cleaned_data["password1"])
#user.password = "asdfasd"
user.is_active = True
if commit:
user.save()
# user.profile.send_activation_email()
# create a new user hash for activating email.
return user
登录.html
{% load static %}
<!DOCTYPE html>
<html lang=en>
<head>
<title>Login</title>
<link rel="stylesheet" href="{% static "dashboard/css/login.css" %}"/>
</head>
<body>
<div class="login">
<div class="loginbox">
{% if form.errors %}
<p>Your username and password didn't match. Please try again.</p>
{% endif %}
{% if next %}
{% if user.is_authenticated %}
<p> To proceed,
please login with an account that has access.</p>
{% else %}
{% if not form.errors %}
<p>Please login to see this page.</p>
{% endif %}
{% endif %}
{% endif %}
<form method="post" action="{% url 'login' %}">
{% csrf_token %}
<label>USER NAME </label>{{ form.username }}<br>
<label>PASSWORD </label>{{ form.password }}<br>
<input type="submit" value="Login" />
<input type="hidden" name="next" value="{{ next }}" />
</form>
<a href='{% url "reset_password" %}' class="btn">Forgot Password</a>
</div>
</div>
</body>
</html>
解决方案
密码重设邮件由 中的PasswordResetForm
班级发送django.contrib.auth.forms
。对于您特定版本的 Django,可以在此处找到代码:https ://github.com/django/django/blob/1.11.2/django/contrib/auth/forms.py#L225
大部分工作在save
方法中完成,一小部分在get_users
andsend_email
方法中完成。
您可以选择在您的子类中对PasswordResetForm
、 覆盖get_users
和调用super().save()
进行子类化。或者只是复制相关位(删除您不需要的东西)并将它们添加到您的表单类中。
推荐阅读
- rust - 仅用于借位检查的零宽度参考?
- python - 基于现有列创建pandas列:由另一列分组的列的条件最小值
- javascript - React / Javascript map 函数 - 返回数组
- python - 如何通过Python按目录文件读取对值进行排序
- .net - .NET 运行时优化服务缺失
- django - 日期格式在带有日期选择器的 html 页面上不起作用
- ios - 如何用用户上传的照片替换按钮
- mysql - EXPLAIN 从 MariaDB 迁移到 MySQL 后显示“DEPENDENT SUBQUERY”并且速度很慢
- rust - 如何确保函数调用后不可变借用结束以启用可变借用?
- c - 将数组指针递增到最后一项之外