from django import forms
from django.forms.models import formset_factory, BaseModelFormSet
from django.forms.widgets import NumberInput

from .models import Reading

class ReadingForm(forms.Form):
    ships_head = forms.FloatField(required=False, widget=NumberInput(attrs={'style': "width: 5em;"}))
    deviation  = forms.FloatField(required=False, widget=NumberInput(attrs={'style': "width: 5em;"}))

ReadingFormSet = formset_factory(form=ReadingForm)

from django import forms
from django.forms.models import formset_factory, BaseModelFormSet
from django.forms.widgets import NumberInput

from .models import Reading

class DegreeInput(NumberInput):

    """Set the default style"""
    def __init__(self, attrs=None):
        if attrs is None:
            attrs = {}
        attrs['style'] = "width: 5em;"

        super(DegreeInput, self).__init__(attrs)

    """Strip decimal points if not needed"""
    def _format_value(self, value):
        return u"%g" % value


class ReadingForm(forms.Form):
    ships_head = forms.FloatField(
        required=False,
        widget=DegreeInput()
    )

    deviation = forms.FloatField(
        required=False,
        widget=DegreeInput()
    )

ReadingFormSet = formset_factory(form=ReadingForm)

Customize degree widget so that if formats floats more elegantly
Customize degree widget so that if formats floats more elegantly

  from django import forms
  from django.forms.models import formset_factory, BaseModelFormSet
  from django.forms.widgets import NumberInput
  
  from .models import Reading
  
+ class DegreeInput(NumberInput):
+ 
+     """Set the default style"""
+     def __init__(self, attrs=None):
+         if attrs is None:
+             attrs = {}
+         attrs['style'] = "width: 5em;"
+ 
+         super(DegreeInput, self).__init__(attrs)
+ 
+     """Strip decimal points if not needed"""
+     def _format_value(self, value):
+         return u"%g" % value
+ 
+ 
  class ReadingForm(forms.Form):
-     ships_head = forms.FloatField(required=False, widget=NumberInput(attrs={'style': "width: 5em;"}))
-     deviation  = forms.FloatField(required=False, widget=NumberInput(attrs={'style': "width: 5em;"}))
+     ships_head = forms.FloatField(
+         required=False,
+         widget=DegreeInput()
+     )
+ 
+     deviation = forms.FloatField(
+         required=False,
+         widget=DegreeInput()
+     )
  
  ReadingFormSet = formset_factory(form=ReadingForm)
  
Customize degree widget so that if formats floats more elegantly
## Code Before:
from django import forms
from django.forms.models import formset_factory, BaseModelFormSet
from django.forms.widgets import NumberInput

from .models import Reading

class ReadingForm(forms.Form):
    ships_head = forms.FloatField(required=False, widget=NumberInput(attrs={'style': "width: 5em;"}))
    deviation  = forms.FloatField(required=False, widget=NumberInput(attrs={'style': "width: 5em;"}))

ReadingFormSet = formset_factory(form=ReadingForm)

## Instruction:
Customize degree widget so that if formats floats more elegantly
## Code After:
from django import forms
from django.forms.models import formset_factory, BaseModelFormSet
from django.forms.widgets import NumberInput

from .models import Reading

class DegreeInput(NumberInput):

    """Set the default style"""
    def __init__(self, attrs=None):
        if attrs is None:
            attrs = {}
        attrs['style'] = "width: 5em;"

        super(DegreeInput, self).__init__(attrs)

    """Strip decimal points if not needed"""
    def _format_value(self, value):
        return u"%g" % value


class ReadingForm(forms.Form):
    ships_head = forms.FloatField(
        required=False,
        widget=DegreeInput()
    )

    deviation = forms.FloatField(
        required=False,
        widget=DegreeInput()
    )

ReadingFormSet = formset_factory(form=ReadingForm)
