使用单选按钮创建Django表单调查表

时间:2018-08-20 22:12:12

标签: django forms api radio

我正在尝试在Django应用程序中实施调查。目前,我是通过手动绘制表单结构的HTML来实现的,如下所示:

      <!-- Question 1-->
  <form method="post" action="{% url 'piuserform' %}">
    {% csrf_token %}
    <div class="mx-auto">
      <h3 class="mb-4">At the time of the accident, the injured party was: </h3>
      <label class="radcontainer">The driver of an automobile.
            <input type="radio" name="question1" value="The driver of an automobile">
            <span class="checkmark"></span>
          </label>
      <label class="radcontainer">The passenger of an automobile.
            <input type="radio" name="question1" value="The passenger of an automobile">
            <span class="checkmark"></span>
          </label>
      <label class="radcontainer">A pedestrian.
            <input type="radio" name="question1" value="A pedestrian">
            <span class="checkmark"></span>
          </label>
    </div>
    <!-- /Question 1 -->


    <!-- question 3-->
    <div class="mx-auto pt-5">
      <h3 class="mb-4">How many vehicles were involved in the accident?</h3>
      <label class="radcontainer">One
            <input type="radio" name="question3" value="One">
            <span class="checkmark"></span>
          </label>
      <label class="radcontainer">Two
            <input type="radio" name="question3" value="Two">
            <span class="checkmark"></span>
          </label>
      <label class="radcontainer">Three
            <input type="radio" name="question3" value="Three">
            <span class="checkmark"></span>
          </label>
      <label class="radcontainer">Four or more
            <input type="radio" name="question3" value="Four or more">
            <span class="checkmark"></span>
          </label>
    </div>
    <!-- /Question 3 -->

现在,我尝试使用Django表单api来实现此目的,但这只会呈现最后一个问题。问卷是所有单选按钮,答案字段相差很大。有没有一种方法可以自动执行此操作,以便可以通过modelForm或Formsetfactory使用{{form}}进行渲染?我看了很多文档,但是对于我的问题来说,它们中的任何一个都不是很清楚,当我尝试实现像我说的那样的形式api时,它只会呈现一个问题。非常感谢

编辑:

当我尝试实现表单API时,我使用了以下方法:

forms.py

blah_choices = [("1", "first"), ("2", "second")]
blah_choices2 = [("3", "third"), ("4", "four")]

class testRadioQuestions(forms.Form):
    q1 = forms.ChoiceField(label="Blah blah blah form question",
                           choices=(blah_choices),
                           widget=forms.RadioSelect()),
    q2 = forms.ChoiceField(label="blah blah some more form question",
                           choices=(blah_choices2),
                           widget=forms.RadioSelect())

视图

def other_questionnaire(request):
     if request.method == "POST":
         print(request.body)
     return render(request, 'website/otherQuestions.html', {'form': form})

2 个答案:

答案 0 :(得分:0)

这看起来像很多工作。我认为问题可能在于您的视图上下文只有最后一种形式。您将需要创建列表或表单字典。您还需要使用“ prefix”来区分他们的“ id”。

我想出了一个解决方案,但是(像你一样,我认为)我有一种挥之不去的感觉,就是我错过了“表单” api中的重要内容。

我的解决方案是为每个表单手动分配一个前缀,使我可以将其从POST中解压缩到正确的表单子类中。我不会发布所有不同的部分,但这是一个草图:

# forms.py
AnswerMap = { question_answer_type1 : FormClass1, etc etc }
def form_get(question, instance=None):
    answerform = AnswerMap[question.answer_type](instance=instance) # not sure this works, may need to handle two cases separately.
    return answerform 

def form_post(question, data, instance=None):
    answerform = AnswerMap[question.answer_type](data=data, instance=instance)
    if answerform.is_valid():
        return answerform
    else:
        pass  # handle error, I guess.

下一部分是视图。我在这里处理request.POST的方式似乎效率很低,因为我经历了两次-首先收集前缀并确定存在哪些问题,第二次(一次理解)收集每个前缀的数据。但是我还没有想出更好的办法。

# views.py
class page_view(View):
    def get(self, page_no):
        forms = {}
        questions = Question.objects.filter(page=page_no)
        for question in questions:
            forms[question] = get_form_for_question(question, prefix=('q'+question.pk))
        self.context['forms'] = forms
        return render(request, template, context)

     def post(self, page_no)
         prefixes = []
         self.form_items = {}
         for key, value in request.POST.items():
             if key.startswith("q"):
                 q_number = re.search(r'q(\d+)#', key)
                 pk_q = int(q_number.group(1))  # the digits are the question primary key
                 prefix = 'q' + str(pk_q)
                 if prefix not in prefixes:
                     prefix.append(prefix)
                     question = Question.objects.get(pk=pk_q)
                     self.form_items[prefix] = {key: val for key,val in request.POST.items() if prefix in key}  # collects data for form; add any data that you don't render but will need in the database somewhere here - e.g. username or timestamp
                     form = form_post(value=self.form_items[prefix], question=question)
                     if form.is_valid():
                         form.save()
         return HttpRedirectResponse('as appropriate')

添加自定义模板标签以将模板中的表格解包:

# custom_tags.py
@register.filter
def get_item(dictionary, key):    
    return dictionary.get(key)

在模板中,提取所需的表单,如下所示:

{% load 'custom_tags' %}
{% for question in page_question_set %}
     {{question}}
     {{ forms|get_item:question }}
{% endfor %}

希望您觉得这有用。我认为这不是一个坏的解决方法,但我仍然担心自己正在研究框架实际上可以解决的问题。

答案 1 :(得分:0)

只需删除每个定义字段末尾的<,>(逗号),这应该可以工作

forms.py

T&&

views.py

from django import forms

    blah_choices = [("1", "first"), ("2", "second")]
    blah_choices2 = [("3", "third"), ("4", "four")]

    class TestRadioQuestions(forms.Form):
        email = forms.EmailField()
        q1 = forms.ChoiceField(label="Blah blah blah form question",choices=(blah_choices),widget=forms.RadioSelect())
        q2 = forms.ChoiceField(label="blah blah some form question",choices=(blah_choices2),widget=forms.RadioSelect())