我想阻止臨時電子郵件,并確保用戶只有在電子郵件是真實的情況下才能注冊(如Gmail、Outlook、Yahoo).
forms.py
class SginupForm(UserCreationForm)。
class Meta:
model = User
fields =('username', 'first_name','last_name', 'email'/span>,'password1'/span>,'password2'/span> )
views.py
@unauthenticated_user
def signup_form(request)。
if request.method == 'POST'/span>:
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'/span>,{'form'/span>:form})
uj5u.com熱心網友回復:
沒有一個自動的方法來知道一個電子郵件是否是臨時的。所以我只會使用一個白名單。只要確保你包括所有最流行的電子郵件供應商(做一個谷歌搜索)。
創建一個電子郵件的串列。為了達到良好的效果,這應該是你的views.py檔案頂部的一個常量。
ALLOWED_EMAILS = ["gmail.com"/span>, "outlook.com"/span>, "yahoo.com"/span>]
當你的用戶提交一個注冊表時,只需驗證該電子郵件 地址以其中任何一個結尾。
下面的條件檢查電子郵件是否不以任何白名單上的電子郵件結尾。在用戶提交表單后立即添加它。添加你自己的自定義訊息和重定向邏輯。
email = form.cleaned_data.get("email"/span>)
if not any(email.endswith(e) fore in ALLOWED_EMAILS):
# 在請求中添加一個失敗資訊。
# 重定向到登錄頁面。
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/313429.html
標籤:
