2

私はグーグルアプリエンジンで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>
  ...
4

3 に答える 3

3

initial_cityを表示するには、renderメソッドを使用して変数をテンプレートに渡します。

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

于 2012-10-01T02:02:50.903 に答える
2

問題を引き起こしているのは「印刷取引」です。response.writeの前に出力されます

于 2012-10-01T01:56:08.870 に答える
1

これは、HTMLページの単なるヘッダーです。あなたはあなたが今試みていることのより多くの情報/コードを提供するべきです。

于 2012-10-01T01:35:56.880 に答える