在 Google App Engine 上将 WTForms 与 webapp 和 Django 模板结合使用
我正在尝试将 WTForms 与 webapp 一起使用,但运气不佳。
我希望能够使用 form_field
模板标签,如文档中所示:
{% form_field form.username class="big_text" onclick="do_something()" %}
我已经在我的应用程序中安装了 WTForms,但它的 Django 模板标签对我不起作用。有人有关于如何安装这个的说明吗?
如果我不能让它工作,我可能会放弃并切换到 Jinja2 模板。到目前为止,我们一直使用 Django 的原因是尽可能限制依赖项的数量。
以下是我到目前为止所做的尝试:
我已在本地安装 Django 1.1 并启用它 根据文档。
我尝试将 INSTALLED_APPS = ['wtforms.ext.django']
添加到我的 settings.py
- 没有效果。
我尝试手动注册 wtforms templatetag:。
register = webapp.template.create_template_register()
from wtforms.ext.django.templatetags import wtforms
register.tag('form_field', wtforms.do_form_field)
这给了我一个错误:InvalidTemplateLibrary:无法从 template_helpers 加载模板库,没有名为 django.templatetags 的模块
所以我尝试将模板标签复制并粘贴到我自己的代码中,但出现错误TemplateSyntaxError:无法解析其余部分:“form_field form.foobar”中的“form.foobar”
。
但是,我认为模板标签注册不起作用,因为如果没有该代码,错误是相同的。
更新:我将这个问题保留下来,以防有一天互联网上的人可以回答它,但我切换到 Jinja2,现在一切正常。带有 Django 模板的 Web 应用程序对我来说已经死了。 ;-)
I'm trying to use WTForms with webapp without much luck.
I would like to be able to use the form_field
templatetag, as shown in the documentation:
{% form_field form.username class="big_text" onclick="do_something()" %}
I've got WTForms installed fine in my application, but its Django template tags isn't working for me. Does anyone have instructions on how to get this installed?
If I can't get this working, I will probably give up and switch to Jinja2 templates. The reason we have kept with Django so far is to limit the number of dependencies as much as possible.
Here's what I've tried so far:
I've install Django 1.1 locally and enabled it per the documentation.
I tried adding INSTALLED_APPS = ['wtforms.ext.django']
to my settings.py
- no effect.
I tried registering the wtforms templatetag manually:.
register = webapp.template.create_template_register()
from wtforms.ext.django.templatetags import wtforms
register.tag('form_field', wtforms.do_form_field)
This gave me an error: InvalidTemplateLibrary: Could not load template library from template_helpers, No module named django.templatetags
So I tried copying and pasting the template tags into my own code, and I got the error TemplateSyntaxError: Could not parse the remainder: ' form.foobar' from 'form_field form.foobar
.
However, I don't think the templatetag registration worked, because the error was the same without that code.
Update: I'm leaving this question up in case someone on the internet can some day answer it, but I switched to Jinja2 and now everything works perfectly. Webapp with Django templates is dead to me. ;-)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我推荐 jinja2 模板而不是 django: http://jinja.pocoo.org/ 它基于 django 模板,但更强大且易于使用使用。我认为在没有 django 堆栈的情况下使用 Django 模板不是一个好主意。另外,如果你想要一个更加结构化的框架,Flask 是一个非常好的简约框架:http://www.pocoo.org /projects/flask/#flask。我对这两个图书馆赞不绝口。我在 Django 中工作了很长时间,发现这个组合非常清爽和简洁。
PS 这应该是一个非常简单的移植过程。当我刚发现 Flask 时,我花了 10 分钟才移植到一个 Web 应用程序网站。
I recommend jinja2 templates over django: http://jinja.pocoo.org/ It's based on the django templates but more powerful and easy to use. I don't think it's a good idea to use Django templates without the django stack. Also, if you want a more structured framework, a VERY nice minimalist framework is Flask: http://www.pocoo.org/projects/flask/#flask. I can't praise those two libraries enough. I worked a long time in Django and found this combo to be very refreshing and succinct.
P.S. this should be a very simple process to port over. It took me 10 minutes to port over a webapp site when I just found out about Flask.