0

模板.html

{% extends "base.html" %}

<body>
{% block content %}

<form action="." method="post">
{% csrf_token %}


<table align="center" style="margin-left:60px";>

        <p>{{KEBReading_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 border="1">
<p> KEB Reading as on Month jan and year 2012</p>
<tr>
<th>Date </th>
<th>Time</th>
<th>True Power Reading</th>
<th>Apparent Power Reading</th>
<th>True Power consumed</th>
<th>Voltage Reading</th>
<th>Power Factor</th>
 </tr>



{% for item in q2 %}
 <tr>
 <td>{{item.date}}</td>
 <td>{{item.time}}</td>
 <td>{{item.truepower_reading}}</td>
 <td>{{item.apparentpower_reading}}</td>
  <td>{{item.truepower_consumed}}</td>
  <td>{{item.voltage_reading}}</td>
    <td> {{item.powerfactor}}</td>
 </tr>
 {% endfor %}

 </table>

{% endblock 内容 %}

视图.py

 def KEBReading1(request):
#form2 = KEBReading.objects.all()
if request.method == "POST":


    form = KEBReading_form(request.POST)
    if form.is_valid():
        prevdate=KEBReading.objects.latest("date")
       # Model.objects.latest('field')
        print prevdate.date
        print prevdate.time
        # q1 = KEBReading.objects.get(datetime.date.today()-datetime.timedelta(0))

        kr_truepower_reading = form.cleaned_data["truepower_reading"]
        kr_apparentpower_reading = form.cleaned_data["apparentpower_reading"]
        truepower_consumed1=kr_truepower_reading-prevdate.truepower_reading
        powerfactor1=((kr_truepower_reading-prevdate.truepower_reading)/(kr_apparentpower_reading-prevdate.apparentpower_reading))
        #instance=truepower_consumed,powerfactor
  ## replace form.save() with following lines



        form1=form.save(commit=False)
        #form1.calculate(truepower_consumed1,powerfactor1)
        form1.truepower_consumed=truepower_consumed1
        form1.powerfactor=powerfactor1
        print form1.powerfactor
        form1.save()



        q2=KEBReading.objects.latest("date")

        context={'KEBReading_form':form,'q2':q2}
        return render_to_response('keb.html',context,context_instance=RequestContext(request))

else:
    form = KEBReading_form()

return render_to_response('keb.html',{'KEBReading_form':form},context_instance=RequestContext(request))

我想在我通过模板中的视图计算的表格中显示所有读数。我在使用 for 循环进行迭代时得到模型对象不可迭代..

4

3 回答 3

0

几个问题:

  1. 首先,正如 Aidan 正确指出的那样,KEBReading.objects.latest("date")这将返回一个对象,而不是集合或可迭代对象。

  2. 其次,您没有分配任何正在计算的字段。truepower_consumed1=kr_truepower_reading-prevdate.truepower_reading不会将它分配给您检索的对象(您的q2)。我不确定这是否是故意的。但是,我怀疑您想将表单的实例返回到您的模板,而不是表中的另一条记录。您需要更新您的问题。

要立即解决这些问题:

 <tr>
 <td>{{q2.date}}</td>
 <td>{{q2.time}}</td>
 <td>{{q2.truepower_reading}}</td>
 <td>{{q2.apparentpower_reading}}</td>
 <td>{{q2.truepower_consumed}}</td>
 <td>{{q2.voltage_reading}}</td>
 <td> {{q2.powerfactor}}</td>
 </tr>
于 2013-01-22T07:48:50.097 回答
0

你的q2变量只是一个对象而不是一个集合。

以下行返回单个对象 -

KEBReading.objects.latest("date")

但是您的模板需要一个可以迭代的集合 -

{% for item in q2 %}
 <tr>
 <td>{{item.date}}</td>
 <td>{{item.time}}</td>
 <td>{{item.truepower_reading}}</td>
 <td>{{item.apparentpower_reading}}</td>
  <td>{{item.truepower_consumed}}</td>
  <td>{{item.voltage_reading}}</td>
    <td> {{item.powerfactor}}</td>
 </tr>
 {% endfor %}

检查该功能的文档latest()

于 2013-01-22T07:42:21.857 回答
0

latest()返回表中的最新对象,而不是一组对象。取而代之的是,您应该使用KEBReading.objects.all().order_by('date')

于 2013-01-22T07:48:21.827 回答