Flask function url_for doesn't work for sub-do

2019-07-21 04:21发布

I deployed a very simple Flask application called 'app' in a sub directory under root directory in Bluehost. Hopefully, example.com points to the homepage and example.com/app points to my Flask application. Actually, the Flask application works pretty fine when the script index.py looks like:

from flask import Flask

app = Flask(__name__)

@app.route('/', methods=['GET'])
def home():
    return 'Hello world'

if __name__ == "__main__":
    app.run()

But things went bad as I introduced a simple login functionality, with the doc structure and index.py look like:

doc structure:

public_html                     
|--app                          
     |--.htaccess
     |--index.fcgi
     |--index.py
     |--static
        |--login.html
     |--templates
        |--home.html

index.py:

from flask import Flask, url_for, request, render_template, redirect, session
app = Flask(__name__)

@app.route('/', methods=['GET'])
def home():
    if not session.get('user'):
        return redirect(url_for('login'))     #go to login page if not logined
    else:
        return render_template('home.html')   #otherwise go to home page

@app.route('/login', methods=['GET', 'POST'])
def login():
    if request.method == 'GET':
        return app.send_static_file('login.html')
    else:
        user = request.form.get('user')
        password = request.form.get('password')
        if user == 'joy' and password == 'joy':
            session['user'] = user
            return render_template('home.html')
        else:
            return 'LOGIN FAILED'

if __name__ == "__main__":
    app.run()

However, accessing example.com/app led to a changed URL as example.com/login and a reasonable 404 error as example.com/login doesn't map to any document.

return redirect(url_for('login'))

The url_for('login') return example.com/login instead of example.com/app/login. That's why the latter version of index.py doesn't work. I tried so many things but didn't came across any fix. Please help. THanks!

My .htaccess:

Options +ExecCGI
AddHandler fcgid-script .fcgi
RewriteEngine On
#RewriteBase /app/        # Neither RewriteBase / or RewriteBase /app/  work
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.fcgi/$1 [QSA,L]

My index.fcgi:

import sys
sys.path.insert(0, '/path_to_my_python_site-packages')

from flup.server.fcgi import WSGIServer
from index import app

class ScriptNameStripper(object):
   def __init__(self, app):
       self.app = app

   def __call__(self, environ, start_response):
       environ['SCRIPT_NAME'] = ''
       return self.app(environ, start_response)

app = ScriptNameStripper(app)

if __name__ == '__main__':
    WSGIServer(app).run()

2条回答
成全新的幸福
2楼-- · 2019-07-21 05:02

The following works for me now.

  1. Comment RewriteBase in .htaccess

  2. Updated index.py with a customized url_for

    from flask import Flask, redirect, url_for
    
    app = Flask(__name__)
    
    def strip_url(orig):
       return orig.replace('index.fcgi/', '')
    
    @app.route('/', methods=['GET'])
    def home():
        return redirect(strip_url(url_for('login')))
    
    @app.route('/login', methods=['GET'])
    def login():
        return 'please login'
    
    if __name__ == "__main__":
        app.run()
    

If a conclusion has to be made, I would like say official Flask fastcgi docs demands a RewriteRule to remove the ***.fcgi from the url which doesn't work for redirect initiated from within code.

查看更多
beautiful°
3楼-- · 2019-07-21 05:02

Set APPLICATION_ROOT in your Flask config:

app.config["APPLICATION_ROOT"] = "/app"

Don't use RewriteBase, Flask needs to know about the URL root to generate URLs.

查看更多
登录 后发表回答