如何使用for循环遍历django中的多个列表

时间:2013-01-22 13:35:59

标签: django

template.html

{% extends "base.html" %}

<body background="100%">
{% block content %}
<!--<table class="table" align="right"style=" margin-right:10px">-->
<form action="." method="post">
{% csrf_token %}


<table align="right" style="margin-right:27px ,margin-top:-20px";>

        <p>{{GeneratorService_form.as_table}}</p>
        <p>{{FuelUsuage_form.as_table}}</p>
<tr><td colspan="2" align="right"><input name="KEBsubmit" type="submit" value="Submit Reading" id="_KEBsubmit1"/> </td></tr>
    <!--<tr><td colspan="2" >{{KEBMessage}} </td></tr>-->

</table>
 </form>


<table align="left" border="1">
<p> Generator Service Readings </p>
<tr>
<th>Date </th>
<th>Time</th>
<th>Running time</th>
<th>Next Service Reading</th>
<th>Running Difference</th>
<th>Part Changed</th>
<th>Fuel Type</th>
<th>Fuel quantity</th>
<th>Fuel Balance</th>
 </tr>
 <tr>
    {% for item,item1 in q2,q3 %}
 <td>{{item.date}}</td>
 <td>{{item.time}}</td>
 <td>{{item.runningtime_reading}}</td>
 <td>{{item.next_service_reading}}</td>
  <td>{{item.running_diff}}</td>
   <td> {{item.part_changed}}</td>
    <td>{{item1.type}}</td>
    <td>{{item1.balance}}</td>
    <td>{{item1.quantity}}</td>
 </tr>
 {% endfor %}
</table>

{% endblock content %}

views.py

def GENService(request):
    q2=GeneratorService.objects.all().order_by('date')[:30]
    q3=FuelUsuage.objects.all().order_by('date')[:30]
    if request.method=="POST":
        form=GeneratorService_form(request.POST)
        form2=FuelUsuage_form(request.POST)
        if form.is_valid():
            prevdate=GeneratorService.objects.latest("date")
            prevdate1=FuelUsuage.objects.latest("fu_date")
            print prevdate.date
            print prevdate.time
            running_time = form.cleaned_data["runningtime_reading"]
            gs_next_service_reading=running_time+250
            gs_running_diff=running_time-prevdate.runningtime_reading
            form1=form.save(commit=False)
            form1.running_diff=gs_running_diff
            form1.next_service_reading=gs_next_service_reading
            form1.save()
            form2.save()

            q2=GeneratorService.objects.all().order_by('date')[:30]
            q3=FuelUsuage.objects.all().order_by('date')[:30]
            print q2.values()

            context={'GeneratorService_form':form,'FuelUsuage_form':form2,'q2':q2,'q3':q3}                              return                          render_to_response('serv.html',context,context_instance=RequestContext(request))

   else:
        form = GeneratorService_form()
        form2=FuelUsuage_form()
    return render_to_response('serv.html',{'GeneratorService_form':form,'FuelUsuage_form':form2,'q2':q2,'q3':q3},context_instance=RequestContext(request))

如何在模板中的for循环中使用两个列表。 im gettting a error无法解析余数:',q3'来自'q2,q3'。我基本上将两个表单组合在一个模板中,并希望在同一模板中显示数据库中的数据

1 个答案:

答案 0 :(得分:1)

你不想这样做。据推测,这两种模式之间存在某种关系 - ForeignKey,OneToOne,ManyToMany?如果是这样,您应该遍历其中一个,并按照关系获取相关项目。