Flask function url_for doesn't work for sub-domain deployment on Bluehost

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-11 02:25:34

问题


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()

回答1:


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.




回答2:


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.



来源:https://stackoverflow.com/questions/29990981/flask-function-url-for-doesnt-work-for-sub-domain-deployment-on-bluehost

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!