I wish I could retrieve in the form of my template the answer to my question (if there is one)
My models.py
class Page(models.Model):
title = models.CharField(max_length=30)
def __str__(self):
return self.title
class Question(models.Model):
label = models.CharField(max_length=30)
page = models.ManyToManyField(Page)
def __str__(self):
return self.label
class Reply(models.Model):
question = models.ForeignKey(Question)
user = models.ForeignKey(Personne)
answer = models.CharField(max_length=30)
creationDate = models.DateTimeField(default=django.utils.timezone.now)
def __str__(self):
return str(self.answer)
How can I access with my model since the answers of my questions?
My template
<form method="POST" action="">
{{ formset.management_form }} {% csrf_token %}
<table>
<!-- <br>{{ formset.as_table }}<br> -->
{% for question in questions %}<hr>
<label for="question">{{ question }} [{{ question.id }}]</label>
<input type="hidden" id="id_form-{{ forloop.counter0 }}-question" name="form-{{ forloop.counter0 }}-question" value="{{ question.id }}"/>
</p>
<p>
<label for="answer">Réponse :</label>
<input type="text" id="id_form-{{ forloop.counter0 }}-answer" name="form-{{ forloop.counter0 }}-answer" value="{{ question.answer }}"/>
</p>
{% endfor %}
<br><br><br><hr> <br><br><br>
</table><br>
<center><input type="submit" value="Submit" class="btn btn-success" />
<a href="../../baseVisite/" class="btn btn-success">Retour</a></center>
</form>
I don't know how to retrieve the answer in the answer field in the database if there is one.
My problem is this line in my template :
<input type="text" id="id_form-{{ forloop.counter0 }}-answer" name="form-{{ forloop.counter0 }}-answer" value="{{ HERE }}"/>
I don't know what to put in value to retrieve the answer
Question
object will have reply_set
attribute with record set of all the objects of Reply
linked with that question, over which you can iterate and display.
{% for reply in question.reply_set %}
<p>
<label for="answer">Réponse :</label>
<input type="text" id="id_form-{{ forloop.parentloop.counter0 }}-answer" name="form-{{ forloop.parentloop.counter0 }}-answer" value="{{ reply.answer }}"/>
</p>
{% endfor %}
Of-course you will have to handle one question having multiple replies.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.