【问题标题】:Python: Page never loads, no syntax errorsPython:页面从不加载,没有语法错误
【发布时间】:2014-03-23 12:57:25
【问题描述】:

我使用 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>

抱歉,这太长了,但我对可能出现的问题感到困惑。任何帮助将不胜感激。

【问题讨论】:

  • 你要去哪个网址?
  • 刚刚从我的本地主机运行,还没有具体的 url。
  • 我有其他项目(我正在使用 Google App Engine)在日志中都给了我相同的 PIL 模块警告,但这些工作正常。只是这个项目不会加载。
  • 你要去哪个端口的url?
  • 试试这个http://localhost:8080/

标签: python html jinja2 app.yaml


【解决方案1】:

它说您的默认服务器位于:

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 

希望这会有所帮助!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-10-03
    • 1970-01-01
    • 1970-01-01
    • 2012-09-01
    • 2015-09-22
    • 1970-01-01
    相关资源
    最近更新 更多