Note that there are some explanatory texts on larger screens.

plurals
  1. POTypeError object is not iterable
    primarykey
    data
    text
    <p>I am getting the following error when trying to loop over a variable in my Django templates. The variable in question is the related object of the model specified in my DetailView subclass:</p> <blockquote> <p>TypeError at /en/applicants/50771459778/</p> <p>'Householdmember' object is not iterable</p> </blockquote> <p>Here is my <code>models.py</code> file:</p> <pre><code>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) </code></pre> <p>Here is my <code>urls.py</code> file:</p> <pre><code>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&lt;pk_masked&gt;\d+)/$', login_required(DetailViewUnmask.as_view( model=Applicant, template_name='applicants/detail.html' )), name='detail'), </code></pre> <p>Here is the relevant part of my template, <code>detail.html</code>:</p> <pre><code>&lt;h2&gt;Household members&lt;/h2&gt; &lt;table class="package_detail"&gt; &lt;tr&gt; {% include "applicants/householdmember_heading_snippet.html" %} &lt;/tr&gt; {% for householdmember in applicant.householdmember_set.all %} &lt;tr&gt; {% for field in householdmember %} &lt;td&gt;{{ field }}&lt;/td&gt; {% endfor %} &lt;!-- &lt;td&gt;{{ householdmember.first_name }}&lt;/td&gt; &lt;td&gt;{{ householdmember.middle_name }}&lt;/td&gt; &lt;td&gt;{{ householdmember.last_name }}&lt;/td&gt; &lt;td&gt;{{ householdmember.national_id }}&lt;/td&gt; &lt;td&gt;{{ householdmember.get_male_display }}&lt;/td&gt; &lt;td&gt;{{ householdmember.date_of_birth }}&lt;/td&gt; &lt;td&gt;{{ householdmember.get_rel_to_head_display }}&lt;/td&gt; &lt;td&gt;{{ householdmember.get_disability_display }}&lt;/td&gt; --&gt; &lt;/tr&gt; {% endfor %} &lt;/table&gt; </code></pre> <p>The part that is commented out (i.e. the part in between the <code>&lt;!-- --&gt;</code> tags) works, which leads me to think that I should be able to iterate over the <code>householdmember</code> variable. But when I try to do so, it doesn't work - I just get the TypeError above. </p> <p>I have searched stackoverflow.com exentsively for an answer, but the closest answer I could find is this one: <a href="https://stackoverflow.com/questions/6693026/django-how-to-loop-through-the-context-object-passed-back-by-a-generic-detailvie">django how to loop through the context object passed back by a generic detailview?</a>, but it does not solve my problem, I think because I'm using class based views. </p> <p>Would greatly appreciate any help. Thanks!</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload