[Django] #31084: ValueError: could not convert string to float:

3 views
Skip to first unread message

Django

unread,
Dec 12, 2019, 11:34:33 AM12/12/19
to django-...@googlegroups.com
#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.

Django

unread,
Dec 12, 2019, 2:59:22 PM12/12/19
to django-...@googlegroups.com
#31084: ValueError: could not convert string to float.
-------------------------------------+-------------------------------------
Reporter: Sevdimali | Owner: nobody
Type: | Status: closed
Cleanup/optimization |
Component: Database layer | Version: 2.2
(models, ORM) |
Severity: Normal | Resolution: invalid

Keywords: | Triage Stage:
| Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by felixxm):

* status: new => closed
* resolution: => invalid
* component: Uncategorized => Database layer (models, ORM)


Comment:

You're trying to use a raw value from `POST` to create a new model
instance, in general that's not a proper way to do this. Please don't use
Trac as
[https://code.djangoproject.com/wiki/TicketClosingReasons/UseSupportChannels
a support channel].

Closing per TicketClosingReasons/UseSupportChannels.

--
Ticket URL: <https://code.djangoproject.com/ticket/31084#comment:1>

Reply all
Reply to author
Forward
0 new messages