Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django - ListView - template for loop does not display any items

This is what I want to achieve:

enter image description here

well_list.html

<thead>
  <tr>
    {% for item in well_info %}
    <th>item</th>
    {% endfor %}
  </tr>
</thead>

project/urls.py

from django.conf.urls import url, include
from django.contrib import admin
from django.views.generic import TemplateView
from django.urls import path, re_path, include
from eric_base import views

urlpatterns = [
    path('contextual/', include('eric_base.urls')),
    path('well_list/', views.well_list)
]

views.py

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

from . import models

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

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)
    name = models.CharField(max_length=100)
    region_location = models.CharField(max_length=100)
    spud_date = models.CharField(max_length=100)
    well_bore = models.CharField(max_length=100)
    rig_name = models.CharField(max_length=100)
    status = models.CharField(max_length=100)

    def get_absolute_url(self):
        return reverse("")

Since I correctly defined context_object_name = 'well_info' in views.py, and I used {% for item in well_info %} in the html, I was expecting that I would get at least something from the model attributes. But when I run this code, I don't get anything. The header row just disappears like the following screenshot:

enter image description here

I want the table headers to have the attribute names defined in models.py, but apparently its not grabbing anything from it. Why is it not grabbing any attributes from models.py, and how can I display only the attribute names as a column header?

So I want the table headers to be:

Api | Name | Region Location | Spud Date | Well Bore | Rig Name | Status

without the values of each keys.

like image 289
Eric Kim Avatar asked Mar 28 '26 07:03

Eric Kim


1 Answers

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>
like image 164
leelum1 Avatar answered Mar 29 '26 22:03

leelum1