title: Django percentage field
body: <p>I'm trying to create a percentage field in Django, where the user just fills in 40 for 40%. There will be a percentage sign on the right of the input box so that they know they should fill in a percentage. 0.4 must be stored in the DB. So far I've tried the following:</p>
  
  <pre><code>class PercentageField(fields.FloatField):
      widget = fields.TextInput(attrs={"class": "percentInput"})
  
      def to_python(self, value):
          val = super(PercentageField, self).to_python(value)
          if is_number(val):
              return val/100
          return val
  
      def prepare_value(self, value):
          val = super(PercentageField, self).prepare_value(value)
          if is_number(val):
              return str((float(val)*100))
          return val
  
  def is_number(s):
      if s is None:
          return False
      try:
          float(s)
          return True
      except ValueError:
          return False
  </code></pre>
  
  <p>It works, but the problem is, when I post invalid data and the form is rendered again, it displays the 40 as 4000. In other words it multiplies the number again with 100 without dividing it as well.</p>
  
  <p>Any suggestions how I can fix it?</p>
  
  <p>I've tried <a href="https://djangosnippets.org/snippets/1914/" rel="noreferrer">this</a> solution, but it repeats the value 100 times. It also has the same problem after I've corrected that.</p>
  
  <p>I'm using Python3.5</p>