Python 模板不显示GAE&;金甲2号
当我访问/admin/newpost时,我希望我的edit.html模板能够呈现和显示我的表单字段。相反,我得到了一个没有错误的空白页。然而,当我查看源代码时,我看到了以下内容Python 模板不显示GAE&;金甲2号,python,google-app-engine,jinja2,Python,Google App Engine,Jinja2,当我访问/admin/newpost时,我希望我的edit.html模板能够呈现和显示我的表单字段。相反,我得到了一个没有错误的空白页。然而,当我查看源代码时,我看到了以下内容 <Template '/edit.html'> 这是我的模板: {% extends "base.html" %} {% block title %}Testing New Post Template{% endblock %} {% block body %} <form method="pos
<Template '/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 %}
我没有使用Jinja,但是您的
render\u template
方法似乎没有实际渲染模板。它调用env.get_template
,这可能会获取一个模板对象,但随后需要调用一些东西来实际渲染该模板-快速查看,您应该执行以下操作:
template = env.get_template(template_name)
return template.render(**(template_vals or {}))
非常感谢。这似乎就是问题所在。关于这个解决方案,我不理解的一点是返回template.render(**(template_vals或{}))。这是干什么的?你具体问的是哪一点?我不懂渲染(**(部分。我假设这些与关键字参数的通配符一样。你可以看出我是Python新手。再次感谢你的帮助。它将
template\u vals
字典转换为关键字参数-相当于egtemplate.render(config='foo',post='bar')
。
application: cmwo-blog
version: 1
runtime: python27
api_version: 1
threadsafe: no
handlers:
- url: /admin/.*
script: admin.app
login: admin
- url: /favicon\.ico
static_files: favicon.ico
upload: favicon\.ico
- url: /static/([^/]+)/(.*)
static_files: themes/\1/static/\2
upload: themes/[^/]+/static/.*
- url: .*
script: static.app
builtins:
- remote_api: on
libraries:
- name: webapp2
version: "2.5.1"
- name: jinja2
version: latest
template = env.get_template(template_name)
return template.render(**(template_vals or {}))