Django:如何在我的视图中逐个迭代一个对象?
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Django:如何在我的视图中逐个迭代一个对象?相关的知识,希望对你有一定的参考价值。
我正在尝试用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
答案
我认为最直接的方法是在Django中保持代码相同,并使用javascript来处理用户在浏览器上看到的内容。然后,完成后,您可以像传统的表格帖子一样提交答案。
另一答案
这听起来像你将你的对象扔到Paginator
('per_page为1)然后你可以穿过它们。
见:https://docs.djangoproject.com/en/2.0/topics/pagination/
以上是关于Django:如何在我的视图中逐个迭代一个对象?的主要内容,如果未能解决你的问题,请参考以下文章
如何在我的 Django 应用程序的“评论”视图中修复此错误?