關於django form驗證是否使用者名稱已存在

2022-02-15 15:15:34 字數 1273 閱讀 5157

想通過django的form模組進行資料庫中是否已存在使用者名稱的驗證,首先我先呼叫了資料庫使用者名字段所有的值,發現是個queryset物件。

隨後經過查詢後發現queryset查詢集物件可以呼叫list工廠方法後,生成乙個類似於[('user_name_1','username_2')]的物件,隨即把列表中的元組物件拿出來進行遍歷,完成驗證,form**如下:

class

registerform(forms.form):

user_name = forms.charfield(label='

你的姓名

', max_length=10,error_messages=)

user_psd = forms.charfield(label='

你的密碼

',widget=forms.passwordinput())

user_psd2 = forms.charfield(label='

請再次輸入你的密碼

',widget=forms.passwordinput())

email = forms.emailfield(label='

你的郵件位址

',widget=forms.textinput)

defclean(self):

cleaned_data =self.cleaned_data

pwd = self.cleaned_data['

user_psd']

pwd2 =self.cleaned_data['

user_psd2']

user_form = self.cleaned_data['

user_name']

user_model = list(models.user_info.objects.all().values_list('

user_name'))

for i in

user_model:#此步i為類似(username_1,username_2,)的元組物件

print

(user_form)

if user_form in

i:

raise forms.validationerror('

使用者名稱已存在,請重新嘗試登入')

if pwd!=pwd2:

raise forms.validationerror('

兩次輸入的密碼不匹配')

return

cleaned_data

django form表單驗證

常用的field 使用field可以是對資料驗證的第一步。你期望這個提交上來的資料是什麼型別,那麼就使用什麼型別的field。charfield 用來接收文字。引數 max length 這個字段值的最大長度。min length 這個字段值的最小長度。required 這個字段是否是必須的。預設是...

DjangoForm 提交驗證

1.建立模版 class loginform forms.form 2.將請求交給模版,建立乙個物件 obj loginform request.post 3.進行驗證 obj.is valid 4.獲取正確的資訊 obj.clean 5.獲取錯誤的資訊 obj.errors 建立乙個 static...

djangoform表單使用驗證碼

直接安裝 pip install django captcha django自動幫我們安裝了相關的依賴庫six olefile和pillow,其中的pillow是大名鼎鼎的繪圖模組。註冊captcha django.contrib.admin django.contrib.auth django.c...