我的表单中无法回答几个问题

时间:2015-06-15 08:30:18

标签: python django forms

我在我的视图“def save_reply”中创建了一个函数,它允许我检索给定患者的问题答案。现在我希望在我的表单中提交许多问题,因为我的功能仅在我有问题和答案时有效,但是当我有一些问题时它不起作用! 我的问题是每次答案只会给我最后一个问题,例如,如果我有5个问题和5个答案,他们将在我的答复模型中注册5次最后答案!我想为每个问题记录他与之相关的回答!也就是说,如果我有5个问题和5个答案,我希望每个问题他都记录了他对应的答案! 谢谢你的帮助!

  

这是我的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
  

这是我的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))
  

这是我的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) 
  

这是我的模板(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"/>

如何让我的提交按钮只注册一个答案,但很多?

0 个答案:

没有答案