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

使用单选按钮创建Django表单调查问卷,django,forms,api,radio,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 acci

我正试图在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 -->
看法


这看起来需要做很多工作。我认为问题可能在于您的视图上下文只有最后一种形式。您需要创建表单列表或词典。您还需要使用“”来区分他们的“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 %}
我希望你觉得这有用。我不认为这是一个不好的解决方法,但我仍然担心我正在解决框架实际上可以解决的问题。

只需删除每个定义字段末尾的(逗号),这应该可以解决问题

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())
views.py


如果你不需要完整的通用解决方案,这里有一个简单的问题和一个简单的解决方案:哇,这看起来很复杂。我对它有中等程度的了解。我将详细阅读如何实现这一点,非常感谢。您使用的是模型吗?显示您的视图和表单。我尝试使用forms.form。我似乎无法用至少两个单选按钮得到一组一致的问题。
{% load 'custom_tags' %}
{% for question in page_question_set %}
     {{question}}
     {{ forms|get_item:question }}
{% endfor %}
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())
def testRadioQuestions(request):
    if request.method == 'POST':
        form = TestRadioQuestions(request.POST)

        if form.is_valid():
            return HttpResponseRedirect('../../url_name')


    else:
        form = TestRadioQuestions()

    return render(request, 'template_url', {'form' : form })