0

i am fairly new to Django and Python and have the following problem:

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.

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()" />
vale981
  • 1
  • 1
  • please chech your python's indentation. Also, could you post the HTML generated by the template (it may help see how the submits are generated) – BorrajaX Apr 13 '14 at 17:47

1 Answers1

0

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. 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):

//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" />
Community
  • 1
  • 1
rofls
  • 4,587
  • 3
  • 21
  • 33
  • http://stackoverflow.com/questions/15124567/how-do-i-submit-multiple-forms-with-a-single-submit-button-in-django is another example of submitting multiple forms with Django. I vote redesign though. – rofls Apr 13 '14 at 18:00