python - Can't be answered several questions in my form -


i create function in views "def save_reply" allows me retrieve answer of question given patient. done many submit questions in form because function works when have question , answer when have questions not work! problem gets me every time answer last question, example if have 5 questions , 5 answers register in reply models 5 times last answer! each question records response associated it! is, if have 5 questions , 5 answers each question recorded me response corresponds it! thank help!

this 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 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 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():             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 forms.py :

from django import forms  pqr.models import reply  class replyform(forms.form):     class meta:         model = reply reply = forms.charfield(label='reply ',max_length=100)  

and 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 %}     {% 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> -->  # don't know have put here enter <!--  -->     {% endfor %} {% endblock %} </fieldset> <input type="submit" value="submit"/> </form> <a href="{% url 'list_patient' %}"/> <input type="button" value="look back"/> 

how submit button either registers 1 answer many?

if form.is_valid():             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() 

you returning there function inside loop, function return after first reply

if form.is_valid():         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() 

put return outside loop, function return after question in questions have been processed.


Comments

Popular posts from this blog

c# - Validate object ID from GET to POST -

node.js - Custom Model Validator SailsJS -

php - Find a regex to take part of Email -