【发布时间】:2017-01-25 03:10:28
【问题描述】:
我正在尝试通过向用户发送带有链接的电子邮件来实现密码重置,该链接会将他/她重定向到新的密码表单。
我以this question 和this site 为例。
但我的问题有点不同。我没有包含用户的本地数据库,因此我无法对其属性执行操作。我通过 API 接收用户数据(用户 ID、用户电子邮件、用户密码)。
所以,这是生成唯一链接以通过电子邮件发送给用户的最佳方式,以便此链接告诉我用户是谁并允许我重置他/她的密码?还有,我如何在 urls.py 中重定向它? 我希望这个链接只能使用一次。
我的views.py是这样的:
def password_reset_form(request):
if request.method == 'GET':
form = PasswordResetForm()
else:
form = PasswordResetForm(request.POST)
if form.is_valid():
email = form.cleaned_data['email']
content_dict = {
'email': email,
'domain': temp_data.DOMAIN,
'site_name': temp_data.SITE_NAME,
'protocol': temp_data.PROTOCOL,
}
subject = content_dict.get('site_name')+ ' - Password Reset'
content = render_to_string('portal/password_reset_email.html', content_dict)
send_mail(subject, content, temp_data.FIBRE_CONTACT_EMAIL, [email])
return render(request, 'portal/password_reset_done.html', {'form': form,})
return render(request, 'portal/password_reset_form.html', {'form': form,})
我发送的电子邮件的模板是:
{% autoescape off %}
You're receiving this e-mail because we got a request to reset the password for your user account at {{ site_name }}.
Please go to the following page and choose a new password:
{% block reset_link %}
{{ protocol }}://{{ domain }}/[***some unique link***]
{% endblock %}
If you didn't request a password reset, let us know.
Thank you.
The {{ site_name }} team.
{% endautoescape %}
谢谢各位。
【问题讨论】:
-
@KlausD.,不,这是密码的盐。
标签: python django django-forms