Djang中的电子邮件验证

2024-06-16 13:43:30 发布

您现在位置:Python中文网/ 问答频道 /正文

我在django有一个webapp。我尝试使用令牌生成器重置密码来创建一个验证邮件,它没有激活电子邮件。在

说到问题

  1. 当用户提供电子邮件时,应检查数据库中是否存在电子邮件(数据库将用用户电子邮件更新)
  2. 在验证数据库中是否存在电子邮件后,系统会提示用户创建密码 3.创建密码后,用户可以登录到相应的页面

有什么解决办法吗?我试着跟着

https://medium.com/@frfahim/django-registration-with-confirmation-email-bb5da011e4ef

上面的帖子帮我发了邮件,但是用户在验证邮件后没有激活,帖子不符合我的要求,虽然我试着检查邮件验证是否可行。在

对于django是否有任何第三方模块或者针对我提到的需求的任何解决方案。在


Tags: django用户httpscom数据库密码电子邮件系统
2条回答

我想出了一个解决方案,但是对于第二个要求,用户必须在创建帐户时输入密码。主要目的是验证用户提供的电子邮件。在

型号

class Yourmodel(models.Model):
    first_name = models.CharField(max_length=200)
    second_name = models.CharField(max_length=200)
    email = models.EmailField(max_length=100)

视图

^{pr2}$

表格

from django.contrib.auth.forms import UserCreationForm


class SignupForm(UserCreationForm):
    class Meta:
        model = User
        fields = ('username', 'email', 'password1', 'password2')

电子邮件模板

{% autoescape off %}
Hi ,
Please click on the link to confirm your registration,
http://{{ domain }}{% url 'activate' uidb64=uid token=token %}
{% endautoescape %}

在注册表.html

{% csrf_token %}
{% for field in form %}
<label >{{ field.label_tag }}</label>
{{ field }}
{% endfor %}

If you don't want to compare with email address in your model you can skip, this will send the email to the email address which was supplied at the time registration without further validation.

email = form.cleaned_data.get('email')
if Yourmodel.objects.filter(email__iexact=email).count() == 1:

关于你的第一个问题我有个答案:

如果您基于PasswordResetView+PasswordResetConfirmView用户密码重置,则可以执行以下操作:

PasswordResetView负责向用户发送电子邮件。它使用自己的表单来输入用户电子邮件-PasswordResetForm。您可以创建自己的表单并从PasswordResetForm继承它。 例如:


class PRForm(PasswordResetForm):
    def clean_email(self):
        email = self.cleaned_data['email']
        if not User.objects.filter(email__iexact=email, is_active=True).exists():
            msg = "There is no user with this email."
            self.add_error('email', msg)
        return email

# User – your user model or any custom model if you have one instead of the default one

此代码将不允许控制器向您的数据库中没有的电子邮件地址发送电子邮件。在

然后在视图中指定此窗体:

^{pr2}$

RatelimitMixin不允许有人通过运行BD来暴力强迫你的DB。你能不能用它-取决于你自己。在

相关问题 更多 >