Django大师/详细信息模板

时间:2013-05-03 14:36:48

标签: django django-templates

我有两个定义两个模型的类

class Master(models.Model):
    date = models.DateTimeField()
    status = models.CharField(default = 'R')

class Detail(models.Model):
    name = models.TextField()
    from = models.ForeignKey(Master)

观点:

def list_view(request):
    masters = master.objects.filter()
    context = {'masters': masters}
    return render_to_response('list.html', context, context_instance = RequestContext(request))

模板:

{% for master in masters %}
    <tr>
        <td>{{ master.date }}</td>
        <td>{{ master.status }}</td>
        <td>{# THIS #}</td>
    </tr>
{% endfor %}

我想在{#THIS#}部分显示详细名称,但我不知道如何修改View。如何在模板中访问主服务器的详细信息?

1 个答案:

答案 0 :(得分:3)

您可以执行以下操作来获取names

{% for master in masters %}
    <tr>
        <td>{{ master.date }}</td>
        <td>{{ master.status }}</td>
        <td>
           {% for detail in master.detail_set.all %}
               {{detail.name}}<br/>
           {% endfor %}        
        </td>
    </tr>
{% endfor %}