如何使用外部循环计数器访问内部循环中的数组

时间:2017-07-02 07:16:15

标签: python arrays django django-templates django-views

在下面的代码中,内部forloop无法正常工作

<tbody>
  {% for rec in medrec  %}

    <tr>
      <td>{{ forloop.counter }}</td>
      <td>{{ rec.date }}</td>
      <td>{{ rec.disease }}</td>
      <td>{{ rec.treatment }}</td>
      <td>  {% for n in medicine.forloop.parentforloop.counter0  %}
      {{ n.medicine }}
        {% endfor %}
      </td>
    </tr>
  {% endfor %}
  </tbody>

上面的代码生成一个表。每个rec都有一系列药物。 对于rec.forloop.counter,其中forloop.counter == 1,医学数组索引[0]中会有对象。 我该如何打印?

def profile(request,rid):

    patient = Patient.objects.get(pk=rid)
    medic = MedicalRec.objects.filter(patient=patient)
    i=0
    a=[]
    for n in medic:
        a.append(medicine.objects.filter(Rec= n))
        print(a)
    if patient:
        return render(request,'patient.html',{
            'medrec' : medic,
            'pat' : patient,
            'medicine' : a
        })
    else:
        return 'patient not found'

模型

from django.db import models

# Create your models here.

class Patient(models.Model):
    name = models.CharField(max_length=100)
    age = models.IntegerField()
    address = models.TextField()
    contact = models.CharField(max_length=15)
    def __str__(self):
        return self.name

class Stock(models.Model):
    name = models.CharField(max_length=100)
    quantity = models.IntegerField()
    expiry = models.DateField()
    price = models.IntegerField()
    def __str__(self):
        return self.name

class MedicalRec(models.Model):
    patient = models.ForeignKey(Patient)
    date = models.DateField()
    disease = models.TextField()
    treatment = models.TextField()
    medicine = models.ForeignKey(Stock)
    def __str__(self):
        return str(self.date)

class medicine(models.Model):
    Rec = models.ForeignKey(MedicalRec,related_name='med_set')
    medicine = models.ForeignKey(Stock)
    def __str__(self):
        return str(self.Rec.date)

class Billing(models.Model):
    name = models.ForeignKey(Stock)
    rate = models.IntegerField()
    Date = models.DateField()
    def __str__(self):
        return self.id

2 个答案:

答案 0 :(得分:1)

您不必自己创建列表。 Django为你创建了一个reverse relation。它将被命名为medicine_set,但现在您已经展示了模型,但已将其覆盖为med_set。因此,您不必在视图中创建列表。您可以在模板中使用相关管理器:

视图:

def profile(request, rid):
    patient = get_object_or_404(Patient, pk=rid)
    medic = MedicalRec.objects.filter(patient=patient)
    return render(request, 'patient.html', {
        'pat': patient,
        'medrec': medic,
    })

模板:

<tbody>
    {% for rec in medrec  %}
        <tr>
            <td>{{ forloop.counter }}</td>
            <td>{{ rec.date }}</td>
            <td>{{ rec.disease }}</td>
            <td>{{ rec.treatment }}</td>
            <td>
                {% for medicine in medrec.med_set.all  %}
                    {{ medicine }}
                {% endfor %}
            </td>
        </tr>
    {% endfor %}
</tbody>

答案 1 :(得分:0)

我修改了melvyn的答案并且有效

{
  "appId":"eb167a6d-aaaa-aaaa-aaaa-46e981be37fa"
}