#31084: ValueError: could not convert string to float:
------------------------------------------------+------------------------
               Reporter:  Sevdimali             |          Owner:  nobody
                   Type:  Cleanup/optimization  |         Status:  new
              Component:  Uncategorized         |        Version:  2.2
               Severity:  Normal                |       Keywords:
           Triage Stage:  Unreviewed            |      Has patch:  0
    Needs documentation:  0                     |    Needs tests:  0
Patch needs improvement:  0                     |  Easy pickings:  0
                  UI/UX:  0                     |
------------------------------------------------+------------------------
 Hi,
 **I have a field like this(models.py):**

 {{{
 value = models.FloatField(null=True)
 }}}

 **And I have a simple form (template/form.html)**

 {{{
  <form action="{% url 'submit_form' %}" method="post">
     {% csrf_token %}
     <input type="hidden" name="star" value="">
     <input type="submit">
 </form>
 }}}

 **in my view, I am saving this form (views.py) :**


 {{{
  def submit_form(request):
     if request.method == "POST":
         print("aa")
         score = request.POST.get('star')

         simpleform.objects.create(
             value=score,
         )
     return redirect("/form")
 }}}

 **and of course, I am getting this exception:**

 {{{
 ValueError: could not convert string to float:
 }}}

 **Adding if statement something like this to view will solve the
 exception:**

 {{{
    if len(score) == 0:
             score = None
 }}}

 **I am thinking that maybe we can add this check to our  site-
 packages/django/db/fields/__init.py__**

 {{{
     def get_prep_value(self, value):
         value = super().get_prep_value(value)
         if value is None:
             return None
         return float(value)
 }}}
 to

 {{{
  def get_prep_value(self, value):
         value = super().get_prep_value(value)
         if value is None or len(value) == 0:
             return None
         return float(value)
 }}}

-- 
Ticket URL: <https://code.djangoproject.com/ticket/31084>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

-- 
You received this message because you are subscribed to the Google Groups 
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-updates+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-updates/052.ff1d5799c833c2f61b2dc291a813963f%40djangoproject.com.

Reply via email to