IntegrityError - NOT NULL 约束失败
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?')
嘿,抱歉,如果这是一个简单的问题,对于 Django 和任何类似的开发来说都是新的。这样做时,它给我错误:
IntegrityError at /teacher/tests NOT NULL 约束失败:main_questions.Q1
有人能帮助我吗?愿意提供您想要的任何信息。
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. When doing this, it gives me the error:
IntegrityError at /teacher/tests
NOT NULL constraint failed: main_questions.Q1
Anyone able to help me out? Willing to provide any information you would like.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
将views.pyteachertests函数中的这一行更改
为
:并应用@Ash Singh提到的步骤
Change this line in views.py teachertests function:
To:
And apply the steps mentioned by @Ash Singh