代码之家  ›  专栏  ›  技术社区  ›  John

Flask不会重定向到GAE上带有尾随斜杠的规范URL

  •  1
  • John  · 技术社区  · 6 年前

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

    然而,当我部署到谷歌应用程序引擎时 , 我试着 ***.appspot.com/test ,它只返回404错误。 它不会重定向到带有尾随斜杠的规范URL。 我真的搞不懂这个问题。我使用gae init https://github.com/gae-init/gae-init

    这是我的应用程序。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/

    1 回复  |  直到 6 年前
        1
  •  0
  •   John    6 年前

    我不知道是什么问题。 然而,当我从Mac部署项目时,问题已经解决了。 如果我从窗口10部署项目,问题就来了。 真有趣!