1

我正在尝试制作一个表单来上传文件,选择多个类别和一个 BooleanField 来将文件标记为项目或否,这个布尔值的小部件是一个选择是或否的选择。我的问题是当 BooleanField 值为 False 时我无法进行保存(),我尝试过不使用自定义小部件,并且 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

视图.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 值时,无法保存,并且收到消息“文件无法保存”。

4

1 回答 1

1

在模型中,您应该定义布尔字段,例如models.BooleanField(blank=True). 但是,当您使用ModelForm. 如果您需要常规表单的相同行为,则将该字段定义为forms.BooleanField(required=False).

于 2013-03-31T17:43:10.277 回答