1

我正在使用 django-phonenumber-field。

使用此应用程序的每个人都可能知道任何前缀都有相同的示例“输入有效的电话号码(例如+12125552368)。”

输入有效的电话号码(例如 +12125552368)。

所以,我不喜欢它,我想改变它,但我无法改变这个文本,我想我会像这样在 html 中改变它:

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

国际电话代码或手机号码不正确!

但是当我更改它时,我意识到这种方法对我不起作用,因为用户可能会输入相同的电话号码并且无法理解为什么如果它没有发送适当的错误消息就不会保存。(电话号码是唯一的)现在有一个问题,在一种情况下,当用户在前缀中输入错误的号码时,这次我希望我的内置文本作为错误消息退出,但是当用户输入相同的number 我想删除 django-phonenumber-field 默认错误消息 使用此电话的帐户已存在。

我会就如何解决这个问题提出任何建议。

表格.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"]
4

1 回答 1

0

在表单中,您可以通过在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!'
    # …
于 2021-07-04T19:54:36.020 回答