[Django]-Django โ€“ ListView โ€“ template for loop does not display any items

4๐Ÿ‘

โœ…

You can try using the Model _meta API. In your views you can put the fields into a list like:

from django.shortcuts import render
from django.views.generic import View, TemplateView, ListView, DetailView, CreateView, UpdateView, DeleteView

from .models import WellInfo

class WellInfoListView(ListView):
    template_name = 'well_list.html'
    context_object_name = 'well_info'
    model = WellInfo

    def get_context_data(self, **kwargs):
        ctx = super(WellInfoListView, self).get_context_data(**kwargs)
        ctx['fields'] = [field.name for field in WellInfo._meta.get_fields()]
        return ctx

and then in your template you can have

<thead>
    {% for field in fields %}
    <th>{{ field }}</th>
    {% endfor %}
</thead>
๐Ÿ‘คleelum1

Leave a comment