2014-04-13 78 views
0

我是相當新的Django和Python,並有以下問題:Python的Django,提交多個表單

我有一個測驗與多個答案。 每個答案都有一個附加到它的表單,它將數據發佈到從模型創建對象的視圖。 但我不能發佈所有表格,因爲它只發布第五個。

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/') 

模板:

{% 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()" /> 
+0

請再檢查一下你的Python的縮進。另外,你可以發佈模板生成的HTML(它可能有助於看到如何提交生成) – BorrajaX

回答

0

我想你應該重新設計你的程序。我的理解是,form submission只能處理一種形式。以下是使用Django/Python的一些變通方法。但是,對於你的情況,如果你正在設計一個測驗,所有的答案應該是一個單一的形式的一部分。而不是返回{% 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" /> 
+0

http://stackoverflow.com/questions/15124567/how-do-i-submit-multiple-forms-with-a -single-submit-button-in-django是用Django提交多個表單的另一個例子。我投票重新設計。 – rofls