表单标签未使用 Django & 渲染WT表格
我正在尝试将 WTForms 与 Django & 一起使用MongoEngine/MongoDB 数据库后端。表格输出正确,但我一生都无法让标签显示出来。
这是我的模板代码:
{% load wtforms %}
<form>
{% for f in form %}
{{ f.label }}: {% form_field f %}<br/>
{% endfor %}
</form>
这就是我在视图中传递的内容:
form = StrandForm()
return render_to_response('create_strand.html', locals(), context_instance = RequestContext(request))
StrandForm 类我尝试从 WTForm mongoengine 扩展的 model_form 类和 WTForm 的 Form 类创建。该标签存在于视图中,我可以将其打印到控制台并显示渲染的表单标签,但在传输到模板时它会以某种方式丢失。我做错了什么吗?
I'm trying to use WTForms with Django & a MongoEngine/MongoDB database backend. The forms are outputting properly, but I can't for the life of me get the labels to show up.
Here is my template code:
{% load wtforms %}
<form>
{% for f in form %}
{{ f.label }}: {% form_field f %}<br/>
{% endfor %}
</form>
This is what I am passing in the view:
form = StrandForm()
return render_to_response('create_strand.html', locals(), context_instance = RequestContext(request))
The StrandForm class I have tried both creating from the WTForm mongoengine extension's model_form class, and from WTForm's Form class. The label exists in the view, I can print it to the console and it shows the rendered form label, but somehow it gets lost when transferring to the template. Am I doing something wrong?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
Django 1.4 有一个新功能:
do_not_call_in_templates
属性。如果您在 wtforms.Field 类上设置它,则每个子类都会继承,并且所有字段都将在 django 模板中正常工作。
现在以下代码可以按预期工作:
Django 1.4 has a new feature:
do_not_call_in_templates
attribute.If you set it on
wtforms.Field
class, every child class inherits and all fields will work fine in django templates.Now following code works as expected:
我今天遇到了同样的问题。它与 WTForms 的编程方式有关,以便它可以与许多不同的模板库一起使用。 Django 1.3 只会看到 f 因为它是 HTML 字符串,即使它有其他属性。
为了解决这个问题,您必须添加一个模板标签来检索属性。
将以下内容添加到您的项目层次结构中:
然后在您的设置中。 py 文件中,将以下行添加到 INSTALLED_APPS
打开 getattribute.py 并粘贴以下代码:
这将允许您在模板内并需要一个不会'的属性时使用以下代码t 出现:
就您而言:
希望有帮助!
I encountered the same problem today. It has to do with the way WTForms is programmed so that it will work with many different templating libraries. Django 1.3 will only see f as it's HTML string even though it has other attributes.
In order to fix this you must add a template tag to retrieve the attribute.
Add the following to your projects hierarchy:
Then in your settings.py file, add the following line to INSTALLED_APPS
Open up getattribute.py and paste the following code:
This will allow you to use the follow code whenever you're inside a template and need an attribute that won't show up:
In your case:
Hope that helped!