尝试遍历 Django 模板中的变量时出现以下错误。有问题的变量是我的 DetailView 子类中指定的模型的相关对象:
/en/applicants/50771459778/ 处的类型错误
“家庭成员”对象不可迭代
这是我的models.py
文件:
class Applicant(models.Model):
user = models.ForeignKey(User, editable=False)
bank_card_number = models.CharField(_('Bank card number'),max_length=50, unique=True)
site_of_interview = models.IntegerField(_('Site of interview'), choices = SITE_CHOICES, default=TIRANA, blank=False)
housenumber = models.CharField(_('House Number'),max_length=8)
address_line1 = models.CharField(_('Address line 1'),max_length=50)
address_line2 = models.CharField(_('Apt #'),max_length=50,blank=True)
municipality = models.CharField(_('Municipality/commune'),max_length=25)
district = models.CharField(_('District'),max_length=25,blank=True)
urban = models.IntegerField(_('Area (urban/rural)'), choices = AREA_CHOICES, blank=False)
postal = models.CharField(_('Postal code'),max_length=25,blank=True)
class Householdmember(models.Model):
applicant = models.ForeignKey(Applicant)
first_name = models.CharField(_('First name'),max_length=50,blank=False)
middle_name = models.CharField(_('Middle name'),max_length=50,blank=True)
last_name = models.CharField(_('Last name'),max_length=50,blank=False)
national_id = models.CharField(_('National ID'),max_length=50,blank=False, unique=True)
male = models.IntegerField(_('Gender'), choices = GENDER_CHOICES, blank=False)
date_of_birth = models.DateField()
rel_to_head = models.IntegerField(_('Gender'), choices = RELTOHEAD_CHOICES, blank=False)
disability = models.IntegerField(_('Is disabled?'), choices = YESNO_CHOICES, blank=False)
created_at = models.DateTimeField(auto_now_add = True)
updated_at = models.DateTimeField(auto_now = True)
这是我的urls.py
文件:
class ListViewApplicants(ListView):
paginate_by = 100
def get_queryset(self):
return Applicant.objects.all()
class DetailViewUnmask(DetailView):
def get_object(self):
return self.get_queryset().get(pk=mask_toggle(self.kwargs.get("pk_masked")))
urlpatterns = patterns('',
url(r'^$',
login_required(ListViewApplicants.as_view(
template_name='applicants/index.html',
#context_object_name='form',
)),
name='index'),
url(r'^(?P<pk_masked>\d+)/$',
login_required(DetailViewUnmask.as_view( model=Applicant,
template_name='applicants/detail.html'
)),
name='detail'),
这是我模板的相关部分detail.html
:
<h2>Household members</h2>
<table class="package_detail">
<tr>
{% include "applicants/householdmember_heading_snippet.html" %}
</tr>
{% for householdmember in applicant.householdmember_set.all %}
<tr>
{% for field in householdmember %}
<td>{{ field }}</td>
{% endfor %}
<!--
<td>{{ householdmember.first_name }}</td>
<td>{{ householdmember.middle_name }}</td>
<td>{{ householdmember.last_name }}</td>
<td>{{ householdmember.national_id }}</td>
<td>{{ householdmember.get_male_display }}</td>
<td>{{ householdmember.date_of_birth }}</td>
<td>{{ householdmember.get_rel_to_head_display }}</td>
<td>{{ householdmember.get_disability_display }}</td>
-->
</tr>
{% endfor %}
</table>
被注释掉的部分(即<!-- -->
标签之间的部分)有效,这让我认为我应该能够迭代householdmember
变量。但是当我尝试这样做时,它不起作用 - 我只是得到上面的 TypeError 。
我已经在 stackoverflow.com 上搜索了一个答案,但我能找到的最接近的答案是这个:django how to loop through the context object pass back by a generic detailview? ,但它并没有解决我的问题,我认为是因为我使用的是基于类的视图。
非常感谢任何帮助。谢谢!