WTForm未显示,取而代之的是python代码

WTForm未显示,取而代之的是python代码,python,flask,wtforms,Python,Flask,Wtforms,我正在用WTForms为烧瓶做一个表格。以下是相应的代码: class UploadForm(flask.ext.wtf.Form): def __init__(self,year): flask.ext.wtf.Form.__init__(self) self.year=year subjects = app.config["SUBJECTS"][year] self.fichier = wtforms.fields.Fi

我正在用WTForms为烧瓶做一个表格。以下是相应的代码:

class UploadForm(flask.ext.wtf.Form):
    def __init__(self,year):
        flask.ext.wtf.Form.__init__(self)
        self.year=year
        subjects = app.config["SUBJECTS"][year]
        self.fichier = wtforms.fields.FileField(u'Fichier')
        self.subject = wtforms.fields.SelectField(u'Matière', choices=subjects)
        self.submit = wtforms.fields.SubmitField(u'Envoyer')

@app.route('/upload/<year>')
def upload(year):
    print year
    form = UploadForm(year)
    return flask.render_template('upload.html', form=form)
但是,当我运行它时,表单不会显示,而是显示以下内容:

<UnboundField(FileField, (u'Fichier',), {})> <UnboundField(SelectField, (u'Mati\xe8re',), {'choices': [('MA111', 'MA111'), ('NE111', 'NE111')]})>  <UnboundField(SubmitField, (u'Envoyer',), {})> 


有人能帮我解决吗?

表单的字段需要定义为类变量:

class UploadForm(flask.ext.wtf.Form):
    fichier = wtforms.fields.FileField(u'Fichier')
    subject = wtforms.fields.SelectField(u'Matière')
    submit = wtforms.fields.SubmitField(u'Envoyer')

    def __init__(self, year):
        flask.ext.wtf.Form.__init__(self)
        self.year=year
        self.subject.choices = app.config["SUBJECTS"][year]

表单的字段需要定义为类变量:

class UploadForm(flask.ext.wtf.Form):
    fichier = wtforms.fields.FileField(u'Fichier')
    subject = wtforms.fields.SelectField(u'Matière')
    submit = wtforms.fields.SubmitField(u'Envoyer')

    def __init__(self, year):
        flask.ext.wtf.Form.__init__(self)
        self.year=year
        self.subject.choices = app.config["SUBJECTS"][year]

您的问题是,您正在创建没有任何字段的表单,然后在
上载表单的后面添加未绑定的字段。
wtforms.form.form
类实际上在幕后实现了很多元类魔法。做你正在做的事情的方法如下:

from flask import render_template
from flask.ext.wtf import Form
from wtforms.fields import FileField, SelectField, SubmitField

class UploadForm(Form):
    """This seemingly static class will be transformed
    by the WTForms metaclass constructor"""

    fichier = FileField(u'Fichier')
    subject = SelectField(u'Matière')
    submit = SubmitField(u'Envoyer')


@app.route('/upload/<year>')
def upload(year):
    subjects = app.config['SUBJECTS'][year]

    form = UploadForm()
    # We can set the choices dynamically, based on year
    form.subject.choices = subjects

    return render_template('upload.html', form=form)
从flask导入渲染模板
来自flask.ext.wtf导入表单
从wtforms.fields导入文件字段,选择字段,提交字段
类上载表单(表单):
“”“这个看似静态的类将被转换
通过WTForms元类构造函数“”
fichier=FileField(u'fichier')
主题=选择字段(u'Matière')
提交=提交字段(u‘特使’)
@app.route(“/upload/”)
def上传(年份):
subjects=app.config['subjects'][年份]
form=UploadForm()
#我们可以根据年份动态设置选项
form.subject.choices=主题
返回呈现模板('upload.html',form=form)

您的问题是,您正在创建没有任何字段的表单,然后在
上载表单的后面添加未绑定的字段。
wtforms.form.form
类实际上在幕后实现了很多元类魔法。做你正在做的事情的方法如下:

from flask import render_template
from flask.ext.wtf import Form
from wtforms.fields import FileField, SelectField, SubmitField

class UploadForm(Form):
    """This seemingly static class will be transformed
    by the WTForms metaclass constructor"""

    fichier = FileField(u'Fichier')
    subject = SelectField(u'Matière')
    submit = SubmitField(u'Envoyer')


@app.route('/upload/<year>')
def upload(year):
    subjects = app.config['SUBJECTS'][year]

    form = UploadForm()
    # We can set the choices dynamically, based on year
    form.subject.choices = subjects

    return render_template('upload.html', form=form)
从flask导入渲染模板
来自flask.ext.wtf导入表单
从wtforms.fields导入文件字段,选择字段,提交字段
类上载表单(表单):
“”“这个看似静态的类将被转换
通过WTForms元类构造函数“”
fichier=FileField(u'fichier')
主题=选择字段(u'Matière')
提交=提交字段(u‘特使’)
@app.route(“/upload/”)
def上传(年份):
subjects=app.config['subjects'][年份]
form=UploadForm()
#我们可以根据年份动态设置选项
form.subject.choices=主题
返回呈现模板('upload.html',form=form)