Python:页面从不加载,没有语法错误

时间:2014-02-21 20:03:48

标签: python html jinja2 app.yaml

我使用Jinja2在Python中创建了一个简单的网页。当我在浏览器中运行我的代码时,页面只是说加载,但从不加载。我安装了jinja2,并且我的front.html文件位于模板文件夹中。

我的日志显示:

WARNING  2014-02-21 14:17:49,151 api_server.py:341] Could not initialize images API; you are likely missing the Python "PIL" module.
INFO     2014-02-21 14:17:49,164 api_server.py:138] Starting API server at: http://localhost:49824
INFO     2014-02-21 14:17:49,168 dispatcher.py:171] Starting module "default" running at: http://localhost:11001
INFO     2014-02-21 14:17:49,171 admin_server.py:117] Starting admin server at: http://localhost:8004

我的app.yaml文件:

application: ascii
version: 1
runtime: python27
api_version: 1
threadsafe: yes

handlers:
- url: /favicon\.ico
  static_files: favicon.ico
  upload: favicon\.ico

- url: .*
  script: asciichan.app
- url: /templates
  script: front.html

libraries:
- name: webapp2
  version: "2.5.2"
- name: jinja2
  version: latest

我的Python文件:

import os
import webapp2
import jinja2

from google.appengine.ext import db

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

class Handler(webapp2.RequestHandler):
    def write(self, *a, **kw):
        self.response.out.write(*a, **kw)

    def render_str(self, template, **params):
        t = jinja_env.get_template(template)
        return t.render(params)

    def render(self, template, **kw):
        self.write(self.render_str(template, **kw))

class MainPage(Handler):
    def render_front(self, title="", art="", error=""):
        self.render("front.html", title=title, art=art, error=error)

    def get(self):
        self.render_front()

    def post(self):
        title = self.request.get("title")
        art = self.request.get("art")

        if title and art:
            self.write("thanks!")
        else:
            error = "we need both a title and some artwork!"
            self.render_front(title, art, error)

app = webapp2.WSGIApplication([('/', MainPage)], debug = True)

我的front.html文件:

<!DOCTYPE html>

<html>
    <head>
        <title>/ascii/</title>
    </head>

    <body>
        <h1>/ascii/</h1>

        <form method = "post">
            <label>
                <div>title</div>
                <input type="text" name="title" value="{{title}}">
            </label>

            <label>
                <div>art</div>
                <textarea name="art">{{art}}</textarea>
            </label>

            <div class="error">{{error}}</div>
            <input type="submit">
        </form>

    </body>
</html>

对不起,这太长了,但我对这个问题可能会感到困惑。任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:0)

它表示您的默认服务器位于:

http://localhost:11001

INFO     2014-02-21 14:17:49,168 dispatcher.py:171] Starting module "default" running  at: http://localhost:11001 

然而去:

http://localhost:11001/ 

会要求你在asciichan.py文件中有一个def索引。在浏览器中试试这个:

http://localhost:11001/MainPage 

希望这有帮助!