动态字段Django测验应用程序

时间:2017-04-02 18:57:47

标签: django forms

我现在真的被困住了。我在这里搜索过,教程和文档。我正在尝试动态创建一个特定的字段。例如,用户可以在多项选择测验中选择2-6个答案。但是,我所遵循的教程似乎没有任何帮助,我很难理解文档。

使用内联表单时,我得到了非常奇怪的答案。

这是我的表格和型号代码。

class QuizForm(forms.Form):
    class Meta:
        model = Quiz
        fields = ['title', 'answerA', 'answerB', 'answerC', 'answerD', 'difficulty_level', 'correctAnswer']

    title = forms.CharField(max_length=1500)
    answerA = forms.CharField(max_length=150)
    answerB = forms.CharField(max_length=150)
    answerC = forms.CharField(max_length=150)
    answerD = forms.CharField(max_length=150)
    difficulty_level = forms.ChoiceField(choices=Quiz.DIFFICULTY_LEVELS)
    # score = forms.CharField(max_length=150)
    correctAnswer = forms.CharField(max_length=150)

class Quiz(models.Model):

    quizID = models.AutoField(primary_key = True)
    title = models.CharField(max_length=1500, default = '')
    answerA = models.CharField(max_length=150, default='')
    answerB = models.CharField(max_length=150, default='')
    answerC = models.CharField(max_length=150, default='')
    answerD = models.CharField(max_length=150, default='')
    # score = models.CharField(max_length=150, default='')
    correctAnswer = models.CharField(max_length=150, default='')
    DIFFICULTY_LEVELS = (
        ('Beginner', '1'),
        ('Intermediate', '2'),
        ('Advanced', '3'),
    )
    difficulty_level = models.CharField(max_length=1, choices=DIFFICULTY_LEVELS, default='')

    def __str__(self):
        return self.title

0 个答案:

没有答案