简体   繁体   English

在 PythonAnywhere 上部署的 Flask 应用程序上找不到 404

[英]Getting 404 not found on Flask app deployed on PythonAnywhere

I've rummaged through maybe 50 different answers to this and still I haven't manage to fix it... I'm pretty new to flask and python.我已经翻遍了大约 50 个不同的答案,但我仍然无法解决它......我对 flask 和 python 还是很陌生。

I have an app which was running great locally but I've struggled with deploying on python anywhere.我有一个在本地运行良好的应用程序,但我很难在任何地方部署 python。 Initially I had a few import modules issues, now it runs but doesn't return any html template, despite not seeing any other issue.最初我遇到了一些导入模块问题,现在它运行但不返回任何 html 模板,尽管没有看到任何其他问题。 The main issue I had was that it couldn't find the "routes" app from wsgi, and I sort of fixed adding the app = Flask( name ) line on routes.py (the short blueprint object is not callable).我遇到的主要问题是它无法从 wsgi 中找到“路线”应用程序,并且我修复了在 routes.py 上添加 app = Flask( name ) 行(短蓝图 object 不可调用)。

routes.py:路线.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初始化.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 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

For the purpose of testing, I've placed all the html templates both in the root directory and in the specific /template directory but it just didn't fix the issue.出于测试的目的,我将所有 html 模板都放在了根目录和特定的 /template 目录中,但这并没有解决问题。

In wsgi.py you are not calling create_app so you are never registering the blueprint.在 wsgi.py 中,您没有调用create_app ,因此您永远不会注册蓝图。 You should replace:你应该更换:

from routes import app as application

by something like:通过类似的东西:

from package_name import create_app
application = create_app() 

(Example: https://www.pythonanywhere.com/forums/topic/12889/#id_post_50171 ) (例如: https://www.pythonanywhere.com/forums/topic/12889/#id_post_50171

Also as you mentioned, the fix adding app = Flask(__name__) to routes.py allows you to bypass create_app (so you should remove it if you want to stick to the create_app approach).同样正如您所提到的,将app = Flask(__name__)添加到routes.py的修复程序允许您绕过 create_app (因此,如果您想坚持使用 create_app 方法,则应将其删除)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM