简体   繁体   English

IntegrityError - NOT NULL 约束失败

[英]IntegrityError - NOT NULL constraint failed

def teachertests(request):
    form = InitialForm()
    if request.method == "POST":
            form = InitialForm(request.POST)
            if form.is_valid():
                data_form = form.cleaned_data
                question = Questions() 
                question.test_label = data_form.get('Test_Name')
                question.Q1 = data_form.get('Q1')
                question.Topic1 = data_form.get('Topic1')
                question.Option1_Q = data_form.get('Option1_Q')
                question.Option1_Q = data_form.get('Option2_Q')
                question.Option1_Q = data_form.get('Option3_Q')
                question.Option1_Q = data_form.get('Option4_Q')
                question.save()
    return render(request, 'teachertests.html', {'form':form})

class Questions(models.Model):
    testID = AutoSlugField(unique=True)
    test_label = models.CharField(max_length=1000)

    Q1 = models.CharField(max_length=1000)
    Topic1 = models.CharField(max_length=1000)
    Option1_Q = models.CharField(max_length=1000)
    Option2_Q = models.CharField(max_length=1000)
    Option3_Q = models.CharField(max_length=1000)
    Option4_Q = models.CharField(max_length=1000)
class InitialForm(forms.Form):
    Test_Name = forms.CharField(label='Label this test')
    Question1 = forms.CharField(label = 'What is the first question?')
    Topic1 = forms.CharField(label = 'What topic is this on?')
    Option1_Q = forms.CharField(label = 'What is the first option?')
    Option2_Q = forms.CharField(label = 'What is the second option?')
    Option3_Q = forms.CharField(label = 'What is the third option?')
    Option4_Q = forms.CharField(label = 'What is the fourth option?')

Hey, sorry if this is a simple issue, new to Django and any development like this.嘿,抱歉,如果这是一个简单的问题,Django 的新问题以及任何类似的开发。 When doing this, it gives me the error:这样做时,它给了我错误:

IntegrityError at /teacher/tests NOT NULL constraint failed: main_questions.Q1 /teacher/tests 处的 IntegrityError NOT NULL 约束失败:main_questions.Q1

Anyone able to help me out?任何人都可以帮助我吗? Willing to provide any information you would like.愿意提供您想要的任何信息。

Change this line in views.py teachertests function:在 views.py teachertests function 中更改此行:

question.Q1 = data_form.get('Q1')

To:到:

question.Q1 = data_form.get('Question1')

And apply the steps mentioned by @Ash Singh并应用@Ash Singh 提到的步骤

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM