为问卷中的每个元素生成无线电字段 [英] Generate Radio Field for each element in a questionnaire

查看:27
本文介绍了为问卷中的每个元素生成无线电字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我使用 wtforms 来生成无线电场,但我需要它们是动态的,就像每个问题有 12 个问题都会得到一组无线电场一样.我试过 FormField ,它给了我我想要的东西,但所有问题的名字都是一样的,有没有更好的方法来做到这一点?

输出应该是这样的:

问题Radiofield1 radioField1题Radiofield2 radioField2

编辑:我继续完成 HTML 字段,但这真的很有帮助.我不记得我做了什么,但这很接近它我可以渲染字段但 ID 不会改变我尝试将元组传递给

options 但它为每个问题呈现动态单选字段,而不是每个问题 1 个集合

<块引用>

Forms.py

class QuestionRadio(FlaskForm):rad=RadioField("rad",choices=[(1,'Yes'),(2,'No')],id="opt")类 QuestionForm(FlaskForm):options=FieldList(FormField(QuestionRadio),min_entries=1)

<块引用>

视图.py

 #这是问题列表question=[(ques.id,ques.question) 对于 Questions.query.all() 中的问题]表单=问题表单()#我不记得我在这之后做了什么

<块引用>

模板.html

{% for key,val in question %}<h4>键.val/h4{{form.options}}{% endfor%}

解决方案

wforms 文档 建议创建一个虚拟类并在您的视图中动态添加字段.

这是文档中的示例:

def my_view():F类(MyBaseForm):经过F.username = StringField('用户名')对于 iterate_some_model_dynamically() 中的名称:setattr(F, name, StringField(name.title()))表单 = F(request.POST, ...)# 查看东西

在您的情况下,代码可能如下所示(未经测试):

def my_view():类 QuestionForm(Form):经过对于 Questions.query.all() 中的 q:field = RadioField(q.question,choices=[(1,'Yes'),(2,'No')],id=q.id)setattr(QuestionForm, q.id, field)form = QuestionForm(request.POST, ...)# 查看东西

So I'm using wtforms to generate radio fields but I need them to be dynamic like if there are 12 questions each question would get a set of radio fields. I tried FormField which gave me what i wanted but the names are same for all the questions is there a better way to do this?

the output should be like this:

Question
Radiofield1 radioField1

Question
Radiofield2 radioField2

Edit: I moved on to complete HTML fields but it would be really helpful. I don't remember much as to what I did but this was close to it I could render the fields but the id wouldn't change I tried passing the tuples to

options but it rendered dynamic radio fields for each question rather than 1 set for each question

Forms.py

class QuestionRadio(FlaskForm):
    rad=RadioField("rad",choices=[(1,'Yes'),(2,'No')],id="opt")

class QuestionForm(FlaskForm):
    options=FieldList(FormField(QuestionRadio),min_entries=1)

Views.py

    #This is the question list  
    question=[(ques.id,ques.question) for ques in Questions.query.all()]
    form=QuestionForm()
    #I don't remember what I did after this

template.html

{% for key,val in question %}

<h4>key. val</h4>

 {{form.options}}


{% endfor%}

解决方案

The wforms documentation suggests creating a dummy class and dynamically adding fields in your view.

This is the example from the documentation:

def my_view():
    class F(MyBaseForm):
        pass

    F.username = StringField('username')
    for name in iterate_some_model_dynamically():
        setattr(F, name, StringField(name.title()))

    form = F(request.POST, ...)
    # do view stuff

In your case the code might look something like this (untested):

def my_view():
    class QuestionForm(Form):
        pass

    for q in Questions.query.all():
        field = RadioField(q.question,choices=[(1,'Yes'),(2,'No')],id=q.id)
        setattr(QuestionForm, q.id, field)

    form = QuestionForm(request.POST, ...)
    # do view stuff

这篇关于为问卷中的每个元素生成无线电字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆