javascript - how to use single button in html forms to store the inputs from the for loop in django -
i working on quiz application , facing issue while submitting form. 1st question answer saving , answers of remaining questions not saving in database. can me in solving this. below code:
views.py:
import random def candidate_test(request): user=request.user profile=profile.objects.get(user=user) list=[] post=invidulator.objects.filter(candidate=profile) items in post: list.append(items) testnumber=list[-1] print testnumber.id questions=useranswer.objects.filter(invidulator=testnumber.id) print questions print questions.count() if request.method == 'post': form = answerform(request.post) print "inside" print form.is_valid() if form.is_valid(): print "profile" #candidate = form.save(commit=false) b=form.cleaned_data['answer'] a= form.cleaned_data['correct_answer'] print ("a",a) print ("b",b) answersave=useranswer.objects.get(id=a) answersave.answer=b answersave.save() return redirect(candidate_test) else: form = answerform() return render(request, 'blog/user.html', {'posts':questions,'form':form}) user.html:
{% q in posts %} <table> <form method="post" class="options" id="options" >{% csrf_token %} <ul> <div> <tr> <td> <p >{{q.question}}</p> <input class="option" type="radio" name="answer" value="option1" onclick="check();" />{{q.option1}}<br> <input class="option" type="radio" name="answer" value="option2" onclick="check();" />{{q.option2}}<br> <input class="option" type="radio" name="answer" value="option3" onclick="check();" />{{q.option3}}<br> <input class="option" type="radio" name="answer" value="option4" onclick="check();"/>{{q.option4}}</br> <input id="id_correct_answer" type="hidden" name="correct_answer" maxlength="100" value={{q.id}} required /></p> </td> </tr> </div> </ul> </form> {% endfor %} </table> <script> function check() { document.getelementbyid('options').submit(); } </script> {% endblock %} this code , want save answers of questions without using submit button , timer should not refreshed when submit each , every forms.
Comments
Post a Comment