我目前在django上收到此错误消息:[“'on'值必须为True或False。”]
views.py
from django.shortcuts import render, redirect
from .models import Comment
from .forms import sign
def index(request):
return render(request, 'submits/index.html')
def sign_list(request):
if request.method == 'POST':
form = sign(request.POST)
if form.is_valid():
new_contact = Comment(email=request.POST['email'], message=request.POST['message'], checkbox=request.POST['checkbox'])
new_contact.save()
return redirect('index')
else:
form = sign()
context = {'form' : form}
return render(request, 'submits/sign.html', context)
forms.py
from django import forms
from .models import Comment
class sign(forms.Form):
email = forms.CharField(max_length=500,
widget=forms.TextInput(attrs={ 'type' : 'email', 'class' : 'form-control', 'placeholder' : 'Enter your email', 'aria-describedby' : 'emailHelp'}))
message = forms.CharField(widget=forms.Textarea(attrs={ 'class' : 'form-control', 'rows' : '3', 'placeholder' : 'Write your proposal'}))
checkbox = forms.BooleanField(required=False, initial=True)
checkbox_attributes={'class': 'form-check'}
models.py
from django.db import models
# Create your models here.
class Comment(models.Model):
email = models.CharField(max_length=500)
message = models.TextField()
checkbox = models.BooleanField()
def __str__(self):
return 'e-mail: {}, ID: {}'.format(self.email, self.id)
提供完整的追溯
Environment:
Request Method: POST
Request URL: http://127.0.0.1:8000/contact_us/
Django Version: 2.0.3
Python Version: 3.5.2
Installed Applications:
['submits',
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles']
Installed Middleware:
['django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware']
Traceback:
File "/home/joaomjaneiro/django/venv/lib/python3.5/site-packages/django/core/handlers/exception.py" in inner
35. response = get_response(request)
File "/home/joaomjaneiro/django/venv/lib/python3.5/site-packages/django/core/handlers/base.py" in _get_response
128. response = self.process_exception_by_middleware(e, request)
File "/home/joaomjaneiro/django/venv/lib/python3.5/site-packages/django/core/handlers/base.py" in _get_response
126. response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/media/joaomjaneiro/LEXAR/django projects/love/submits/views.py" in sign_list
14. new_contact.save()
File "/home/joaomjaneiro/django/venv/lib/python3.5/site-packages/django/db/models/base.py" in save
729. force_update=force_update, update_fields=update_fields)
File "/home/joaomjaneiro/django/venv/lib/python3.5/site-packages/django/db/models/base.py" in save_base
759. updated = self._save_table(raw, cls, force_insert, force_update, using, update_fields)
File "/home/joaomjaneiro/django/venv/lib/python3.5/site-packages/django/db/models/base.py" in _save_table
842. result = self._do_insert(cls._base_manager, using, fields, update_pk, raw)
File "/home/joaomjaneiro/django/venv/lib/python3.5/site-packages/django/db/models/base.py" in _do_insert
880. using=using, raw=raw)
File "/home/joaomjaneiro/django/venv/lib/python3.5/site-packages/django/db/models/manager.py" in manager_method
82. return getattr(self.get_queryset(), name)(*args, **kwargs)
File "/home/joaomjaneiro/django/venv/lib/python3.5/site-packages/django/db/models/query.py" in _insert
1125. return query.get_compiler(using=using).execute_sql(return_id)
File "/home/joaomjaneiro/django/venv/lib/python3.5/site-packages/django/db/models/sql/compiler.py" in execute_sql
1282. for sql, params in self.as_sql():
File "/home/joaomjaneiro/django/venv/lib/python3.5/site-packages/django/db/models/sql/compiler.py" in as_sql
1235. for obj in self.query.objs
File "/home/joaomjaneiro/django/venv/lib/python3.5/site-packages/django/db/models/sql/compiler.py" in <listcomp>
1235. for obj in self.query.objs
File "/home/joaomjaneiro/django/venv/lib/python3.5/site-packages/django/db/models/sql/compiler.py" in <listcomp>
1234. [self.prepare_value(field, self.pre_save_val(field, obj)) for field in fields]
File "/home/joaomjaneiro/django/venv/lib/python3.5/site-packages/django/db/models/sql/compiler.py" in prepare_value
1174. value = field.get_db_prep_save(value, connection=self.connection)
File "/home/joaomjaneiro/django/venv/lib/python3.5/site-packages/django/db/models/fields/__init__.py" in get_db_prep_save
767. return self.get_db_prep_value(value, connection=connection, prepared=False)
File "/home/joaomjaneiro/django/venv/lib/python3.5/site-packages/django/db/models/fields/__init__.py" in get_db_prep_value
762. value = self.get_prep_value(value)
File "/home/joaomjaneiro/django/venv/lib/python3.5/site-packages/django/db/models/fields/__init__.py" in get_prep_value
1024. return self.to_python(value)
File "/home/joaomjaneiro/django/venv/lib/python3.5/site-packages/django/db/models/fields/__init__.py" in to_python
1017. params={'value': value},
Exception Type: ValidationError at /contact_us/
Exception Value: ["'on' value must be either True or False."]
在HTML文件中,我有forms.checkbox,而不是硬编码的,我似乎无法弄明白
我真的无法理解如何修复此错误
随机发送文字,jajajajajajajajajajajaj
答案 0 :(得分:5)
当前,当我保存员工对象时遇到同样的问题。
我可以使用以下代码解决问题。
if is_retired == 'on':
is_retired = True
else:
is_retired = False
我发现我已将is_retired声明为布尔字段,但是在我的项目中,我将字符串值分配为“ on”或“ off”,因此只要求设置布尔值。
因此,作为解决方案,我可以通过将字符串值转换为布尔值来解决问题。
希望上述解决方案将为您服务!