我在加入或正确格式化模板方面遇到了一些麻烦...我是新人,并且自学了很多,但我在查询和陈述方面遇到了麻烦。
到目前为止我所拥有的是:
输出
June 13, 2016 June 14, 2016
Item Amount Amount
Cars 10800.00
Tires 156400.00
10200.00
156001.00
About
我想要的是获得第二个"在下一个金额列下输出。我没有做出正确的查询吗?
模板/索引
<body>
{% if data %}
{% if date %}
{{ date.date }}
{% endif %}
<table>
<tr>
<th>List Item</th>
<th>Amount</th>
<th>Amount</th>
</tr>
<tr>
{% for firstdata in data %}
<td>{{ firstdata.item }}</td>
<td>{{ firstdata.amount }}</td>
</tr>
{% endfor %}
{% for firstdata in data1 %}
<td>{{ firstdata.amount }}</td>
</tr>
{% endfor %}
{% else %}
<strong>There are no categories present.</strong>
{% endif %}
</table>
<a href="/rango/about/">About</a>
</body>
views.py
def index(request):
context_dict = {}
statement = "Sales"
try:
now = datetime.date.today()
recentdate = StatementData.objects.order_by('date').distinct('date').filter(date__lte=now).reverse()[0]
context_dict['date'] = recentdate
first_statement = StatementData.objects.order_by('-id').filter(date=recentdate.date).values('item', 'amount')
context_dict['data'] = first_statement
now = datetime.date.today()
recentdate1 = StatementData.objects.order_by('date').distinct('date').filter(cik=cik, date__lte=now).reverse()[1]
#context_dict['date1'] = recentdate1
second_statement = StatementData.objects.order_by('-id').filter(date=recentdate1.date).values('amount')
#second_statement = StatementData.objects.order_by('-id').filter(date=recentdate1.date).values('id')
context_dict['data1'] = second_statement
#two_dates = first_statement | second_statement
#context_dict['data1'] = two_dates
except StatementData.DoesNotExist:
# We get here if we didn't find the specified category.
# Don't do anything - the template displays the "no category" message for us.
pass
# Go render the response and return it to the client.
return render(request, 'profit/index.html', context_dict)
答案 0 :(得分:0)
有三种解决方案:
1)通过制作第二个&#34; Amount &#34;来操纵前端。列作为原始表格之外的另一个单独表格,然后将这两个表格的显示属性设置为&#34; 内嵌块&# 34 ;. 因此,将它们对齐并排。 虽然以上一般不是一个好的方法
2)将另一个字段添加到 StatementData模型中说&#34; Amount2&#34;正如John Gordon评论的那样。这样,您就可以在“数据”和“数据”中同时使用金额和金额2。因此可以简单地写:
{% for firstdata in data %}
<td>{{ firstdata.item }}</td>
<td>{{ firstdata.amount }}</td>
<td>{{ firstdata.amount2 }}</td>
</tr>
{% endfor %}
但是我在两个不同的日期遇到了这些两个不同数量的同一项的问题。所以从技术上讲,你的模型中没有Amount的两个字段。
3)从技术上讲,这是解决问题的理想解决方案,这与John Gordan试图说的一样,将整体视为一个对象。
你需要在你的视图中定义一个类,然后说&#34; Angell&#34;并在其中定义三个变量说&#34; obj&#34; ,&#34; amount1&#34;,&#34; amount2&#34; 现在,您需要列出Angell类的对象列表,您可以在HTML中访问它们。 以下是实现相同的代码:
class Angell:
def __init__(self, obj, amount1, amount2):
self.obj = obj
self.amount1 = amount1
self.amount2 = amount2
def index(request):
context_dict = {}
statement = "Sales"
try:
now = datetime.date.today()
recentdate = StatementData.objects.order_by('date').distinct('date').filter(date__lte=now).reverse()[0]
context_dict['date'] = recentdate
first_statement = StatementData.objects.order_by('-id').filter(date=recentdate.date).values('item', 'amount')
#context_dict['data'] = first_statement
now = datetime.date.today()
recentdate1 = StatementData.objects.order_by('date').distinct('date').filter(cik=cik, date__lte=now).reverse()[1]
#context_dict['date1'] = recentdate1
second_statement = StatementData.objects.order_by('-id').filter(date=recentdate1.date).values('amount')
#second_statement = StatementData.objects.order_by('-id').filter(date=recentdate1.date).values('id')
#context_dict['data1'] = second_statement
#two_dates = first_statement | second_statement
#context_dict['data1'] = two_dates
n = len(first_statement) #this should be equal to length of second_statement
i = 0
some_list = []
while i < n:
itm = first_statement[i].item
amt1 = first_statement[i].amount
amt2 = second_statement[i].amount
Harp = Angell(itm, amt1, amt2)
some_list.append(Harp)
i += 1
context_dict[data] = some_list
except StatementData.DoesNotExist:
# We get here if we didn't find the specified category.
# Don't do anything - the template displays the "no category" message for us.
pass
# Go render the response and return it to the client.
return render(request, 'profit/index.html', context_dict)
现在您的HTML看起来像这样:
{% for firstdata in data %}
<tr>
<td>{{ firstdata.obj }}</td>
<td>{{ firstdata.amount1 }}</td>
<td>{{ firstdata.amount2 }}</td>
</tr>
{% endfor %}
我希望这对你有用。