Django:如何在我的视图中逐个迭代一个对象?

时间:2018-03-06 10:18:32

标签: django templates view

我正在尝试用Django编写一个Quiztool。我创建了一个列出所有调查的索引。单击一个可以进入详细视图。现在,列出所有问题和答案,提交按钮无效。我要求的是如何管理将只有一个问题,当我提交答案时,下一个问题将不会跳出细节视图。如果答案很简单,我也会很高兴能够得到我必须阅读的内容......

以下是我的views.py

中的一些代码
def detail(request, survey_id):
    #try:

    question = Survey.objects.get(pk=survey_id).question.all()

    question_dict = {
      'question': question,
    }
    return render(request, 'survey/detail.html', question_dict)

这是我的deatil.html

{% if question %}
    <form method="post">
    {% for x in question %}
    <fieldset style="width:10%;">
      <legend>{{x.question_text}}</legend>
      {% for y in x.answer.all %}
        <p style="display: flex;justify-content: space-between;">
        <label for="{{ y.answer_id }}">{{ y.answer_text }}</label>
        <input name="{{x.question_id}}" type="radio" value="{{y.answer_id}}" id="{{y.answer_id}}"/></p>
      {% endfor%}
    </fieldset>
    {% endfor %}
    <input type="button" value="Senden" onclick="var Sende=()=>{console.log('gesendet');}; Sende();">
    </form>
{% else %}
    <p>No questions are available.</p>
{% endif %}

我的模特

class Answer(models.Model):
    answer_id = models.AutoField(blank=False, null=False, primary_key=True)
    answer_text = models.CharField(blank=False, null=True, max_length=500, verbose_name=_(u'Text der Antwort'))

    # Internal fields
    date_created = models.DateTimeField(blank=False, null=True, auto_now_add=True, verbose_name=_(u'Erstellt am'))
    date_updated = models.DateTimeField(blank=True, null=True, auto_now=True, verbose_name=_(u'Geändert am'))

    def __str__(self):
        return self.answer_text

    class Meta:
        # db_table = 'data'
        verbose_name = _(u'Antwort')
        verbose_name_plural = _(u'Antworten')
        ordering = ['answer_id']

class Question(models.Model):
    question_id = models.AutoField(blank=False, null=False, primary_key=True)
    # Fields

    answer = models.ManyToManyField('Answer', through='Question_Answer', related_name='+')
    question_text = models.CharField(blank=False, null=True, max_length=500, verbose_name=_(u'Text der Frage'))

    # Internal fields
    date_created = models.DateTimeField(blank=False, null=True, auto_now_add=True, verbose_name=_(u'Erstellt am'))
    date_updated = models.DateTimeField(blank=True, null=True, auto_now=True, verbose_name=_(u'Geändert am'))
    #Typunterscheidung der Fragen

    QUESTION_TYPES = (
        ('0', 'Vezweigt'),
        ('1', 'Wahr/Falsch'),
        ('2', 'Punkte'),
        ('3', 'Umfrage'),
    )

    type_id = models.CharField(blank=False, null=False, max_length=1, choices=QUESTION_TYPES)
    # String

    def __str__(self):
        return self.question_text

    # Meta class
    class Meta:
        # db_table = 'data'
        verbose_name = _(u'Frage')
        verbose_name_plural = _(u'Fragen')
        ordering = ['question_id']

# Hilfstabelle für many to many Feld mit informationen für die Frage Logik
class Question_Answer(models.Model):
    question_answer_id = models.AutoField(blank=False, null=False, primary_key=True)
    question = models.ForeignKey('Question', on_delete=models.SET_NULL, null=True, related_name='+')
    answer = models.ForeignKey('Answer', related_name='+', on_delete=models.SET_NULL, null=True)
    is_correct = models.NullBooleanField(blank=True, null=True, default=False, verbose_name=_(u'Richtige Antwort'))
    next_question = models.IntegerField(blank=True, null=True, verbose_name=_(u'Naechste Frage(Verzw.)'))
    points = models.IntegerField(blank=True, null=True, verbose_name=_(u'Wertigkeit der Antwort'))


class Survey(models.Model):
    survey_id = models.AutoField(blank=False, null=False, primary_key=True)
    question = models.ManyToManyField('Question', through='Survey_Question', related_name='+')
    survey_titel = models.CharField(blank=False, null=True, max_length=500, verbose_name=_(u'Titel des Fragebogens'))

    class Meta:
        # db_table = 'data'
        verbose_name = _(u'Fragebogen')
        verbose_name_plural = _(u'Fragebögen')
        ordering = ['survey_id']

class Survey_Question(models.Model):
    survey_question_id = models.AutoField(blank=False, null=False, primary_key=True)
    question = models.ForeignKey('Question', related_name='+', on_delete=models.SET_NULL, null=True)
    survey = models.ForeignKey('Survey', on_delete=models.SET_NULL, null=True, related_name='+')
    order = models.IntegerField(blank=True, null=False, verbose_name=_(u'Rangfolge der Antwort'))

提前感谢Flotzen

2 个答案:

答案 0 :(得分:0)

我认为最直接的方法是在Django中保持代码相同,并使用Javascript来处理用户在浏览器上看到的内容。然后,完成后,您可以像传统的表格帖子一样提交答案。

答案 1 :(得分:0)

这听起来就像你将对象扔进Paginator('per_page为1)然后你可以穿过它们。

请参阅:https://docs.djangoproject.com/en/2.0/topics/pagination/