why i am having getting this error ? ["“('1',)” value must be either True, False, or None."] did i miss something in my code? i am trying to update the data and i receive this error.
This is my html
<input type="checkbox" name="Ubo" value="1" {% if feedback.q5Ubo %}checked="check" {% endif %}>
<input type="checkbox" name="Lagnat" value="1" {% if feedback.q5Lagnat %}checked="check" {% endif %}>
<input type="checkbox" name="Sipon" value="1" {% if feedback.q5Sipon %}checked="check" {% endif %} >
<input type="checkbox" name="Lbm" value="1" {% if feedback.q5LBM %}checked="check" {% endif %}>
This is my views.py
Ubo = request.POST.get('Ubo') or 0
Lagnat = request.POST.get('Lagnat') or 0
Sipon = request.POST.get('Sipon') or 0
Lbm = request.POST.get('Lbm') or 0
this is my models.py
q5Ubo = models.BooleanField(default=False, blank=True, null=True)
q5Lagnat = models.BooleanField(default=False, blank=True, null=True)
q5Sipon = models.BooleanField(default=False, blank=True, null=True)
q5LBM = models.BooleanField(default=False, blank=True, null=True)
UPDATE i change my html and views.py
html
<input type="checkbox" name="Ubo" value=True>
<input type="checkbox" name="Lagnat" value=True >
<input type="checkbox" name="Sipon" value=True >
<input type="checkbox" name="Lbm" value=True >
views.py
Ubo = bool(request.POST.get('Ubo')) or False
Lagnat = bool(request.POST.get('Lagnat')) or False
Sipon = bool(request.POST.get('Sipon')) or False
Lbm = bool(request.POST.get('Lbm')) or False
this is the traceback
Traceback (most recent call last):
File "/home/MyHealth/contacttracingapp/ctenv/lib/python3.8/site-packages/django/core/handlers/exception.py", line 47, in inner
response = get_response(request)
File "/home/MyHealth/contacttracingapp/ctenv/lib/python3.8/site-packages/django/core/handlers/base.py", line 179, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/home/MyHealth/contacttracingapp/TracingSettings/TracingApp/views.py", line 865, in Data
insert_2tab.save()
File "/home/MyHealth/contacttracingapp/ctenv/lib/python3.8/site-packages/django/db/models/base.py", line 753, in save
self.save_base(using=using, force_insert=force_insert,
File "/home/MyHealth/contacttracingapp/ctenv/lib/python3.8/site-packages/django/db/models/base.py", line 790, in save_base
updated = self._save_table(
File "/home/MyHealth/contacttracingapp/ctenv/lib/python3.8/site-packages/django/db/models/base.py", line 872, in _save_table
updated = self._do_update(base_qs, using, pk_val, values, update_fields,
File "/home/MyHealth/contacttracingapp/ctenv/lib/python3.8/site-packages/django/db/models/base.py", line 926, in _do_update
return filtered._update(values) > 0
File "/home/MyHealth/contacttracingapp/ctenv/lib/python3.8/site-packages/django/db/models/query.py", line 803, in _update
return query.get_compiler(self.db).execute_sql(CURSOR)
File "/home/MyHealth/contacttracingapp/ctenv/lib/python3.8/site-packages/django/db/models/sql/compiler.py", line 1522, in execute_sql
cursor = super().execute_sql(result_type)
File "/home/MyHealth/contacttracingapp/ctenv/lib/python3.8/site-packages/django/db/models/sql/compiler.py", line 1143, in execute_sql
sql, params = self.as_sql()
File "/home/MyHealth/contacttracingapp/ctenv/lib/python3.8/site-packages/django/db/models/sql/compiler.py", line 1488, in as_sql
val = field.get_db_prep_save(val, connection=self.connection)
File "/home/MyHealth/contacttracingapp/ctenv/lib/python3.8/site-packages/django/db/models/fields/__init__.py", line 823, in get_db_prep_save
return self.get_db_prep_value(value, connection=connection, prepared=False)
File "/home/MyHealth/contacttracingapp/ctenv/lib/python3.8/site-packages/django/db/models/fields/__init__.py", line 818, in get_db_prep_value
value = self.get_prep_value(value)
File "/home/MyHealth/contacttracingapp/ctenv/lib/python3.8/site-packages/django/db/models/fields/__init__.py", line 967, in get_prep_value
return self.to_python(value)
File "/home/MyHealth/contacttracingapp/ctenv/lib/python3.8/site-packages/django/db/models/fields/__init__.py", line 957, in to_python
raise exceptions.ValidationError(
Exception Type: ValidationError at /Data/
Exception Value: ['“(False,)” value must be either True, False, or None.']
this is my full views.py
q1 = request.POST.get('q1')
q2 = request.POST.get('q2')
q2Reason = request.POST.get('q2Reason')
q3 = request.POST.get('q3')
q4 = request.POST.get('q4')
q4Reason = request.POST.get('q4AnswerDetailed')
q5 = request.POST.get('q5')
q6visitedPlacesDuringNonWorkingDays = request.POST.get('q6visitedPlacesDuringNonWorkingDays')
Ubo = bool(request.POST.get('Ubo')) or False
Lagnat = bool(request.POST.get('Lagnat')) or False
Sipon = bool(request.POST.get('Sipon')) or False
Lbm = bool(request.POST.get('Lbm')) or False
paghinga = bool(request.POST.get('paghinga')) or False
Pananakit = bool(request.POST.get('Pananakit')) or False
katawan = bool(request.POST.get('katawan')) or False
Pagkawala = bool(request.POST.get('Pagkawala')) or False
insert_2tab = TrEmployeeSuppliersFeedbackQuestionsSubmittedRecords(
dateSubmitted=datenow,
fmCustomerID=company,
fmCustomerEmployeeSupplierID=fmCustomerEmployeeSupplierID,
q1Answer=q1,
q2Answer=q2,
q2AnswerDetailed=q2Reason,
q3Answer=q3,
q4Answer=q4,
q4AnswerDetailed=q4Reason,
q5Answer=q5,
q5Ubo=Ubo,
q5Lagnat=Lagnat,
q5Sipon=Sipon,
q5LBM=Lbm,
q5HirapSaPaghinga=paghinga,
q5PananakitngUlo=Pananakit,
q5PananakitngKatawan=katawan,
q5PagkawalaNgPangAmoyatPanlasa=Pagkawala
)
insert_2tab.save()
I'm not exactly sure why your "hand written form" isn't working, and I suspect most other users don't either, because you shouldn't write forms by hand !
It will be much easier for both of us , if I show you how to use a form with your setup.
In a file traditionally called forms.py
, create a new form class for your model:
from django import forms
from .models import TrEmployeeSuppliersFeedbackQuestionsSubmittedRecords as ThisNameIsTooLong
class ThisNameIsTooLongForm(forms.ModelForm):
class Meta:
model = ThisNameIsTooLong
fields = '__all__'
Now, in your views.py
:
from django.shortcuts import render, redirect
form django.core.exceptions import ValidationError, SuspiciousOperation
from .forms import ThisNameIsTooLongForm
def my_view(request):
form = ThisNameIsTooLongForm(request.POST or None)
if request.method == 'GET':
context = {'form': form}
return render(request, 'my_template.html', context)
if request.method == 'POST':
if form.is_valid():
form.save()
# handle valid form here. eg:
return redirect('some_view')
else:
# handle invalid form
raise ValidationError('form was invalid')
# method was neither "GET" nor "POST", raise a 405: Method Not Allowed
raise SuspiciousOperation(405)
Finally, in my_template.html
, you will need to render the form via the form object passed into the template context:
<form method="POST"> {% csrf_token %}
{{ form.as_ul }}
<button>save</button>
</form>
I'm not sure where your feedback
object is coming from, so I left out the programmatic toggling of the form input's value. If you want the same functionality that your current setup has, you will need to find a way to pass the feedback
to the form's __init__
, for example:
feedback = {'q5Ubo': True}
form = ThisNameIsTooLongForm(request.POST or None, feedback=feedback)
And in your form class:
class ThisNameIsTooLongForm(forms.ModelForm):
class Meta:
model = ThisNameIsTooLong
fields = '__all__'
def __init__(self, *args, feedback=None, **kwargs):
super().__init__(*args, **kwargs)
if feedback is not None:
for key, value in feedback.items():
self.initial[key] = value
You should really give the form docs
a read if you haven't already.
You have created the field as Boolean. So it should return either True
or False
. Also, it will accept None
, because you have added null = True.
But the value you are currently getting from HTML is a string. You have to convert it into boolean.
Ubo = bool(request.POST.get('Ubo'))
Updates: As per @ElrondSupportsMonica, or False
is not needed. .get(key, default=None, /)
will return the key, if the key is there, else it will return the default value. For the default value, we have None
. bool(None)
will be False. So or False is not needed.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.