3

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.

1
  • Can you post the full source for well_list.html (or at least all the relevant portions)? The snippet you posted does not have sufficient information. Commented Jul 19, 2018 at 2:13

1 Answer 1

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>
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.