【问题标题】:Avoid temp emails using Django避免使用 Django 发送临时电子邮件
【发布时间】:2021-11-04 11:26:53
【问题描述】:

我想阻止临时电子邮件并确保只有当电子邮件是真实的(如 Gmail、Outlook、Yahoo)时用户才能注册。

forms.py

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

views.py

@unauthenticated_user
def signup_form(request):
    if request.method == 'POST':
        form=SginupForm(request.POST)
        if form.is_valid():
            user=form.save()
            send_action_email(user,request)
            messages.add_message(request, messages.SUCCESS,
                                 'we have sent ur activation link')
            return redirect('core:login')
   
    else:
        form=SginupForm()
    return render(request,'user/sign-up.html',{'form':form})

【问题讨论】:

    标签: django email temp


    【解决方案1】:

    没有一种自动方法可以知道电子邮件是否是临时电子邮件。所以我只会使用白名单。只需确保包含所有最受欢迎的电子邮件提供商(在 Google 上搜索)。

    创建电子邮件列表。为了获得良好的实践,这应该是 views.py 文件顶部的常量。

    ALLOWED_EMAILS = ["gmail.com", "outlook.com", "yahoo.com"]
    

    然后,当您的用户提交注册表单时,只需验证电子邮件 地址以任何这些结尾。

    以下条件检查电子邮件是否不以任何列入白名单的电子邮件结尾。在用户提交表单后立即添加。添加您自己的自定义消息和重定向逻辑。

    email = form.cleaned_data.get("email")
    if not any(email.endswith(e) for e in ALLOWED_EMAILS):
        # Add a failure message to the request.
        # Redirect back to the login page.
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-03-07
      • 1970-01-01
      • 2014-06-06
      • 2011-04-09
      相关资源
      最近更新 更多