提交表单时-验证错误:值错误

时间:2019-04-15 19:33:05

标签: django django-models django-forms django-views

我已经创建了一个用户登录表。我想保存用户提交的问题的答案。 提交答案表单时,Django抛出值错误

型号

class Day11(models.Model):
    user = models.OneToOneField(User, on_delete=models.CASCADE,null=True)
    ans = models.CharField(max_length=100, default=0)
    time = models.TimeField(auto_now=True)

表格

class Ans1(forms.ModelForm):
    ans = forms.FileField(required=True)
    def save(self, commit=False):
        answer = super(Ans1, self).save(commit=False)
        answer.ans = self.cleaned_data['ans']
        if commit:
            answer.save()
        return answer
    class Meta:
        model = Day11
        fields = ('ans',)
        exclude = ('user',)

查看

def email(request):
    print("test1")
    if request.method == "POST":
        form = Answer(request.POST)
        if form.is_valid():
            post = form.save()
            post.author = request.user
            form.save()
            return redirect('/')
        else:
            print("error")
    else:
        form = Answer()
    return render(request, 'day1/email_q1.html', {'ans1_1': form})

0 个答案:

没有答案