0

模板.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 %}

视图.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 循环中使用两个列表。我得到一个错误无法解析剩余部分:来自'q2,q3'的',q3'。基本上我在模板中组合了两个表单并希望在同一模板中显示来自数据库的数据

4

1 回答 1

1

你不想那样做。大概这两个模型之间存在某种关系——ForeignKey、OneToOne、ManyToMany?如果是这样,您应该遍历其中之一,并按照关系获取相关项目。

于 2013-01-22T13:40:48.743 回答