【问题标题】:HTML output problemsHTML 输出问题
【发布时间】:2012-09-21 21:49:20
【问题描述】:

我正在用谷歌应用引擎中的 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>
  ...

【问题讨论】:

    标签: python html google-app-engine jinja2


    【解决方案1】:

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

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

    【讨论】:

      【解决方案2】:

      它是导致问题的“打印交易”。它在任何 response.write 之前输出。

      【讨论】:

      • 谢谢!消息现在消失了。知道为什么 {{ initial_city }} 没有显示在索引页面中选择的城市。我的 index.html 页面脚本是: Dealzz Home!

        我要去(选择城市):


      • 导入时正在执行打印语句。如果请求启动实例,则运行导入。实例 /handler 的缓存意味着直到下一次导入才会执行打印。您没有任何选项的“已选择”属性,因此当前不会选择任何项目。
      【解决方案3】:

      这只是 HTML 页面的headers。您应该提供更多关于您现在正在尝试的信息/代码。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2023-03-20
        • 2010-10-26
        • 1970-01-01
        • 2018-02-06
        • 2015-06-09
        • 2013-05-16
        • 2011-01-08
        • 1970-01-01
        相关资源
        最近更新 更多