一个页面上有多个表单
我对 Django 比较陌生,并尝试创建一个页面,我在其中提出问题并允许用户给出两个答案。所以我想显示问题列表以及下面两个猜测的字段。我尝试使用表单进行制作,但无法保存或验证从这些表单获取的数据。 下面是我的相关代码。
来自models.py:
class Subject(models.Model):
subject = models.FloatField(verbose_name='ID')
class SubjectForm(forms.Form):
subject = forms.FloatField()
class General(models.Model):
question = models.CharField(max_length=400)
answer = models.FloatField()
class Guess(models.Model):
general = models.ForeignKey(General)
subject = models.ForeignKey(Subject)
lower = models.FloatField('lower boundary')
upper = models.FloatField('upper boundary')
class GuessForm(forms.Form):
lower = forms.FloatField()
upper = forms.FloatField()
来自views.py:
def questions(request):
if request.method == 'POST': # If answers were submitted
for question in all_questions:
form = GuessForm()
if form.is_valid():
lower = request.POST.get('lower','')
upper = request.POST.get('upper','')
new_guess = Guess(pk = 'question',
subject = subject,
lower = lower,
upper = upper)
new_quess.save()
return HttpResponseRedirect('/general_knowledge/questions/thanks/')
else:
form = GuessForm()
csrfContext = RequestContext(request,{'form': form})
return render_to_response('general_knowledge/question_form.html',
{'all_questions': all_questions},
context_instance=RequestContext(request))
来自模板
{% for question in all_questions %}
<form action="" method="post">{% csrf_token %}
<ol>
<p><LI> {{ question.question }}.</p>
<label for="lower">Lower boundary:</label><input type="text" name="lower">
{{ form.lower.errors }}
<label for="upper">Upper boundary: </label><input type="text" name="upper">
{{ form.upper.errors }}
</ol>
</form>
{% endfor %}
<input type="submit" value="Submit" />
I'm relatively new to Django and trying to create a page, where I ask questions and allow users to give two answer. So I want to display a list of questions, and fields for two guesses below. I tried to make using form, but I can't save or validate the data I get from those forms.
Below if my relevant code.
From models.py:
class Subject(models.Model):
subject = models.FloatField(verbose_name='ID')
class SubjectForm(forms.Form):
subject = forms.FloatField()
class General(models.Model):
question = models.CharField(max_length=400)
answer = models.FloatField()
class Guess(models.Model):
general = models.ForeignKey(General)
subject = models.ForeignKey(Subject)
lower = models.FloatField('lower boundary')
upper = models.FloatField('upper boundary')
class GuessForm(forms.Form):
lower = forms.FloatField()
upper = forms.FloatField()
From views.py:
def questions(request):
if request.method == 'POST': # If answers were submitted
for question in all_questions:
form = GuessForm()
if form.is_valid():
lower = request.POST.get('lower','')
upper = request.POST.get('upper','')
new_guess = Guess(pk = 'question',
subject = subject,
lower = lower,
upper = upper)
new_quess.save()
return HttpResponseRedirect('/general_knowledge/questions/thanks/')
else:
form = GuessForm()
csrfContext = RequestContext(request,{'form': form})
return render_to_response('general_knowledge/question_form.html',
{'all_questions': all_questions},
context_instance=RequestContext(request))
From template
{% for question in all_questions %}
<form action="" method="post">{% csrf_token %}
<ol>
<p><LI> {{ question.question }}.</p>
<label for="lower">Lower boundary:</label><input type="text" name="lower">
{{ form.lower.errors }}
<label for="upper">Upper boundary: </label><input type="text" name="upper">
{{ form.upper.errors }}
</ol>
</form>
{% endfor %}
<input type="submit" value="Submit" />
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以使用 JQuery 在提交时打印验证,如下所示:
You can use JQuery to print the validation when submit, something like this :