一个页面上有多个表单

发布于 2025-01-03 06:15:24 字数 2306 浏览 0 评论 0原文

我对 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

执笔绘流年 2025-01-10 06:15:24

您可以使用 JQuery 在提交时打印验证,如下所示:

$("input[type="submit"]).click(function () {
        if ($("#lower").text() == // some condition
        {
            $("#validation").show();
        }
        else {
            $("Form").submit();
        }
    });

You can use JQuery to print the validation when submit, something like this :

$("input[type="submit"]).click(function () {
        if ($("#lower").text() == // some condition
        {
            $("#validation").show();
        }
        else {
            $("Form").submit();
        }
    });
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文