简体   繁体   English

Python Django,提交多种表单

[英]Python Django, submit multiple forms

i am fairly new to Django and Python and have the following problem: 我对Django和Python相当陌生,并且存在以下问题:

I have a quiz with multiple answers. 我有多个答案的测验。 Every answer has a form attached to it, which posts data to a view that creates an objects from a Model. 每个答案都附有一个表格,该表格将数据发布到从模型创建对象的视图中。 But i cant post all forms, because its only posting the 5th one. 但是我不能发布所有表格,因为它只发布了第5个表格。

views.py: views.py:

def quiz(request, quiz_id=1):
cs = {}
cs.update(csrf(request))

height=75*Frage.objects.count()
height_menu = height + 10
if Frage.objects.count() == 0:
    height = 170
    height_menu = height

len_quiz = len(Quiz.objects.get(id=quiz_id).title)
count=Frage.objects.filter(quiz=quiz_id).count
count_aw = 0
aw = Frage.objects.filter(quiz=quiz_id)

cs.update({'quiz': Quiz.objects.get(id=quiz_id),
                         'Frage': Frage.objects.filter(quiz=quiz_id),
                         'len': len_quiz,
                         'hg': height,
                         'hg_m': height_menu,
                         'user': request.user,
                         'aw': Antwort.objects.all(),
                         'count': count,
                         'count_aw': count_aw})
    return render_to_response('quiz.html', cs)

    def check_view(request):
    check = request.POST.get('aw_check', '')
    user_id = request.POST.get('user_log', '')
    user_act = User.objects.get(id=user_id)
    frage_id = request.POST.get('frage', '')
    frage = Frage.objects.get(id=frage_id)
    antwort_id = request.POST.get('antwort', '')
    antwort = Antwort.objects.get(id=antwort_id)
    richtig = request.POST.get('richtig', '')
    quiz_id = request.POST.get('quiz', '')
    quiz = Quiz.objects.get(id=quiz_id)
    res = Results(quiz=quiz, frage=frage, user=user_act, richtig=richtig, choice=check,    aw=antwort)
    res.save()
    return  HttpResponseRedirect('/quiz/all/')

template: 模板:

{% if Frage.count == 0 %}
    <br>
        <h1 id="main_link" style="text-align: center" align="center">Keine Fragen vorhanden!</h1>
    {% endif %}
<ol>
{% for Frage in Frage %}
    <li><div id="frage">{{Frage.content}}</div></li>
    {% for aw in Frage.antworten.all %}
        <form action="/quiz/check/" name="{{ aw.content }}" method="post">{% csrf_token %}
        <label for="aw_check">{{ aw.content }}</label>
        <input type="checkbox" name="aw_check" id="aw_check">
        <input type="hidden" name="user_log" value="{{ user.id }}">
        <input type="hidden" name="frage" value="{{ Frage.id }}">
        <input type="hidden" name="antwort" value="{{ aw.id }}">
        <input type="hidden" name="quiz" value="{{ quiz.id }}">

        {% if aw.richtig %}
        <input type="hidden" name="richtig" value=True>
        {% else %}
        <input type="hidden" name="richtig" value=False>
        {% endif %}
        <input type="submit" value="Abgeben" />
        <br>

</form>

{% endfor %}
{% endfor %}


</ol>


<script language="javascript">
function call_submitforms(){
{% for Frage in Frage %}
{% for aw in Frage.antworten.all %}
setTimeout("document.{{ aw.content }}.submit()",1000);
    {{ check.it }}
{% endfor %}
{% endfor %}
}
</script>

<input type="button" value="Abschicken" onclick="call_submitforms()" />

I think you should redesign your program. 我认为您应该重新设计程序。 My understanding is that form submission can only handle one form. 我的理解是, 表单提交只能处理一种表单。 Here are some work-arounds using Django/Python . 以下是使用Django / Python的一些解决方法。 But, for your case, if you're designing a quiz, all of the answers should be part of a single form. 但是,就您的情况而言,如果您要设计测验,则所有答案都应属于一种形式。 Instead of returning {% for Frage in Frage %} I would do something like this (you'll have to redesign your models and views first): 而不是返回{% for Frage in Frage %}我会这样做(您必须首先重新设计模型和视图):

//one form contains all questions

form action="/quiz/check/" name="{{ aw.content }}" method="post">{% csrf_token %}

//list the questions as part of that form
{%for Frage in quiz.Frages%}
    <input type="hidden" name="user_log" value="{{ user.id }}">
    <input type="hidden" name="antwort" value="{{ aw.id }}">
    <input type="hidden" name="quiz" value="{{ frage.id }}">
{% endfor %}
//submit all of the data for each page of the quiz in one form
      <input type="hidden" name="richtig" value=True>
    {% else %}
    <input type="hidden" name="richtig" value=False>
    {% endif %}
    <input type="submit" value="Abgeben" />

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

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