WTForms 不会渲染字段
我在使用 WTForms 呈现表单字段时遇到问题。我将它与 GAE 中的 webapp 框架和 Django 模板一起使用。
我做了一个简单的项目作为测试,效果很好。这是来自一个较大项目的一些代码,存在问题的代码:
class ShowBoardForm(Form):
name = TextField('Name', [validators.Length(max=20)])
email = TextField('Email', [validators.Length(max=20)])
subject = TextField('Subject', [validators.Length(max=20)])
textfield = TextAreaField('Comment', [validators.Length(max=2000)])
filefield = FileField('Image')
这是请求处理程序中使用的表单类的片段(我省略了一些不相关的代码):
template_vars = {
'form':ShowBoardForm()
}
path = os.path.join(os.path.dirname(__file__),'templates','showboard.html')
self.response.out.write(template.render(path,template_vars))
这是我使用 form
的方式在模板中:
...
<form method='post' action='{{ url }}/post' enctype='multipart/form-data'>
{{ form.name.label }}{{ form.name }}<br/>
{{ form.email.label }}{{ form.email }}<br/>
{{ form.subject.label }}{{ form.subject }}<br/>
{{ form.textfield.label }}{{ form.textfield }}<br/>
{{ form.filefield.label }}{{ form.filefield }}<br/>
<input type='submit' value='ok'/>
</form>
...
问题是,模板中的 form.name
、form.email
等不是给我字段的 HTML 表示形式,而是这样输出类型:
或
等...我在测试中做了同样的事情我提到的项目,看起来效果很好。这里可能出了什么问题?
I'm having trouble rendering my form's fields with WTForms. I'm using it with the webapp framework and Django templates in GAE.
I've made a simple project as test and it worked fine. Here's some code from a larger project, the one with the problem:
class ShowBoardForm(Form):
name = TextField('Name', [validators.Length(max=20)])
email = TextField('Email', [validators.Length(max=20)])
subject = TextField('Subject', [validators.Length(max=20)])
textfield = TextAreaField('Comment', [validators.Length(max=2000)])
filefield = FileField('Image')
And here's a snippet of the form class being used in a request handler (I ommited some irrelevant code):
template_vars = {
'form':ShowBoardForm()
}
path = os.path.join(os.path.dirname(__file__),'templates','showboard.html')
self.response.out.write(template.render(path,template_vars))
Here's how I'm using form
in the template:
...
<form method='post' action='{{ url }}/post' enctype='multipart/form-data'>
{{ form.name.label }}{{ form.name }}<br/>
{{ form.email.label }}{{ form.email }}<br/>
{{ form.subject.label }}{{ form.subject }}<br/>
{{ form.textfield.label }}{{ form.textfield }}<br/>
{{ form.filefield.label }}{{ form.filefield }}<br/>
<input type='submit' value='ok'/>
</form>
...
The problem is that instead of giving me the HTML representation of the field, form.name
, form.email
, etc... in the template has this kind of output: <form.TextField instance at 0xac261ec>
or <form.TextAreaField instance at 0x966e76c>
, etc... I did the same thing in the test project I mentioned, and it seems to work just fine. What can be going wrong here?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我想通了。结果我有另一个模块定义了
Form
、TextField
和TextAreaField
类,它们被用来代替 WTForms 类。I figured it out. Turns out I had another module defining a
Form
,TextField
, andTextAreaField
class, that was being used in place of WTForms classes.