I currently working on a flask backend but have hit an issue where all routes return a 404 despite the routes.py apparently running.
routes.py:
import sys, os, json
from flask import Flask, render_template
from flask_cors import CORS, cross_origin
app = Flask(__name__)
app.config.from_pyfile('config.py')
CORS(app)
@app.route('/', endpoint='index')
@app.route('/index', methods=['GET'], endpoint='index')
@cross_origin()
def index():
print('Hello world!', file=sys.stderr)
return render_template('index.html', title='home')
@app.route('/projects', endpoint='projects') # route for projects showcase including json project data loading
@cross_origin()
def projects():
projects = os.path.join(app.static_folder, 'data', 'projects.json')
with open(projects) as pData:
data = json.load(pData)
return render_template('projects.html', title='showcase', data=data)
@app.route('/contact', endpoint='contact')
def contact():
return render_template('contact.html', title='contact')
print('Hello routes!', file=sys.stderr) #testing line
When I launch it the output is this:
* Serving Flask app 'ClayG' (lazy loading)
* Environment: development
* Debug mode: on
Hello routes!
* Restarting with stat
* Debugger is active!
Hello routes!
* Debugger PIN: 274-753-929
* Running on http://127.0.0.1:5000/
the print function at the end of routes.py runs so my app is making it through, but when i visit localhost:5000 I get a 404 with only this in the terminal output
127.0.0.1 - - [05/Feb/2022 17:20:29] "GET / HTTP/1.1" 404 -
The other files and file structure
.
├── ClayG.py
├── .flaskenv
├── app
│ ├── config.py
│ ├── routes.py
│ └── __init__.py
ClayG.py
import os, sys
sys.path.append(os.path.join(os.path.dirname(os.path.realpath(__file__))))
from app import app
.flaskenv
FLASK_APP=ClayGale
FLASK_ENV=development
init.py
from flask import Flask
from . import routes
app = Flask(__name__)
config.py
class Config:
STATIC_FOLDER = 'static'
TEMPLATES_FOLDER = 'templates'
DEBUG = True
TESTING = True
One thing i've noticed is that adding FLASK_ENV = 'development' to the config class is that it still launches as production and I can only get it to launch in development with the .flaskenv file. I'm unfortunately just stumped as for what to do from here so any wisdom on what to try would be helpful.
CodePudding user response:
You have created two Flask apps. One in routes.py
with the endpoints, and a new one in __init__.py
which is completely empty. You should use the previously created app in the __init__.py
file:
from . import routes
app = routes.app