How to set class name in table cell in python django?
I am learning django. I have a simple model named customer. Here is my model:
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
and view is: from django.shortcuts import render, get_object_or_404, redirect from .models import 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)
template:
{% 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>
Now I want that in cell of each row , th will be class and return the customer name,just like <th class='Customer Name 1'> 454</th>
Every cell of row will return customer name with product. Please help me as early possible.
considering format of display in template, you need to use list of dicts as follows:
Views
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)
Template:
<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/56538.html
上一篇: 上传后在Django中预处理图像