2
我得到我的表單以使用Google App Engine上的WTForm model_form來顯示。我希望看到顯示的字段,當我訪問/管理/ newpostWTForm GAE model_field不會呈現Jinja2模板中的字段
我的模型
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 %}
總之,怎麼我得到的第一個版本,以顯示在我的示範田形成?