我在 Windows 7 上使用 Python 2.7.2 和 Django 1.4.1 运行它。
我在 sqlite 数据库中有几条记录,我正在尝试使用通用类视图 ListView 显示它们。我已经消除了它不可能的东西,并将所有东西都剥离为最简单的格式,但我不确定去哪里弄清楚它可能是什么。
模型:
class DMG_New_Inv (models.Model): # DMG's
def __init__(self, *args, **kwargs):
super(DMG_New_Inv, self).__init__(*args, **kwargs)
self.objects = None
model_num = models.ForeignKey(DMG_Models_Default, verbose_name='Model')
serial_num = models.CharField(max_length=25, verbose_name='Serial Number')
date_in = models.DateField(auto_now_add=True) #this will set the date to the creation date automaticly
date_out = models.DateField(blank=True, null=True) # the date is was removed from inventory
status = models.CharField(max_length=2, verbose_name='Asset Status', choices=DMG_ASSET_STATUS) # see list above
company = models.CharField(max_length=50, verbose_name='Company', blank=True, null=True) # the company or person who received the product
notes = models.CharField(max_length=255, verbose_name='Notes', blank=True, null=True)
pass
class Meta:
verbose_name = 'DMG'
verbose_name_plural = 'DMGs'
看法:
from django.views.generic import list
from django.views.generic import ListView
from django.shortcuts import get_object_or_404
from inventory.models import DMG_New_Inv
class dmg_new_status(ListView):
context_object_name = "dmg_new_status"
template_name = 'inventory/dmg_inv.html'
def get_queryset(self):
status = get_object_or_404(DMG_New_Inv, status__iexact=self.args[0])
return DMG_New_Inv.objects.filter(status__iexact=status)
内容:
{% block content %}
<div id="content">
<table cellspacing="3" cellpadding="3">
<tr align="center">
<th>Model</th><th>Serial Number</th><th>Date In</th><th>Date Out</th><th>Status</th><th>Company</th><th>Notes</th>
</tr>
{% for dmg in dmg_new_status %}
<tr align="center">
<td>{{ dmg.model_num }}</td>
<td>{{ dmg.serial_num }}</td>
<td>{{ dmg.date_in }}</td>
<td>{{ dmg.date_out }}</td>
<td>{{ dmg.status }}</td>
<td> {{ dmg.company }}</td>
<td> {{ dmg.notes }}</td>
</tr>
{% empty %}
No DMG's in this list
{% endfor %}
</table>
{% 端块 %}
网址:
(r'^inventory/dmg/(\w+)/$', dmg_new_status.as_view()), #status page
我确定这不是路径问题,因为我添加了空选项,它显示表标题但没有记录。如果我不得不猜测并且我在猜测,我不会返回任何东西,因此数组是空的。我不确定如何进一步解决此问题。我打开一个控制台,导入所有内容并以这种方式运行 query_set 函数,但什么也没得到,这就是为什么我相信错误在那里。
谢谢