Reputation: 160
I`ve got these defined models
class Occupation(models.Model):
title = models.CharField(max_length=150)
code = models.CharField(max_length=10)
what_they_do = models.TextField(blank=True, default="")
skills = models.ManyToManyField(Skill)
knowledge = models.ManyToManyField(Knowledge)
abilities = models.ManyToManyField(Ability)
technologies = models.ManyToManyField(Technology)
created_at = models.DateTimeField(auto_now_add=True)
modified_at = models.DateTimeField(auto_now=True)
def __unicode__(self):
return self.title
Where Knowledge, Technology, Skill, Ability are similar. I have used this structure.
class Skill(models.Model):
title = models.CharField(max_length=64)
element_id = models.CharField(max_length=10)
created_at = models.DateTimeField(auto_now_add=True)
modified_at = models.DateTimeField(auto_now=True)
def __unicode__(self):
return self.title
In my template , currently I have:
<ul>
{% for skill in ocuppation.skills.all %}
<li>{{skill.title}}</li>
{% endfor %}
</ul>
but {{ skill.title }} is blank.
In my views.py , I have defined this :
def detail(request, pk):
possible_occupation = Occupation.objects.filter(code=pk)
occupation = possible_occupation[0] if len(possible_occupation) == 1 else None
if occupation is not None:
context = {
'occupation': occupation
}
return render(request, 'careers/detail.html', context)
else:
return HttpResponseNotFound("No hay datos")
When I use the debugger I can see that occupation.skills, occupation.abilities ... are None. If I check an occupation object in django admin , everything seems ok, but i can´t use them in templates.
Can anyone help? Sorry for my bad english
Upvotes: 0
Views: 70
Reputation: 308799
You have misspelled occupation
in your template.
{% for skill in ocuppation.skills.all %}
It should be
{% for skill in occupation.skills.all %}
Here's a tip for debugging next time. When the for loop didn't print anything, I would try to include the queryset I was looping over.
{{ ocuppation.skills.all }}
and if that didn't work, try the instance itself
{{ ocuppation }}
Then I would know that the problem is with the variable ocuppation
, not the many to many field. Hopefully I would then spot the misspelling.
Upvotes: 1