HTML
<thead>
<tr>
{% for field in fields %}
<th>{{ field }}</th>
{% endfor %}
</tr>
</thead>
<tbody>
{% for well in well_info %}
<tr>
<td><p>{{ well.api }}</p></td>
<td><p>{{ well.well_name }}</p></td>
<td><p>{{ well.status }}</p></td>
<td><p>{{ well.phase }}</p></td>
<td><p>{{ well.region }}</p></td>
<td><p>{{ well.start_date }}</p></td>
<td><p>{{ well.last_updates }}</p></td>
</tr>
{% endfor %}
<tr>
views.py
class WellList_ListView(ListView):
template_name = 'well_list.html'
context_object_name = 'well_info'
model = models.WellInfo
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
context['fields'] = [field.name for field in models.WellInfo._meta.get_fields()]
return context
models.py
from django.db import models
from django.urls import reverse
# Create your models here.
class WellInfo(models.Model):
api = models.CharField(max_length=100, primary_key=True)
well_name = models.CharField(max_length=100)
status = models.CharField(max_length=100)
phase = models.CharField(max_length=100)
region = models.CharField(max_length=100)
start_date = models.CharField(max_length=100)
last_updates = models.CharField(max_length=100)
def get_absolute_url(self):
return reverse("")
def __str__(self):
return self.well_name
I was able to list all attribute field names by getting context['fields'], but I don't know how to automatically print each objects all attributes values.
So in my html file, I hard-coded all the attribute names, but I want to know if I can to this in a more elegant way, by using for loop. So something like:
<tbody>
{% for well in well_info %}
<tr>
{% for value in attribute_list %}
<td><p>{{ well.value }}</p></td>
{% endfor %}
</tr>
{% endfor %}
<tr>