0

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.

I have an app which was running great locally but I've struggled with deploying on python anywhere. Initially I had a few import modules issues, now it runs but doesn't return any html template, despite not seeing any other issue. 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).

routes.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

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.

2
  • It would be very helpful if you could also add a screenshot with the address you're trying to get and the error message (even though it is 404) Commented Apr 26, 2020 at 8:41
  • Thanks @AlexanderPushkarev I'm getting the following message: Not Found The requested URL was not found on the server. If you entered the URL manually please check your spelling and try again. I'm trying to connect to the mysite.pythonanywhere.com, in this case b297py.pythonanywhere.com, Commented Apr 26, 2020 at 17:56

1 Answer 1

1

In wsgi.py you are not calling create_app so you are never registering the blueprint. 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)

Also as you mentioned, the fix adding app = Flask(__name__) to routes.pyallows you to bypass create_app (so you should remove it if you want to stick to the create_app approach).

Sign up to request clarification or add additional context in comments.

3 Comments

thanks @JBLaf. Do I need to import the create_app function I've created in the init.py? I've tried doing so and I get an error: ´Error running WSGI application 2020-04-26 20:45:43,441: AssertionError: View function mapping is overwriting an existing endpoint function: short.index´ 2020-04-26 20:45:43,441: File "/var/www/b297py_pythonanywhere_com_wsgi.py", line 18, in <module> 2020-04-26 20:45:43,441: application = create_app(). I've checked the Flask documentation and it seems I've got nothing off link
Yes you have to import this one. Did you delete the other Flask object creation in routes.py. You might need to have a look to blog.miguelgrinberg.com/post/… or digitalocean.com/community/tutorials/… : app/__init__.py is the one you are after (and then import it in wsgi.py with from app import create_app
thanks JBLaf. Eventually I've sorted it out recreating the virtualenv and setting up the wsgi file.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.