Reputation: 1523
Is there a way to for loop through rows of a model into a table. It would also be very useful if i could exclude certain fields. I am making a form that the user can create by creating there own questions to ask in one model and a model for the answers
For example:
model.py
class Questions(models.Model):
name = models.Charfield()
Question1 = models.Charfield()
Question2 = models.Charfield()
ect
class Answers(models.Model):
question = models.ForeignKey(Questions, related_name='question')
qustion_no = models.IntegerField()
answer = models.Charfield()
form.html
<table>
<tr>
<th>Question</th>
<th>Answers</th>
</tr>
{% for q in Questions %}
<tr>
<td>{{q}}</td>
<td>{{q.question}}</td
</tr>
{% endfor %}
</table>
Upvotes: 3
Views: 11383
Reputation: 6009
I think you may searching for something like this.
<table>
<tr>
<th>Question</th>
<th>Answers</th>
</tr>
{% for q in Questions %}
<tr>
{% for a in q.question.all %}
<td>{{q}}</td>
<td>{{a.answer}}</td>
{% endfor %}
</tr>
{% endfor %}
</table>
Upvotes: 5
Reputation: 3567
Try to use django-tables2
It is pretty flexible and can be modified easily
Upvotes: 0