Hello
I'm a new django developper and i must build a statistic web site.
For this, i made some opération.
# models.py
class Question(models.Model):
question_text = models.CharField(max_length=200)
categorie = models.CharField(max_length=20)
pub_creat = models.DateTimeField('date creation')
# champs à afficher
def __str__(self):
return self.question_text
class Response(models.Model):
question = models.ForeignKey(Question, on_delete=models.CASCADE)
response_text = models.CharField(max_length=200)
votes = models.IntegerField(default=0)
# champs à afficher
def __str__(self):
return self.response_text
views.py
class enfantQueryListView(generic.ListView):
template_name = 'colorrun/enfant2.html'
context_object_name = 'liste_question_enfant'
def get_queryset(self):
return Question.objects.filter(categorie='Enfant')
enfant2.html
{% if liste_question_enfant %}
<ul>
{% for question in liste_question_enfant %}
<li>Question : {{ question.question_text }}</a></li>
<form action="{% url 'colorrun:vote' question.id %}" method="post"> {% csrf_token %}
{% for reponse in question.response_set.all %}
<input type="radio" name="rep_question" id="rep_question{{ forloop.counter}}" value="{{reponse.id}}"> <label for="rep_question{{ forloop.counter}}"> {{reponse.response_text }}</label><br>
{% endfor %}
</form>
{% endfor %}
</ul>
{% else %}
<p>Pas de question pour les enfants.</p>
{% endif %}
I hope to save all responses in doer two increment the field "vote" in the table "response".
Actually, i don't kow to implement this mechanism.
Could you help me, I have to shown my first version of the web site on friday in order to validate the POC.
Thanks for your help