我可以使用Google App Engine上的WTForm model_form显示我的表单。我希望在访问/ admin / newpost
时看到显示的字段我的模特
class BlogPost(db.Model):
#The URL path to the blog post. Posts have a path if they are published.
path = db.StringProperty()
title = db.StringProperty(required=True, indexed=False)
body = db.TextProperty(required=True)
published = db.DateTimeProperty(auto_now_add=True)
updated = db.DateTimeProperty(auto_now=True)
我的PostHandler
class PostHandler(webapp2.RequestHandler):
@webapp2.cached_property
def jinja2(self):
return jinja2.get_jinja2(app=self.app)
def render_to_response(
self,
template_name,
template_vals=None,
theme=None
):
template_name = "admin/%s" % template_name
self.response.out.write(render_template(
template_name, template_vals, theme))
def render_form(self, form):
self.render_to_response("edit.html", {'form': form()})
def get(self):
form = model_form(BlogPost)
self.render_form(form)
不起作用的Edit.html模板
{% extends "base.html" %}
{% block title %}New Post{% endblock %}
{% block body %}
<form method="post" action="">
<table>
{{form}}
</table>
<input type="submit" />
</form>
{% endblock %}
使用此模板,我看到带有表单按钮的html。问题是没有显示模型字段。要让我的字段显示,我必须这样做......
可运行的Edit.html模板
{% extends "base.html" %}
{% block title %}Testing New Post Template{% endblock %}
{% block body %}
<form method="post" action="">
<table>
<div>{{ form.title.label }}: {{ form.title(class="css_class") }}</div>
{% if form.title.errors %}
<ul class="errors">{% for error in form.name.errors %}<li>{{ error }}</li>{% endfor %}</ul>
{% endif %}
<div>{{ form.body.label }}: {{ form.body() }}</div>
{% if form.body.errors %}
<ul class="errors">{% for error in form.body.errors %}<li>{{ error }}</li>{% endfor %}</ul>
{% endif %}
</table>
<input type="submit" />
</form>
{% endblock %}
简而言之,如何让第一个版本在表单中显示我的模型字段?