{{{
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.
* 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>