不使用Django&;渲染表单标签;WTForms
我正在尝试将WTForms与Django一起使用&一个MongoEngine/MongoDB数据库后端。表单输出正常,但我一辈子都无法让标签显示出来 以下是我的模板代码:不使用Django&;渲染表单标签;WTForms,django,mongoengine,wtforms,Django,Mongoengine,Wtforms,我正在尝试将WTForms与Django一起使用&一个MongoEngine/MongoDB数据库后端。表单输出正常,但我一辈子都无法让标签显示出来 以下是我的模板代码: {% load wtforms %} <form> {% for f in form %} {{ f.label }}: {% form_field f %}<br/> {% endfor %} </form> 我尝试过从WTForm mongoengine
{% load wtforms %}
<form>
{% for f in form %}
{{ f.label }}: {% form_field f %}<br/>
{% endfor %}
</form>
我尝试过从WTForm mongoengine扩展的model_form类和WTForm的form类创建StrandForm类。标签存在于视图中,我可以将其打印到控制台,并显示呈现的表单标签,但在传输到模板时不知何故它丢失了。我做错什么了吗?我今天遇到了同样的问题。它与WTForms的编程方式有关,因此它可以与许多不同的模板库一起工作。Django 1.3将只看到f作为HTML字符串,即使它有其他属性 为了解决这个问题,您必须添加一个模板标记来检索属性 将以下内容添加到项目层次结构中:
- 模板标签
- templatetags/init.py
- 模板标签/模板标签
- templatetags/templatetags/init.py
- templatetags/templatetags/getattribute.py
'templatetags',
打开getattribute.py并粘贴以下代码:
from django import template
from django.conf import settings
register = template.Library()
@register.tag
def getattribute(parser, token):
try:
tag_name, tag_object, tag_function = token.split_contents()
except ValueError:
raise template.TemplateSyntaxError("%r tag requires two arguments" % token.contents.split()[0])
return getattrNode(tag_object, tag_function)
class getattrNode(template.Node):
def __init__(self, tag_object, tag_function):
self.tag_object = tag_object
self.tag_function = tag_function
def render(self, context):
return getattr(context[self.tag_object], self.tag_function)()
这将允许您在模板中需要不显示的属性时使用以下代码:
{% load getattribute %}
{% getattribute OBJECT ATTRIBUTE %}
就你而言:
{% getattribute f label %}
希望有帮助 我今天遇到了同样的问题。它与WTForms的编程方式有关,因此它可以与许多不同的模板库一起工作。Django 1.3将只看到f作为HTML字符串,即使它有其他属性 为了解决这个问题,您必须添加一个模板标记来检索属性 将以下内容添加到项目层次结构中:
- 模板标签
- templatetags/init.py
- 模板标签/模板标签
- templatetags/templatetags/init.py
- templatetags/templatetags/getattribute.py
'templatetags',
打开getattribute.py并粘贴以下代码:
from django import template
from django.conf import settings
register = template.Library()
@register.tag
def getattribute(parser, token):
try:
tag_name, tag_object, tag_function = token.split_contents()
except ValueError:
raise template.TemplateSyntaxError("%r tag requires two arguments" % token.contents.split()[0])
return getattrNode(tag_object, tag_function)
class getattrNode(template.Node):
def __init__(self, tag_object, tag_function):
self.tag_object = tag_object
self.tag_function = tag_function
def render(self, context):
return getattr(context[self.tag_object], self.tag_function)()
这将允许您在模板中需要不显示的属性时使用以下代码:
{% load getattribute %}
{% getattribute OBJECT ATTRIBUTE %}
就你而言:
{% getattribute f label %}
希望有帮助 Django 1.4有一个新特性:
do_not_call_in_templates
属性
如果将其设置为wtforms.Field
class,则每个子类都将继承,并且所有字段都将在django模板中正常工作
import wtforms
wtforms.Field.do_not_call_in_templates = True
现在,以下代码按预期工作:
{% load wtforms %}
{{ f.label }}: {% form_field f %}
Django 1.4有一个新特性:
do\u not\u call\u in\u templates
属性
如果将其设置为wtforms.Field
class,则每个子类都将继承,并且所有字段都将在django模板中正常工作
import wtforms
wtforms.Field.do_not_call_in_templates = True
现在,以下代码按预期工作:
{% load wtforms %}
{{ f.label }}: {% form_field f %}
这修复了一个问题,我在表单和模板中定义了
nested\u form=FormField(…)
:{%form\u field form.nested\u form.nested\u field%}
在我执行wtforms.field.do\u调用\u in\u templates=True之前不会显示。谢谢。这修复了一个问题,我在表单和模板中定义了nested\u form=FormField(…)
:{%form\u field form.nested\u form.nested\u field%}
直到我定义了wtforms.field.do\u不调用\u in\u templates=True
。谢谢