Django表单不会使用Happypy表单呈现PhoneNumberField

时间:2017-07-03 15:13:16

标签: django django-forms

您好,我的联系表格代码如下:

class ContactForm(forms.Form):
    def __init__(self, *args, **kwargs):
        super(ContactForm, self).__init__(*args, **kwargs)
        self.helper = FormHelper()
        self.helper.form_class = 'form-horizontal'
        self.helper.label_class = "col-md-2"
        self.helper.layout = Layout(
                 Div(
                     #I need to set size for every field wrapped in a div
                     Div('from_name', css_class="col-md-6 form-group"),
                     Div('from_email', css_class='col-md-6 form-group'),
                     Div('phone_number', css_class='col-md-6 form-group'),
                     Div('subject', css_class='col-md-6'),
                     Div('message', css_class='form-group col-md-12'),
                     css_class='row'
                 ),
            ButtonHolder(
                Submit('submit', 'Submit', css_class='btn btn-small btn-dark-solid')
            )
        )
    from_name = forms.EmailField(required=True, label='Name', max_length=100, widget=forms.TextInput(attrs={'class':'form-control', 'id':'name'}))
    from_email = forms.EmailField(required=True, label='Email', max_length=100, widget=forms.TextInput(attrs={'class':'form-control', 'id':'email'}))
    phone_number = PhoneNumberField(blank=True, default='', null=True, unique=True)
    subject = forms.CharField(required=True, label='Subject', max_length=100, widget=forms.TextInput(attrs={'class':'form-control', 'id':'subject'}))
    message = forms.CharField(required=True, label='Message', max_length=100, widget=forms.TextInput(attrs={'class':'cmnt-text form-control', 'id':'message'}))

这些是来自设置的已安装应用:

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'maet',
    'debug_toolbar',
    'phonenumber_field',
    'crispy_forms',
]

以下是观点:

def comming_soon(request):
    if request == 'POST':
        form = ContactForm(request.POST)
        if form.is_valid():
            return HttpResponseRedirect('/thanks/')
    else:
        form = ContactForm()
        # import ipdb;ipdb.set_trace();
    return render(request, 'maet/comming_soon.html', {'form':form})

这是加载表单的模板的一部分:

<form method="post" action="#" id="form" role="form" class="contact-comments">
      <div class="row">
       {% csrf_token %}
       {% crispy form %}
      </div>
</form>

问题是表单不会呈现'phone_number'字段,我想使用django-phonenumber-field库来验证电话号码。

我在这里做错了什么?

0 个答案:

没有答案