0

我正在用 django 制作一个调查网站。我是 django 的新手,所以如果我不能很好地解释,我提前道歉。我的问题集中在以下模型上:

class SurveyType(models.Model):

    name = models.CharField(max_length=200)

    def __unicode__(self):
        return self.name    

class Question(models.Model):

    ANSWERTYPE_CHOICES = (
        (u'T', u'Text'),
        (u'R', u'Range'),
        (u'M', u'Media'),
    )

    question = models.CharField(max_length=200)

    surveytype = models.ForeignKey(SurveyType)

    answertype = models.CharField(max_length=1, choices=ANSWERTYPE_CHOICES)

    order = models.IntegerField(default=0)

    def __unicode__(self):
        return self.question        


class Survey(models.Model):

    course = models.ForeignKey(Course)

    surveytype = models.ForeignKey(SurveyType)

    def __unicode__(self):
        return u"%s %s" % (self.course, self.surveytype) 

class Answer(models.Model):

    answer = models.CharField(max_length=400)

    survey = models.ForeignKey(Survey)

    question = models.ForeignKey(Question)

    def __unicode__(self):
        return self.answer

Django 接收调查 ID。使用调查 ID,它会获取调查类型并显示必须显示的问题。

def survey(request, survey_id):
    survey_data = get_object_or_404(Survey, pk=survey_id)
    survey_type = survey_data.surveytype
    questions = Question.objects.all().filter(surveytype = survey_type).order_by('order')

我已经阅读了关于表单集的 django 文档,但我不明白我必须在 forms.py 中写什么,所以我不能在views.py 中调用表单来在显示问题的模板中呈现表单并编写答案模型中的答案。

在此先感谢并为我的英语感到抱歉。

4

2 回答 2

1

使用 modelforms 和 foo 循环解决。

模型.py

class Question(models.Model): 
    question = models.CharField(max_length=200)
    surveytype = models.ForeignKey(SurveyType)
    answertype = models.ForeignKey(ContentType,
    limit_choices_to = Q(name='text answer', app_label='surveys')| \
    Q(name='media answer', app_label='surveys')| \
    Q(name='range answer', app_label='surveys'))

class RangeAnswer(models.Model):
    answer = models.IntegerField(max_length=1, choices=CHOICES_RANGE, default=0)
    def __unicode__(self):
        return u'%s'%(self.answer)

class TextAnswer(models.Model):
    answer= models.CharField(max_length=200)
    def __unicode__(self):
        return u'%s'%(self.answer)

class MediaAnswer(models.Model):
    answer= models.ForeignKey(Media)
    def __unicode__(self):
        return u'%s'%(self.answer)

表格.py

class RangeAnswerForm(forms.ModelForm):
    def __init__(self, *args, **kwargs):
        super(RangeAnswerForm, self).__init__(*args, **kwargs)
        self.fields['answer'].label = "Mi valoración"    
    class Meta:
        model = RangeAnswer
        widgets = {
            'answer': forms.RadioSelect(renderer=RadioRenderer)
        } 
RangeAnswer.form = RangeAnswerForm

class MediaAnswerForm(forms.ModelForm):
    def __init__(self, *args, **kwargs):
        super(MediaAnswerForm, self).__init__(*args, **kwargs)
        self.fields['answer'].label = "Medio"    
    class Meta:
        model = MediaAnswer
MediaAnswer.form= MediaAnswerForm

class TextAnswerForm(forms.ModelForm):
    def __init__(self, *args, **kwargs):
        super(TextAnswerForm, self).__init__(*args, **kwargs)
        self.fields['answer'].label = "Respuesta"    
    class Meta:
        model = TextAnswer
TextAnswer.form = TextAnswerForm

视图.py

for q in questions :
    q.form = q.answertype.model_class().form(prefix="%s"%q.id)
于 2012-05-11T17:47:09.063 回答
0

您是否将表单导入到您的views.py?

之后,您只需创建一个将表单传递给模板的视图。

例如在views.py

def your_form(request):
    form = RegisterForm()
    return render_to_response('your_template.html', {'form': form}, RequestContext(request))

然后在您的模板中,您可以简单地通过编写来呈现表单

{{ form }}
于 2012-04-07T13:30:09.567 回答