python - 通过电子邮件重置 Django 密码

标签 python django django-forms

我正在尝试通过向用户发送一封电子邮件来实现密码重置,其中包含一个链接,该链接会将他/她重定向到新的密码表单。

我举了个例子this questionthis 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 %}

谢谢大家。

最佳答案

不要重新发明轮子。您应该使用 django-allauth对于这种东西。该库维护良好并且正在积极开发中。

关于python - 通过电子邮件重置 Django 密码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39539102/

相关文章:

python - 使用 urllib2 从受基本身份验证保护的 Jenkins 服务器获取 URL

django - Ajax - 如何自动保存我的 Django 表单

python - PEP572 中的海象运算符示例

python - Django + DjangoCMS 信号处理程序未被调用

Apache 和 mod_wsgi 中的 Django 自定义错误处理程序

python - 从 Django 模型中过滤外键 ID

python - 测试从 Django 中的数据库获取选择的表单 - 不允许数据库访问

django - 如何根据 django 管理面板中的登录用户清理表单数据?

python - Pandas /Python : How to create new column based on values from other columns and apply extra condition to this new column

python - Merce csv 文件(来自文件夹)合并为一个,使用 Python 添加具有不同名称的列