基于类的视图-使用FormView

时间:2019-01-23 11:43:33

标签: django forms django-class-based-views

我正在尝试构建一个Survey App。我为调查,问题和回应定义了一个模型,如下所示。

我想要做的是创建一个调查(可以在管理员那里进行),然后将该调查显示为用户可以填写的页面。

我是第一次使用基于类的视图,并且对如何呈现“调查”表单以及相关的“问题”并允许输入“答案”感到困惑。

在Admin中,我创建了一个调查,并成功向其中添加了问题。 FormView(和UpdateView,作为另一个SO答案的一部分尝试使用)使我能够显示Survey模型的“名称”和“描述”属性-但问题不会出现。

我需要做些什么来使调查及其问题在我的表单中可用(并允许用户输入答复)?

型号

class Survey(models.Model):
    name = models.CharField(max_length=400)
    description = models.TextField()

    def survey_questions(self):
        if self.pk:
            return Question.objects.filter(survey=self.pk)
        else:
            return None

    def publish(self):
        self.published_date = timezone.now()
        self.save()

class Question(models.Model):
    survey = models.ForeignKey(Survey, on_delete=models.CASCADE)
    question = models.TextField()

class Response(models.Model):
    member = models.ForeignKey(user_model, on_delete=models.SET_NULL, null=True, blank=True)
    survey = models.ForeignKey(Survey, on_delete=models.CASCADE)
    created = models.DateTimeField(auto_now_add=True)
    updated = models.DateTimeField(auto_now_add=True)

class AnswerBase(models.Model):
    question = models.ForeignKey(Question, on_delete=models.CASCADE)
    response = models.ForeignKey(Response, on_delete=models.CASCADE)
    created = models.DateTimeField(auto_now_add=True)
    updated = models.DateTimeField(auto_now=True)

class AnswerText(AnswerBase):
    body = models.TextField(blank=True, null=True)

URLS

urlpatterns = [path('survey/<int:pk>/', views.SurveyResponseView.as_view(), name='survey_detail')]

观看次数

class SurveyResponseView(FormView):
    template_name = 'survey/survey_detail.html'
    form_class = ResponseForm

表格

class ResponseForm(forms.ModelForm):

    class Meta():
        model = Survey
        fields = '__all__'

1 个答案:

答案 0 :(得分:1)

好的,这样做的方法是使用FormSets。它们设置起来有点麻烦,但不要花太长时间。我建议使用this small js library to help

我还重写了一些代码,希望可以使您的生活更轻松:

models.py

class SurveyTemplate(models.Model):
    name = models.CharField(max_length=400)
    description = models.TextField()
    # You were missing this I believe
    published_date = models.DateTimeField(blank=True, null=True)

    def publish(self):
        self.published_date = timezone.now()
        self.save()


class Question(models.Model):
    # Now you can do SurveyTemplate.questions.all()
    survey = models.ForeignKey(Survey, on_delete=models.CASCADE, related_name='questions')
    question = models.TextField()


class SurveyResponse(models.Model):
    member = models.ForeignKey(user_model, on_delete=models.SET_NULL, null=True, blank=True)
    survey = models.ForeignKey(Survey, on_delete=models.CASCADE)
    created = models.DateTimeField(auto_now_add=True)
    updated = models.DateTimeField(auto_now_add=True)


class Answer(models.Model):
    # Now you can do Question.answers.all()
    question = models.ForeignKey(Question, on_delete=models.CASCADE, related_name='answers')
    # Now you can do Response.answers.all()
    response = models.ForeignKey(Response, on_delete=models.CASCADE, related_name='answers')
    created = models.DateTimeField(auto_now_add=True)
    updated = models.DateTimeField(auto_now=True)

views.py

from django.views.generic import CreateView


# CreateView is Django's built in View for creating an object, you would be best to use it
class SurveyResponseView(CreateView):
    model = Survey
    template = 'survey/survey_detail.html'
    form_class = ResponseForm

forms.py

class ResponseForm(forms.ModelForm):
    class Meta():
        model = Survey
        fields = '__all__'