Django表单Newbie问题 [英] Django Forms Newbie Question

查看:98
本文介绍了Django表单Newbie问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好的,我在Django Forms中感到失落,因为这些文档似乎并没有涵盖我正在寻找的内容。至少看起来,一旦你超越了最初始的形式,它就会尖叫起来。我更愿意链接到良好的文档,或链接到一本涵盖这个主题的好书,作为答案。基本上,这是怎么回事,我有3个模型(测验,问题,答案)。我有20个问题,4个潜在答案(多选),每个测验。数字可能会有所不同,但是你会得到点。

Alright, I'm at a loss with the Django Forms, as the documentation just doesn't seem to quite cover what I'm looking for. At least it seems to come to a screeching halt once you get past the most rudimentary of forms. I'm more than willing to take a link to good documentation, or a link to a good book that covers this topic, as an answer. Basically, this is how it breaks down, I have 3 models (quiz, questions, answers). I have 20 questions, with 4 potential answers (multi-choice), per quiz. The numbers can vary, but you get the point.

我需要为这些项目创建一个表单,就像您期望在多项选择测验中一样。但是,当我在模板中手工创建表单时,而不是使用django.forms,我得到以下内容:

I need to create a form for these items, much like you'd expect in a multiple choice quiz. However, when I create the form by hand in the templates, rather than using django.forms, I get the following:

对于基数为10的int()的无效文字: 'test'

invalid literal for int() with base 10: 'test'

所以我试图搞砸django.forms,但我想我只是没有把握如何建立一个正确的形式的想法那些。谢谢你们的帮助,谢谢。

So I'm trying to mess with the django.forms, but I guess I'm just not grasping the idea of how to build a proper form out of those. Any help would be greatly appreciated, thanks.

对于这里的值得一提的是:

For what it's worth here are the models:

class Quiz(models.Model):
    label = models.CharField(blank=True, max_length=400)
    slug = models.SlugField()

    def __unicode__(self):
        return self.label

class Question(models.Model):
    label = models.CharField(blank=True, max_length=400)
    quiz = models.ForeignKey(Quiz)

    def __unicode__(self):
        return self.label

class Answer(models.Model):
    label = models.CharField(blank=True, max_length=400)
    question = models.ForeignKey(Question)
    correct = models.BooleanField()

    def __unicode__(self):
        return self.label


推荐答案

p>是的,我必须同意这里的文件和示例真的很缺乏。对于您所描述的情况,您不需要开箱即用,因为它深入三层:quiz-> question-> answer。

Yeah I have to agree the documentation and examples are really lacking here. The is no out of the box solution for the case you are describing because it goes three layers deep: quiz->question->answer.

Django有模型内联表单,解决了两层深度的问题。您需要做什么来生成您想要的表单:

Django has model inline formsets which solve the problem for two layers deep. What you will need to do to generate the form you want is:


  1. 加载测验表单(只需一个标签文本框从您的模型)

  2. 加载一个问题formset:QuestionFormSet(queryset = Question.objects.filter(quiz = quiz))

  3. 对于每个问题,加载一个答案表单与加载问题formset的方式大致相同

  4. 确保以正确的顺序保存所有内容:quiz-> question-> answer,因为每个下级都需要外键的

  1. Load up a quiz form (just a label text box from your model)
  2. Load a an question formset: QuestionFormSet(queryset=Question.objects.filter(quiz=quiz))
  3. For each question load up a answer formset in much the same way you load up the question formset
  4. Make sure you save everything in the right order: quiz->question->answer, since each lower level needs the foreign key of the item above it

这篇关于Django表单Newbie问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆