3

I'm trying to render a simple form with no db models but I am getting error. Following is the code:

from django import forms
from django.utils.safestring import mark_safe
class ContactForm(forms.Form):
    category_options = (
        ('select category', '--Select Category--'),
        ('fire safety', 'Fire Safety'),
        ('batteries/solar panels', 'Batteries/Solar Panels'),
        ( 'cctv systems','CCTV Systems')
    )
    category = forms.ChoiceField(label="Select a Category", choices=category_options, required=True)
    full_name = forms.CharField(required=False)
    email = forms.EmailField()
    phone = forms.CharField(widget=forms.IntegerField)
    message = forms.CharField(widget=forms.Textarea(attrs={'cols': 10, 'rows': 10}))

    def clean_email(self):
        email = self.cleaned_data.get('email')
        
        return email

I'm not getting the phone field right. I need to add validation for phone numbers and couldn't figure out the right way to use widget for IntegerField.

I need to find the accurate way of adding a phone field.

I'm getting the error:

AttributeError at /contact/
'IntegerField' object has no attribute 'is_hidden'

I get following results when I use

phone = forms.CharField(widget=forms.NumberInput)

enter image description here

My html generated from the python code for different fields is following:

<p> 
<label for="id_email">Email:</label>
<input id="id_email" name="email" type="email">
</p>

<p> 
<label for="id_phone">Phone:</label>
<input id="id_phone" name="phone" type="number">
</p>

Both my fields above are generated with python code but email field is applying template css properly. But "Phone" field is not rendering css.

Please advise.

Super Kai - Kazuya Ito
  • 22,221
  • 10
  • 124
  • 129
Shazia Nusrat
  • 174
  • 1
  • 10
  • 36

3 Answers3

6

forms.IntegerField is not a widget but a field type on form.

What you need is a forms.NumberInput

phone = forms.CharField(widget=forms.NumberInput)  
AKS
  • 18,983
  • 3
  • 43
  • 54
  • By your recommendation see above what I am getting. It is not getting my css instead its giving me a field that is not inheriting css at all. I've pasted the result as snapshot in original question above. – Shazia Nusrat Mar 08 '16 at 05:09
  • Can you also paste the HTML for the `phone` field and other fields as well? And, also how are you rendering this form in the view? – AKS Mar 08 '16 at 05:11
  • – Shazia Nusrat Mar 08 '16 at 05:16
  • 1
    Please update this in question so it is available for everyone. And, please also include the css for both the inputs `email` and `phone` fields. – AKS Mar 08 '16 at 05:18
  • 1
    Please add the css as well. Because now your form is rendering and just the css for two fields is different. It could very well be that the css for `input[type=number]` is not set as it has been for other type of fields. – AKS Mar 08 '16 at 09:55
1

You can use

phone = forms.CharField(widget=forms.TextInput(
      attrs={
          'type' : 'number',
          'class' : 'your_class'
      }))
DJeanCar
  • 1,463
  • 1
  • 11
  • 13
0

You assign the form field forms.IntegerField to "widget" which is wrong:

class ContactForm(forms.Form):
    # ...                               # Wrong
    phone = forms.CharField(widget=forms.IntegerField)
    # ...

So, instead, you need to assign the widget forms.NumberInput to "widget" which is correct:

class ContactForm(forms.Form):
    # ...                               # Correct
    phone = forms.CharField(widget=forms.NumberInput)
    # ...
Super Kai - Kazuya Ito
  • 22,221
  • 10
  • 124
  • 129