Django - 将表单内容保存到数据库中
Django - Saving contents of a form into a database
class InitialForm(forms.Form):
Teacher_Name = forms.CharField(label='Teacher Name')
Subject = forms.CharField(label = 'Subject')
Question = forms.CharField(label = 'What is the first question?')
Topic = 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?')
Answer_Q = forms.CharField(label = 'Which option is the correct option?', widget=forms.Select(choices=Options))
class Questions(models.Model):
testID = AutoSlugField(unique=True)
teacherID = models.ForeignKey(Teacher, on_delete=models.CASCADE)
studentID = models.ForeignKey(Student, on_delete=models.CASCADE)
Subject = models.CharField(max_length=1000)
Q = models.CharField(max_length=1000)
Topic = 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)
AnswerQ = models.CharField(max_length=1000)
def teachertests(request):
form = InitialForm()
if request.method == "POST":
form = InitialForm(request.POST)
form.save()
return render(request, 'teachertests.html', {'form':form})
嘿,一般的编码新手,想将表单的内容保存到数据库中,然后显示表单中的特定字段,不确定我做错了什么,有人可以帮忙吗?
当您使用表单时,添加 form.is_valid() 很重要,因为从您所看到的情况来看,您没有在表单中指明是否存在 require=False 不强制要求的字段,只要因为它无效,所以不会被保存
def teachertests(request):
form = InitialForm()
if request.method == "POST":
form = InitialForm(request.POST)
if form.is_valid():
data_form = form.cleaned_data
question = Question()
question.Option1_Q = data_form.get('Option1_Q')
# ....... all fields form
question.save()
return render(request, 'teachertests.html', {'form':form})
class InitialForm(forms.Form):
Teacher_Name = forms.CharField(label='Teacher Name')
Subject = forms.CharField(label = 'Subject')
Question = forms.CharField(label = 'What is the first question?')
Topic = 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?')
Answer_Q = forms.CharField(label = 'Which option is the correct option?', widget=forms.Select(choices=Options))
class Questions(models.Model):
testID = AutoSlugField(unique=True)
teacherID = models.ForeignKey(Teacher, on_delete=models.CASCADE)
studentID = models.ForeignKey(Student, on_delete=models.CASCADE)
Subject = models.CharField(max_length=1000)
Q = models.CharField(max_length=1000)
Topic = 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)
AnswerQ = models.CharField(max_length=1000)
def teachertests(request):
form = InitialForm()
if request.method == "POST":
form = InitialForm(request.POST)
form.save()
return render(request, 'teachertests.html', {'form':form})
嘿,一般的编码新手,想将表单的内容保存到数据库中,然后显示表单中的特定字段,不确定我做错了什么,有人可以帮忙吗?
当您使用表单时,添加 form.is_valid() 很重要,因为从您所看到的情况来看,您没有在表单中指明是否存在 require=False 不强制要求的字段,只要因为它无效,所以不会被保存
def teachertests(request):
form = InitialForm()
if request.method == "POST":
form = InitialForm(request.POST)
if form.is_valid():
data_form = form.cleaned_data
question = Question()
question.Option1_Q = data_form.get('Option1_Q')
# ....... all fields form
question.save()
return render(request, 'teachertests.html', {'form':form})