第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問(wèn)題,去搜搜看,總會(huì)有你想問(wèn)的

Django 表單驗(yàn)證錯(cuò)誤顯示 Django 調(diào)試頁(yè)面

Django 表單驗(yàn)證錯(cuò)誤顯示 Django 調(diào)試頁(yè)面

斯蒂芬大帝 2023-04-18 15:55:02
我有一個(gè)注冊(cè)視圖,其中包含電子郵件、密碼、確認(rèn)密碼和額外的字符串,這些字符串必須是唯一的。我所有的驗(yàn)證錯(cuò)誤都正確返回(例如,如果電子郵件重復(fù),它顯示這必須是唯一的,如果密碼不匹配則顯示密碼不匹配)。但是,額外的字符串會(huì)顯示帶有驗(yàn)證錯(cuò)誤的 Django 調(diào)試頁(yè)面,而不是將其顯示到表單中。為什么會(huì)這樣?Django調(diào)試頁(yè)面報(bào)錯(cuò):ValidationError at /signup/['Extra string must be unique.']模板摘錄:                    {% for field in form %}                        <div class="form-group">                            {% for error in field.errors %}                                <p style="color: red">{{ error }}</p>                            {% endfor %}                            <label for="{{ field.id_for_label }}">{{ field.label }}:</label>                            {{ field }}                        </div>                    {% endfor %}形式:class UserCreationForm(forms.ModelForm):    password1 = forms.CharField(label='Password', widget=forms.PasswordInput(attrs={'class': 'form-control'}))    password2 = forms.CharField(label='Password confirmation', widget=forms.PasswordInput(attrs={'class': 'form-control'}))    email = forms.CharField(label='Email', widget=forms.EmailInput(attrs={'class': 'form-control'}))    extra_string = forms.CharField(label='Extra String (Must be unique)', widget=forms.TextInput(attrs={'class': 'form-control'}))    class Meta:        model = User        fields = ('email',)    def clean_password2(self):        """A function to check that the two passwords provided by the user match."""        # Check that the two password entries match        #: User's password.        password1 = self.cleaned_data.get("password1")        #: Password confirm.        password2 = self.cleaned_data.get("password2")        if password1 and password2 and password1 != password2:            raise forms.ValidationError("The passwords must match.") #: This displays properly        return password2    def ensure_unique_string(self):        """Checks that the entered extra string is unique"""
查看完整描述

1 回答

?
慕神8447489

TA貢獻(xiàn)1780條經(jīng)驗(yàn) 獲得超1個(gè)贊

因?yàn)槟阍诒4娣椒ㄖ刑岢觯?dāng)你在保存時(shí),告訴你所有的字段都是有效的,所以你需要在調(diào)用保存方法之前驗(yàn)證你的字段。


你有 2 個(gè)解決方案:


在 clean_FIELD_NAME 中:


   def clean_extra_string(self, data):

        if len(ExtraString.objects.filter(name=data)) > 0:

            raise forms.ValidationError("Ana Group Name must be unique.")

        return data

在驗(yàn)證方法中:


 def validate(self, validate_data):

        if len(ExtraString.objects.filter(name=validate_data['extra'])) > 0:

            raise forms.ValidationError("Ana Group Name must be unique.")


    return validate_data


查看完整回答
反對(duì) 回復(fù) 2023-04-18
  • 1 回答
  • 0 關(guān)注
  • 123 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)