gpt4 book ai didi

python - Django 错误信息 : ["' on' value must be either True or False."]

转载 作者:搜寻专家 更新时间:2023-10-31 22:53:57 25 4
gpt4 key购买 nike

我目前在 django 上收到此错误消息:["'on' value must be either True or 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)

表单.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'}

模型.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 而不是硬编码的,我似乎无法弄清楚

我真的无法理解如何修复这个错误

最佳答案

我能够使用下面的代码解决这个问题:

if is_retired == 'on':
is_retired = True
else:
is_retired = False

我发现我已将 is_retired 声明为 Boolean 字段,但在我的项目中我分配了一个 string 值为 'on' 或 'off'。

作为解决方案,我将字符串值转换为 bool 值。

关于python - Django 错误信息 : ["' on' value must be either True or False."],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50321297/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com