【发布时间】:2015-11-26 15:48:59
【问题描述】:
我有这些定义好的模型
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
知识、技术、技能、能力相似的地方。我用过这个结构。
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
在我的模板中,目前我有:
<ul>
{% for skill in ocuppation.skills.all %}
<li>{{skill.title}}</li>
{% endfor %}
</ul>
但是 {{ Skill.title }} 是空白的。
在我的 views.py 中,我定义了这个:
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")
当我使用调试器时,我可以看到职业.技能、职业.能力……都没有。 如果我在 django admin 中检查职业对象,一切似乎都很好,但我不能在模板中使用它们。
有人可以帮忙吗? 对不起我的英语不好
【问题讨论】:
-
您确定
pk必须等于code? -
错字:
{% for skill in ocuppation.skills.all %}。应该是:{% for skill in occupation.skills.all %} -
我在模板中拼错了职业。使用 {% for Skill inoccup.skills.all %} 效果很好。