Can’t be answered several questions in my form

I create a function in my views “def save_reply” that allows me to retrieve the answer of a question for a given patient. Now I would like it done with many submit questions in my form because my function only works when I have a question and answer but when I have some questions it does not work!
My problem is that it gets me every time the answer only the last question, for example if I have 5 questions and 5 answers they will register in my Reply models 5 times the last answer! I would like for each question he records his response associated with it! That is, if I have 5 questions and 5 answers I would like for each question he recorded me his response that corresponds to it!
Thank you for your help!

This is my models.py :

class Patient(models.Model):
    name = models.CharField(max_length=50)

    def __unicode__(self):
    return self.name + ' [' + str(self.id) + ']'


class Question(models.Model):
    question_text = models.CharField(max_length=200)
    pub_date = models.DateTimeField('date published')

    def __unicode__(self):
    return self.question_text


class Reply(models.Model):
    question = models.ForeignKey(Question)
    patient = models.ForeignKey(Patient)
    reply_text = models.CharField(max_length=200)

    def __unicode__(self):
        return self.reply_text

This is my views.py :

def save_reply(request, patient_id):
    list_patient = Patient.objects.order_by('name')
    context = RequestContext(request, {'list_patient':list_patient,'welcome': 'Welcome on my page Django !',})
    questions = Question.objects.all()
    patient = get_object_or_404(Patient, pk=patient_id)
    if request.method == 'POST':
        form = ReplyForm(request.POST)
        if form.is_valid():
            for question in questions:
                my_reply = form.cleaned_data['reply_'+question.id]
                post = Reply.objects.create(patient = patient, question = question, reply_text = my_reply)
                post.save()
                return render(request, 'PQR/list.html', context )
        else:
            form = ReplyForm()
            previous_reply = Reply.objects.filter(patient=patient_id).first
            return render_to_response('PQR/formDynamic.html', {'form': form, 'questions': questions, 'patient': patient, 'previous_reply': previous_reply, }, context_instance=RequestContext(request))

This is my forms.py :

from django import forms 
from PQR.models import Reply

class ReplyForm(forms.Form):
    class Meta:
        model = Reply
reply = forms.CharField(label='Reply ',max_length=100) 

And this is my template (formDynamic) :

{% load staticfiles %}
<link rel="stylesheet" type="text/css" href="{% static 'Forum/style.css' %}" />
<div>
    <h1>Patient details : {{ patient }}</h1>    
</div>
<form action="{% url 'detail_patient' patient.id %}" method="post">
{% csrf_token %}
<input type="hidden" value="{{ patient.id }}" />
<fieldset>
{% block content %}
    {% for question in questions %}
        <h2><abcd>{{ question.question_text }}</abcd>  <h6>{{ question.pub_date }}</h6></h2> 
        <!--{{ form }}
        <label>{{ question }}</label>
        <input id="id_reply_{{ question.id }}" type="text" name="reply_{{ question.id }}" maxlength="100" value="{{ previous_reply }}"></input> --> 
# I don't know what I have to put here enter <!--  -->
    {% endfor %}
{% endblock %}
</fieldset>
<input type="submit" value="Submit"/>
</form>
<a href="{% url 'list_patient' %}"/> <input type="button" value="Look Back"/>

How do I get my submit button either registers only one answer but many?


Source: forms

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.