前端获取表数据的3种方式

视图:

def business(request):

    v1 = models.Business.objects.all()                             # 对象(Queryset)
    v2 = models.Business.objects.values('id','caption')        # 字典
    v3 = models.Business.objects.values_list('id','caption')     #列表 

    return render(request, 'business.html', {'v1':v1,'v2':v2,'v3':v3})


def host(request):

    v1 = models.Host.objects.filter(nid__gt=0)
    for row in v1:
        print(row.nid,row.hostname,row.ip,row.port,row.b_id,row.b.caption,row.b.code,row.b,id,sep='\t')
    # 跨表查询时,用双下滑线取元素
    v2 =models.Host.objects.filter(nid__gt=0).values('nid','hostname','b_id','b__caption')
    return render(request,'host.html',{'v1':v1,'v2':v2})

单表前端:

    <h1>业务线列表(对象)</h1>
    {%  for row in v1 %}
        <li>{{ row.id }}-{{ row.caption }}-{{ row.code }}</li>
    {%  endfor  %}
    <h1>业务线列表(字典)</h1>
    {%  for row in v2 %}
        <li>{{ row.id }}-{{ row.caption }}</li>
    {%  endfor  %}
    <h1>业务线列表(列表)</h1>
    {%  for row in v3 %}
        <li>{{ row.0 }}-{{ row.1 }}</li>
    {%  endfor  %}

 跨表前端

            {% for row in v1 %}
                <tr hid="{{ row.nid }}" bid="{{ row.b_id }}">
{#                    <td>{{ row.nid }}</td>#}
                    <td>{{ row.hostname }}</td>
                    <td>{{ row.ip }}</td>
                    <td>{{ row.port }}</td>
{#                    <td>{{ row.b_id }}</td>#}
                    <td>{{ row.b.caption }}</td>
{#                    <td>{{ row.b.code }}</td>#}
                </tr>
            {% endfor %}

            {% for row in v2 %}
                <tr hid="{{ row.nid }}" bid="{{ row.b_id }}">
{#                    <td>{{ row.nid }}</td>#}
                    <td>{{ row.hostname }}</td>
{#                    <td>{{ row.b_id }}</td>#}
                    <td>{{ row.b__caption }}</td>
{#                    <td>{{ row.b.code }}</td>#}
                </tr>
            {% endfor %}

 

posted @ 2019-03-13 21:39  Ray_chen  阅读(3586)  评论(0编辑  收藏  举报