I have a situation similar to this:
# models
class Entity(models.Model):
name = models.TextField()
class Member(models.Model):
entity = models.ForeignKey('common.Entity', help_text="FKey relationship to Entity table")
name = models.TextField()
# view
def details(request, eid):
entity = Entity.objects.select_related().get(id=eid)
entity.entity_test = 'oh yeah'
for member in entity.member_set.all():
member.member_test = 'oh noes'
response = {
'entity': entity,
}
return render_to_response(
'common/details.html',
{'response': response},
context_instance=RequestContext(request)
)
# html template
visible -> {{ response.entity.entity_test }}<br />
{% for member in response.entity.member_set.all %}
not visible -> {{ member.member_test }} <!-- WHY? -->
{% endfor %}
So, how can I set an attribute here to each of my member
so I can use it in the template?
You are not modifying a member_set
in the loop. Store the result of the entity.member_set.all()
in the list variable and then modify it's items in the loop:
entity_members = entity.member_set.all()
for member in entity_members:
member.member_test = 'oh noes'
response = {
'entity': entity,
'entity_members': entity_members
}
Then, in the template:
{% for member in entity_members %}
{{ member.member_test }}
{% endfor %}
Hope that helps.
I have a situation similar to this:
# models
class Entity(models.Model):
name = models.TextField()
class Member(models.Model):
entity = models.ForeignKey('common.Entity', help_text="FKey relationship to Entity table")
name = models.TextField()
# view
def details(request, eid):
entity = Entity.objects.select_related().get(id=eid)
entity.entity_test = 'oh yeah'
for member in entity.member_set.all():
member.member_test = 'oh noes'
response = {
'entity': entity,
}
return render_to_response(
'common/details.html',
{'response': response},
context_instance=RequestContext(request)
)
# html template
visible -> {{ response.entity.entity_test }}<br />
{% for member in response.entity.member_set.all %}
not visible -> {{ member.member_test }} <!-- WHY? -->
{% endfor %}
So, how can I set an attribute here to each of my member
so I can use it in the template?
You are not modifying a member_set
in the loop. Store the result of the entity.member_set.all()
in the list variable and then modify it's items in the loop:
entity_members = entity.member_set.all()
for member in entity_members:
member.member_test = 'oh noes'
response = {
'entity': entity,
'entity_members': entity_members
}
Then, in the template:
{% for member in entity_members %}
{{ member.member_test }}
{% endfor %}
Hope that helps.
0 commentaires:
Enregistrer un commentaire