Django-ListView-循环模板不显示任何项目 [英] Django - ListView - template for loop does not display any items

查看:35
本文介绍了Django-ListView-循环模板不显示任何项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我想要实现的:

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("")

因为我在 views.py 中正确定义了 context_object_name ='well_info',所以我在 {%中为well_info%}中的项目使用了的html,我期望我至少可以从模型属性中得到一些东西.但是,当我运行这段代码时,我什么也没得到.标题行就像下面的屏幕快照一样消失了:

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:

我希望表头具有在models.py中定义的属性名称,但是显然它没有从中获取任何东西.为什么它不从 models.py 抓取任何属性,我如何仅将属性名称显示为列标题?

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?

所以我希望表头是:

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

,不包含每个键的值.

推荐答案

您可以尝试使用Model _meta API.在您的视图中,您可以将字段放入列表中,例如:

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>

这篇关于Django-ListView-循环模板不显示任何项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆