簡體   English   中英

如何更改 django-phonenumber-field 錯誤消息

[英]How to change django-phonenumber-field error messages

我正在使用 django-phonenumber-field。

使用此應用程序的每個人都可能知道任何前綴都有相同的示例“輸入有效的電話號碼(例如 +12125552368)”。

輸入有效的電話號碼(例如 +12125552368)。

所以,我不喜歡它,我想改變它,但我無法改變這個文本,我想我會像這樣在 html 中改變它:

    {% if form.phone.errors %}
      
        <p class="alert-p"></p>Incorrect International Calling Code or Mobile Number!</p>
     
    {% endif %}

錯誤的國際電話代碼或手機號碼!

但是當我更改它時,我意識到這種方法對我不起作用,因為用戶可能會輸入相同的電話號碼並且無法理解為什么如果它沒有發送適當的錯誤消息就不會保存。 (電話號碼是唯一的)現在有一個問題,在一種情況下,當用戶在前綴中輸入錯誤的號碼時,這次我希望我的內置文本作為錯誤消息退出,但是當用戶輸入相同我想刪除 django-phonenumber-field 默認錯誤消息Account with this Phone 已經存在。

我會就如何解決這個問題接受任何建議。

表格.py

    from django import forms
    from django.contrib.auth.forms import ReadOnlyPasswordHashField
    from django.db.models.fields import CharField
    from django.forms import widgets
    from phonenumber_field.formfields import PhoneNumberField
    from phonenumber_field.widgets import PhoneNumberPrefixWidget
    from .models import Account
    from django_countries.widgets import CountrySelectWidget
    from django import forms
    from phonenumber_field.formfields import PhoneNumberField
    

class UserChangeForm(forms.ModelForm):

    class Meta:
        model = Account
        fields = ('email', 'fname', 'lname', 'phone','bday', 'country', 'gender','picture')
        
    # email = forms.CharField(widget=forms.EmailInput(attrs={'class':'form-control'}))
    # fname = forms.CharField(widget=forms.TextInput(attrs={'class':'form-control','placeholder':'Name'}))
    # lname = forms.CharField(widget=forms.TextInput(attrs={'class':'form-control', 'placeholder':'Surname'}))
    phone = PhoneNumberField(widget=PhoneNumberPrefixWidget(initial='GE'))
    # bday = forms.CharField(widget=DatePickerInput(attrs={'class':'form-control', 'placeholder':'Birth Date'}))
    # country = forms.CharField(widget=forms.TextInput(attrs={'class':'form-control','placeholder':'Country'}))
    # gender = forms.ChoiceField(choices=Account.GENDER_CHOICES, widget=forms.Select(attrs={'class':'regDropDown'}))
    # picture = forms.FileField(widget=forms.ClearableFileInput(attrs={'class':'form-control'}))


    
    
    def clean_password(self):
        # Regardless of what the user provides, return the initial value.
        # This is done here, rather than on the field, because the
        # field does not have access to the initial value
        return self.initial["password"]

在表單中,您可以通過在phone_number.error_messages字典中覆蓋它來覆蓋錯誤消息:

from django import forms
from phonenumber_field.formfields import PhoneNumberField

class UserChangeForm(forms.ModelForm):
    # …
    phone = PhoneNumberField(widget=PhoneNumberPrefixWidget(initial='GE'))
    phone.error_messages['invalid'] = 'Incorrect International Calling Code or Mobile Number!'
    # …

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM