【问题标题】:Flask does not redirect to the canonical URL with the trailing slash on GAEFlask 不会重定向到 GAE 上带有斜杠的规范 URL
【发布时间】:2018-11-22 03:57:56
【问题描述】:

我有一个简单的 Flask 应用程序。 在本地主机上,Flask 重定向到带有斜杠的规范 URL。所以如果我访问localhost:8080/test,它会带我到localhost:8080/test/

但是,当我部署到谷歌应用引擎时, 我尝试 ***.appspot.com/test ,它只是返回 404 错误。 它不会重定向到带有斜杠的规范 URL。 我真的无法弄清楚问题所在。我使用 gae-init https://github.com/gae-init/gae-init

这是我的 app.yaml 文件

service: default
instance_class: F1
runtime: python27
api_version: 1
threadsafe: true

builtins:
- appstats: on
- deferred: on
- remote_api: on

inbound_services:
- warmup

libraries:
- name: ssl
  version: latest

error_handlers:
- file: templates/error_static.html

handlers:
- url: /favicon.ico
  static_files: static/img/favicon.ico
  upload: static/img/favicon.ico

- url: /robots.txt
  static_files: static/robots.txt
  upload: static/robots.txt

- url: /p/(.*\.ttf)
  static_files: static/\1
  upload: static/(.*\.ttf)
  mime_type: font/ttf
  expiration: "365d"

- url: /p/(.*\.woff2)
  static_files: static/\1
  upload: static/(.*\.woff2)
  mime_type: font/woff2
  expiration: "365d"

- url: /p/
  static_dir: static/
  expiration: "365d"

- url: /.*
  script: main.app
  secure: always
  redirect_http_response_code: 301

skip_files:
- ^(.*/)?#.*#
- ^(.*/)?.*/RCS/.*
- ^(.*/)?.*\.bak$
- ^(.*/)?.*\.py[co]
- ^(.*/)?.*~
- ^(.*/)?Icon\r
- ^(.*/)?\..*
- ^(.*/)?app\.yaml
- ^(.*/)?app\.yml
- ^(.*/)?index\.yaml
- ^(.*/)?index\.yml
- ^lib/.*
- ^static/dev/.*
- ^static/ext/.*\.coffee
- ^static/ext/.*\.css
- ^static/ext/.*\.js
- ^static/ext/.*\.less
- ^static/ext/.*\.json
- ^static/src/.*

这是我处理测试的方式

@app.route('/test/')
@auth.login_required
def contact_list():
  contact_dbs, contact_cursor = model.Contact.get_dbs(
      user_key=auth.current_user_key(),
    )

  return flask.render_template(
      'contact_list.html',
      html_class='contact-list',
      title='Contact List',
      contact_dbs=contact_dbs,
      next_url=util.generate_next_url(contact_cursor),
    )


The console log is 
2018-06-14 02:45:09.388 JST
301 - Moved Permanently: https://ktest321986.appspot.com/contact (/base/data/home/apps/b~ktest321986/20180613t165904.410404733871213095/control/error.py:29)
2018-06-14 02:45:09.389 JST
301 Moved Permanently: None (/base/data/home/apps/b~ktest321986/20180613t165904.410404733871213095/control/error.py:31)
Traceback (most recent call last):
  File "lib.zip/flask/app.py", line 1813, in full_dispatch_request
    rv = self.dispatch_request()
  File "lib.zip/flask/app.py", line 1791, in dispatch_request
    self.raise_routing_exception(req)
  File "lib.zip/flask/app.py", line 1774, in raise_routing_exception
    raise request.routing_exception
RequestRedirect: 301 Moved Permanently: None

你可以在这里试试: 不起作用:https://ktest321986.appspot.com/contact 工作:https://ktest321986.appspot.com/contact/

【问题讨论】:

  • 听起来像是浏览器缓存问题。您是否在任何地方设置了strict_slashesapp = Flask(__name__) ... app.url_map.strict_slashes = False
  • 我尝试了不同的浏览器,但结果相同。我在任何地方都没有 strict_slashes = False。
  • 我尝试了 gae-init 教程,如果你有时间可以按照docs.gae-init.appspot.com/tutorial 的教程来重现错误
  • This 是 GAE 中另一个项目的教程,不是 Google App Engine 团队的官方文档。不过,我不排除所描述的实现的可行性。但是,documentation 描述了使用处理程序来管理 URL 和静态文件。因此,我相信返回 404 是因为您尝试使用 App.yaml 文件中不存在的处理程序“测试”。
  • 我构建了一个在本地服务器上运行的 /test 页面,它确实返回了一个页面。

标签: google-app-engine flask


【解决方案1】:

我不知道是什么问题。 但是,当我从 Mac 部署项目时,问题就消失了。 如果我从窗口 10 部署项目,问题就来了。 太搞笑了!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-12-22
    • 1970-01-01
    • 2014-06-09
    相关资源
    最近更新 更多