表单标签未使用 Django & 渲染WT表格

发布于 2024-11-08 01:56:44 字数 584 浏览 1 评论 0原文

我正在尝试将 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

过度放纵 2024-11-15 01:56:44

Django 1.4 有一个新功能:do_not_call_in_templates 属性。

如果您在 wtforms.Field 类上设置它,则每个子类都会继承,并且所有字段都将在 django 模板中正常工作。

import wtforms
wtforms.Field.do_not_call_in_templates = True

现在以下代码可以按预期工作:

{% load wtforms %}
{{ f.label }}: {% form_field f %}

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.

import wtforms
wtforms.Field.do_not_call_in_templates = True

Now following code works as expected:

{% load wtforms %}
{{ f.label }}: {% form_field f %}
裸钻 2024-11-15 01:56:44

我今天遇到了同样的问题。它与 WTForms 的编程方式有关,以便它可以与许多不同的模板库一起使用。 Django 1.3 只会看到 f 因为它是 HTML 字符串,即使它有其他属性。

为了解决这个问题,您必须添加一个模板标签来检索属性。

将以下内容添加到您的项目层次结构中:

  • templatetags
  • templatetags / init.py
  • templatetags / templatetags
  • templatetags / templatetags / init.py
  • templatetags / templatetags / getattribute.py

然后在您的设置中。 py 文件中,将以下行添加到 INSTALLED_APPS

'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)()

这将允许您在模板内并需要一个不会'的属性时使用以下代码t 出现:

{% load getattribute %}
{% getattribute OBJECT ATTRIBUTE %}

就您而言:

{% getattribute f label %}

希望有帮助!

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:

  • templatetags
  • templatetags / init.py
  • templatetags / templatetags
  • templatetags / templatetags / init.py
  • templatetags / templatetags / getattribute.py

Then in your settings.py file, add the following line to INSTALLED_APPS

'templatetags',

Open up getattribute.py and paste the following code:

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)()

This will allow you to use the follow code whenever you're inside a template and need an attribute that won't show up:

{% load getattribute %}
{% getattribute OBJECT ATTRIBUTE %}

In your case:

{% getattribute f label %}

Hope that helped!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文