【问题标题】:All I get is a blank page when I visit localhost:9080当我访问 localhost:9080 时,我得到的只是一个空白页面
【发布时间】:2015-07-14 19:23:56
【问题描述】:

我一直在使用 udacity.com 来学习编程,但我的代码(python)遇到了一些问题。该应用程序是发布的,以允许记下您出生的那一天,当您推送提交时,它的发布是说“谢谢,这是一个完全有效的日子!”。它没有这样做。应用程序再次重新启动。有人能告诉我为什么 is 语句不起作用以及如何解决它。

import webapp2
form="""

    <form>
        What is your birthday?
        <br>
        <label>Month<input type="type" name="month"></label>
        <label>Day<input type="type" name="day"></label>
        <label>Year<input type="type" name="year"></label>
        <div style="color: red">%(error)s</div>
        <br>
        <br>
        <input type="submit">
    </form>

"""
class MainPage(webapp2.RequestHandler):
    def write_form(self, error=""):
        self.response.out.write(form % {"error": error} )
    def get(self):
        self.write_form()

        def post(self):
            user_month = valid_month(self.request.get('month'))
            user_day = valid_day(self.request.get('day'))
            user_year = valid_year(self.request.get('year'))

            if not (user_month and user_month and user_year):
                self.write_form("That doesn't look valid to me, friend.")
            else:
                self.response.out.write("Thanks! That's a totally valid day!")


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

【问题讨论】:

  • 您的配置很可能是不正确的,但这只是猜测,因为其他人尝试回答也是如此。这里没有具体的问题来实际回答,它基本上说我的东西不起作用。您的问题不是其他人可以解决的,而是您缺乏信息。 不鼓励链接到外部资源,尤其是第三部分托管的图像,因为它会导致死链接和无用的问题和答案。
  • 我的问题是,为什么将要发布的一段代码放入 Google 应用引擎 locahost 时会出现空白。恳请各位大侠帮忙看看有没有错误或遗漏类型。
  • 通常 App Engine 端口是 8080 而不是 9080。
  • 我把 localhost 改成 8080 还是空白
  • 你们有在谷歌应用引擎上使用python的经验

标签: python-2.7 google-app-engine


【解决方案1】:

该片段有语法错误。如果您正在查看终端/控制台,您会注意到 Python 抱怨 else(缺少尾随 :)。如果你想取得进步,观察控制台并理解它在说什么是关键。

固定后,您将看到表单。然后,当您尝试发布时,您将收到“405 Method not allowed”,因为 post 方法过度缩进。

【讨论】:

  • 我如何不过度缩进帖子?
  • Python 对缩进非常敏感。在您发布的片段中,post 方法是get 的私有方法。如果您从def post 开始删除缩进,使其与def get 处于同一级别,则post 方法将对路由可见。这将解决 405 错误。
  • 现在代码看起来还可以吗?这是代码的链接:plus.google.com/u/1/115545843446144625696/posts/…
  • (self) 前面缺少def post。否则,找。您还可以编辑您的原始帖子。
  • 它可以工作,但是当我推送提交时它不会转到“”谢谢!那是完全有效的一天!""。我在哪里错过了代码?
【解决方案2】:

将 method="post" 添加到您的表单标签中,就在文本“你的生日是什么时候?”上方。您正在使用 post 方法将数据发布到表单,但按钮提交不知道您正在使用 post,因为标签中没有 method="post" 。

还可以在此处查看我对您的其他相关帖子之一的回答。 python post and get commands

【讨论】:

    猜你喜欢
    • 2015-07-16
    • 1970-01-01
    • 2012-03-17
    • 1970-01-01
    • 2010-12-06
    • 1970-01-01
    • 2020-01-24
    • 1970-01-01
    • 2021-12-26
    相关资源
    最近更新 更多