gpt4 book ai didi

python - 表单中的 BooleandField 不能为 False

转载 作者:太空宇宙 更新时间:2023-11-03 19:02:17 25 4
gpt4 key购买 nike

我正在尝试制作一个表单来上传文件,选择多个类别和一个 bool 字段来将文件标记为项目或否,此 bool 值的小部件是一个选择,可以选择是或否。我的问题是当 BooleanField 值为 False 时我无法进行 save(),我尝试过不使用自定义小部件,并且 CheckBox 也失败。如果我做错了什么,请告诉我。

模型.py

class file(models.Model):
def url(self,filename):
ruta = "files/%s/%s"%(self.nombre.str(filename))
return ruta
name = models.CharField(max_length=200)
description = models.TextField(max_length=300)
user = models.ForeignKey(User)
file = models.FileField(upload_to='files')
status = models.BooleanField(default=False)
project = models.BooleanField()
main_category = models.ForeignKey(main_category)
category1 = models.ManyToManyField(category1, related_name='files')
category2 = models.ManyToManyField(category2, related_name='files')
def __unicode__(self):
fileinfo = "%s %s"%(self.name,self.file)
return fileinfo

表单.py

choices = ( (1,'Yes'),
(0,'No'),
)

class uploadfileForm(forms.Form):
name = forms.CharField(widget=forms.TextInput(attrs={'style':'width:450px'}),initial="Name")
description = forms.CharField(widget=forms.Textarea(attrs={'style':'width:450px'}),initial="Description for your file")
file = forms.FileField(widget=forms.FileInput())
main_category = forms.ModelChoiceField(queryset=main_category.objects.all(),widget=forms.Select())
category1 = forms.ModelMultipleChoiceField(queryset=category1.objects.all(),widget=forms.SelectMultiple(attrs={'style':'width:218px; height:90px'}),required=True)
category2 = forms.ModelMultipleChoiceField(queryset=category2.objects.all(),widget=forms.SelectMultiple(attrs={'style':'width:218px; height:90px'}),required=False)
project = forms.BooleanField(widget=forms.Select(choices=choices))
def clean(self):
return self.cleaned_data

View .py

def upload_file_view(request):
info = "Starting"
if request.user.is_authenticated():
if request.method == "POST":
form = uploadfileForm(request.POST, request.FILES)
if form.is_valid():
name = form.cleaned_data['name']
description = form.cleaned_data['description']
file = request.FILES['file']
category1 = form.cleaned_data['category1']
category2 = form.cleaned_data['category2']
main_category = form.cleaned_data['main_category']
project = form.cleaned_data['project']
user = request.user
p = file()
p.user = user
p.main_category = main_category
p.save()
p.name = name
p.description = description
p.project = project
p.file = file
p.category1 = category1
p.category2 = category2
p.status = True
p.save()
info = "File saved"
else:
info = "File cant be saved"
form = uploadfileForm()
ctx = {'form':form, 'info':info}
return render_to_response('files/uploadfile.html',ctx,context_instance=RequestContext(request))
else:
return HttpResponseRedirect('/')

我在模板中显示变量信息,当我对项目使用 False 值时,无法保存,并且收到消息“文件无法保存”。

最佳答案

在模型中,您应该定义 bool 字段,例如 models.BooleanField(blank=True)。然而,只有当您使用 ModelForm 时,这才适用于表单。如果您需要常规表单的相同行为,请将字段定义为 forms.BooleanField(required=False)

关于python - 表单中的 BooleandField 不能为 False,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15731209/

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