如何在python django的表格单元格中设置类名称?
我正在学习Django。 我有一个名为customer的简单模型。 这是我的模特:
class Year(models.Model):
year = models.CharField(max_length=255)
created_at = models.DateTimeField(auto_now=False, auto_now_add=True)
updated_at = models.DateTimeField(auto_now=True, auto_now_add=False)
def __unicode__(self):
return self.year
def __str__(self):
return self.year
class Customer(models.Model):
name = models.CharField(max_length=255)
created_at = models.DateTimeField(auto_now=False, auto_now_add=True)
updated_at = models.DateTimeField(auto_now=True, auto_now_add=False)
def __unicode__(self):
return self.name
def __str__(self):
return self.name
class Product(models.Model):
customer_name = models.ForeignKey(Customer, on_delete=models.CASCADE)
quantity = models.CharField(max_length=255)
year = models.ForeignKey(Year, on_delete=models.CASCADE)
created_at = models.DateTimeField(auto_now=False, auto_now_add=True)
updated_at = models.DateTimeField(auto_now=True, auto_now_add=False)
def __unicode__(self):
return self.score
def __str__(self):
return self.score
并且view是:从django.shortcuts导入render,get_object_or_404,从.models导入Customer,Product,Year
views.py
def home(request):
customers = Customer.objects.all()
years = Year.objects.all().values_list('year', flat=True).asc() # List of year name
rows = []
for year in years:
row = [year] + [None] * len(customers) # Row with year in first column, and the rest init with same size of customers list
for idx, customer in enumerate(customers):
quantities = customer.product_set.filter(year__year=year).valu e_list('quantity', flat=True) # filter product by year. That can return multiple product !!!
row[idx + 1] = ' ,'.join(quantities) # create a string of quantities
rows.append(row) # Add new row in our rows list
context = {'customers': customer,
'rows': rows}
return render(request, 'customer.html', context)
模板:
{% extends 'base.html' %}
{% block customer %}
<div class="container">
<h2>Players Table</h2>
<p>Customer with Product</p>
<table class="table">
<thead>
<tr>
<th>Year/Product</th>
{% for customer in customers %}
<th>{{ customer.name }}</th>
{% endfor %}
</tr>
</thead>
<tbody>
{% for row in rows %}
<tr>
{% for cell in row %}
<th class="">{{cell}}</th>
{% endfor %}
</tr>
{% endfor %}
</tbody>
</table>
</div>
现在我想在每一行的单元格中,th将类和返回客户名称,就像<th class='Customer Name 1'> 454</th>
行<th class='Customer Name 1'> 454</th>
每个单元格都将返回客户名称和产品。 请尽早帮助我。
考虑到模板中显示的格式,您需要使用如下所示的字典列表:
查看
def home(request):
customers = Customer.objects.all()
years = Year.objects.all().values_list('year', flat=True).asc() # List of year name
result= []
for year in years:
row = {year: []} # Row with year as key
for idx, customer in enumerate(customers):
quantities = customer.product_set.filter(year__year=year).value_list('quantity', flat=True) # filter product by year. That can return multiple product !!!
row[year].append({customer: ' ,'.join(quantities)}) # {'2017': [{'A': '2 ,2 ,3'}]}
result.append(row) # Create list of dicts [{'2017': [{'A': '2 ,2 ,3'}, {'B': '2 ,2 ,3'}, {'C': '2 ,2 ,3'}]}, {'2016': [{'A': '2 ,2 ,3'}, {'B': '2 ,2 ,3'}, {'C': '2 ,2 ,3'}]}]
context = {'customers': customer, 'result': result}
return render(request, 'customer.html', context)
模板:
<tbody>
{% for data in result %}
{% for year, cust_prod_data in data.items %}
<tr>
<th>{{year}}</th>
{% for cust_prod in cust_prod_data %}
{% for customer_name, prod_qty in cust_prod.items %}
<th class="{{customer_name}}">{{prod_qty}}</th>
{% endfor %}
{% endfor %}
</tr>
{% endfor %}
{% endfor %}
</tbody>
链接地址: http://www.djcxy.com/p/56537.html
上一篇: How to set class name in table cell in python django?
下一篇: Can we format as to how the Model Form displays in Django on template