【问题标题】:Dynamic View not displayed动态视图未显示
【发布时间】:2013-03-15 04:12:12
【问题描述】:

我正在尝试进行测验,如果您回答错误的问题,您会看到该问题被添加到错误问题列表中。

问题可以在多个测验中使用,所以我不能硬编码先问 question_1 然后 question_2 等等。

当我加载页面时,我会看到第一个问题,但如果我提交或刷新,我会看到 right.html 模板。它不会问所有的问题。

它应该在渲染 right.html 页面之前询问所有问题

for question in quiz.questions.all():
   if question not in asked_questions:
        asked_questions.append(question)
        return answer_question(request, quiz_id, question.id, module_list)

models.py

class Choice(models.Model):
    choice = models.CharField(max_length=64)
    def __unicode__(self):
        return self.choice

#create a multiple choice quiz to start
class Question(models.Model):
    question = models.CharField(max_length=64)
    answer = models.CharField(max_length=64)
    choices = models.ManyToManyField(Choice)
    module = models.CharField(max_length=64)

    def __unicode__(self):
        return self.question

class Quiz(models.Model):
    name = models.CharField(max_length=64)
    questions = models.ManyToManyField(Question)

    def __unicode__(self):
        return self.name

views.py

asked_questions = []
module_list = []
module_list.append('test')

def take_quiz(request, quiz_id):
    quiz = Quiz.objects.get(id=str(quiz_id))

    for question in quiz.questions.all():
        if question not in asked_questions:
            asked_questions.append(question)
            return answer_question(request, quiz_id, question.id, module_list)
    #quiz is over
    return render (request, 'right.html')

def answer_question(request, quiz_id, question_id, module_list):
    question = Question.objects.get(id=question_id)
    #module_list = []
    if request.method == 'POST':
        form = QuestionForm(request.POST)
        choices = [(i, i) for i in question.choices.all()]
        form.fields['selection'].choices = choices

        if form.is_valid():
            if form.cleaned_data['selection'] != str(question.answer):
                module_list.append(question.module)
            return take_quiz(request, quiz_id)
    else:
        form = QuestionForm()
        choices = [(i, i) for i in question.choices.all()]
        form.fields['selection'].choices = choices

    return render(request, 'answer_question.html', {'question':question, 'form':form, 'module_list':module_list})

【问题讨论】:

  • 能分享一下right.html的内容吗?
  • 它包含“正确”,所以我知道我完成了测验

标签: django python-2.7 django-forms django-views


【解决方案1】:

如果您打算托管它并在网络上提供它,那么您真的应该重新开始。你不应该使用这样的列表来存储东西,而应该使用sessionscache。此外,您最好使用FormWizard 进行测验,因为它旨在完成您正在尝试做的事情:处理多个表单并处理它们。

【讨论】:

  • 我正在查看 FormWizard,我想知道。如何设置表单域的选项?您如何多次但动态地使用相同的表单。显然你不能在 FormWizard 中使用 ajax 仍然是这种情况吗?
  • @Siecje 所以 FormWizard 有一些你可以重写的方法来设置类似的东西。您可能需要get_form 方法。至于多次使用同一个表单,您可能只是动态地创建一个包含表单副本的列表以传递给视图。我不确定 ajax,您可能可以制作一个使用 ajax 的模板,我暂时想不出有什么会阻止您这样做。
【解决方案2】:

也许你应该试试

return HttpResponseRedirect(reverse('answer_question', args=[quiz_id, question_id, module_list]))

将您的视图重定向到另一个视图。

ps:reverse 应该包含您的“answer_question”视图的名称

【讨论】:

  • 我同意 Ngenator 在会话或缓存中的观点。
  • 未找到带有参数“(u'1', 1, ['test'])' 和关键字参数“{}”的“answer_question”的反向操作。
  • 就像我说的,“answer_question”应该替换为您在 urls.py 中的名称,该名称会导致该视图。
  • urls.py 中的示例:urlpatterns = patterns('', url(r'^answer_question/$', 'app.views.view', name='answer_question'), )跨度>
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-12-12
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多