在部署在PythonAnywhere上的Flask应用上得到404不存在。

问题描述 投票:0回答:1

我已经翻阅了大概50个不同的答案,但还是没能解决这个问题...... 我对flask和python很陌生。

我有一个应用程序,在本地运行得很好,但我一直在努力部署在python上的任何地方。最初我有一些导入模块的问题,现在它运行但不返回任何html模板,尽管没有看到任何其他问题。我的主要问题是它无法从wsgi中找到 "路由 "应用,我算是解决了,添加了app = Flask(名称)行(短蓝图对象不可调用)。

路由.py:

from flask import Blueprint, render_template, request, redirect, send_file
import pyqrcode 
from pyqrcode import QRCode 
import subprocess

from extensions import db
from models import Link

app = Flask(__name__)

short = Blueprint('short', __name__, url_prefix='/')

@short.route('/index')
def index():
    return render_template('index.html')

init.py

from flask import Flask
from extensions import db
from routes import short

def create_app(config_file='settings.py'):

    app = Flask(__name__)

    app.config.from_pyfile(config_file)

    db.init_app(app)

    app.register_blueprint(short)

    return app

wsgi.py

import sys

# add your project directory to the sys.path
project_home = u'/home/b297py/mysite'

if project_home not in sys.path:
    sys.path = [project_home] + sys.path

# import flask app but need to call it "application" for WSGI to work
from routes import app as application

为了测试,我把所有的html模板都放在了根目录和特定的模板目录下,但这并没有解决这个问题。

python flask wsgi pythonanywhere
1个回答
1
投票

在wsgi.py中,你没有调用 create_app 所以你从来没有注册过蓝图。你应该把.替换为

from routes import app as application

用类似于:

from package_name import create_app
application = create_app() 

(例如: https:/www.pythonanywhere.comforumstopic12889#id_post_50171)

此外,正如您所提到的,在修复后增加了 app = Flask(__name__)routes.py允许你绕过create_app (所以如果你想坚持使用create_app的方法,你应该把它删除)。

© www.soinside.com 2019 - 2024. All rights reserved.