我在加入或正确地格式化模板时遇到了一些麻烦...我是新手,并且已经自学了很多,但是我在查询和陈述方面遇到了麻烦。
到目前为止,我所拥有的是:
输出
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)
有三个解决方案:
1)操纵前端,通过将第二个" 量"列作为另一个单独的表在原始表外,然后将两个表的属性作为" inline-block "。因此,将它们对齐并排。虽然以上是不是遵循的好方法
2)添加在您的 statementdata model 中说"量子2",添加,如John Gordon所说。这样,您将在"数据"中同时拥有金额和金额2,因此可以简单地编写:
{% for firstdata in data %}
<td>{{ firstdata.item }}</td>
<td>{{ firstdata.amount }}</td>
<td>{{ firstdata.amount2 }}</td>
</tr>
{% endfor %}
,但是我得到了您的问题,即两个不同的日期上这些是同一项目。因此,从技术上讲,您不能在模型中有两个字段。
3)从技术上讲,这是您问题的理想解决方案,这是约翰·戈丹(John Gordan)试图说的。
您需要在您的观点中定义一个类,说" angell",并在其中定义三个变量,说" obj"," nose1"," nose2"现在,您需要列出可以在HTML中访问的类Angell的对象列表。以下是实现相同的代码:
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 %}
我希望这对您有用。