python - HTML 输出问题

标签 python html google-app-engine jinja2

我正在用 python 和 jinja2 在谷歌应用引擎中编写一个网站。我的 html 页面在页面上显示了这个:

Status: 200
Content-Type: text/html;
charset=utf-8
Cache-Control: no-cache
Content-Length: 432

知道这个问题是从哪里来的吗?另外,为什么 {{ initial_city }} 不显示结果?

我的 main.py 脚本是:

from google.appengine.ext import db
from google.appengine.api import memcache
from models import Deal
import os
import webapp2
import jinja2


#databasestuff
deal = Deal(title='Hello',
        description='Deal info here',
        city='New York')
deal.put()
print deal


jinja_environment = jinja2.Environment(autoescape=True,
    loader=jinja2.FileSystemLoader(os.path.join(os.path.dirname(__file__), 'templates')))

class MainPage(webapp2.RequestHandler):
def get(self):
    template = jinja_environment.get_template('index.html')
    self.response.out.write(template.render())


class DealHandler(webapp2.RequestHandler):
def get(self):
    def get_deals(choose_city, update=False):
        key = str(choose_city)
        all_deals = memcache.get(key)
        return all_deals

    choose_city = self.request.get('c')
    try:
        initial_city = str(choose_city)
        choose_city = initial_city
    except ValueError:
        initial_city = 0
        choose_city = initial_city


    template = jinja_environment.get_template('deal.html')
    self.response.out.write(template.render())




class ContentHandler(webapp2.RequestHandler):
def get(self):
    template = jinja_environment.get_template('content.html')
    self.response.out.write(template.render())

app = webapp2.WSGIApplication([('/', MainPage),
                           ('/deal', DealHandler),
                           ('/content', ContentHandler)],
                          debug=True)

我的 html 页面:

<!DOCTYPE html>
<html>
  <head>
    <title>
      Deallzz: Chosen City: {{ initial_city }}
    </title>
  </head>
  ...

最佳答案

要显示 initial_city,使用 render 方法将变量传递给模板:

self.response.out.write(template.render(initial_city = initial_city))

关于python - HTML 输出问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12666618/

相关文章:

python - 使用 py.test 但在命令行上排除测试列表?

html - CSS - 为什么文件上传控件不遵循 CSS 宽度规则?

python - 如何为 Google App Engine 编写 Web 服务?

python - 交换numpy数组的维度

python - 我们可以用openERp计算净工资吗

python - 我无法导入 tensorflow-gpu

html - CSS变量到SASS变量=不透明度函数的问题

asp.net - 关于响应式网页设计的建议

python - 如何使用 WSGI 将用户从 http 重新路由到 https

python - 使用 Appengine 用 python 解码 JSON