Reputation: 1073
There are a number of questions about this, but I have spent the past 7 hours trying everything that's written up here on SO, and I am still getting a 404 error in Flask when trying to render the most basic index.html.
Edited: I stripped my code down and am still getting a 404. COde reflects newest version...
My code:
init.py
import os
from flask import Flask
from flask.ext.sqlalchemy import SQLAlchemy
from config import basedir
app = Flask(__name__)
app.config.from_object('config')
db = SQLAlchemy(app)
from app import views, models
Models.py
from app import db
from sqlalchemy import create_engine
from sqlalchemy import Column, Integer
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import sessionmaker, relationship, scoped_session
engine = create_engine('sqlite:///bikes.db', convert_unicode=True)
session = scoped_session(sessionmaker(autocommit=False,
autoflush=False,
bind=engine))
Base = declarative_base()
Base.query = session.query_property()
class User(db.Model):
__tablename__="user"
id = db.Column(db.Integer, primary_key = True)
name = db.Column(db.String(15))
email = db.Column(db.String(25))
age= db.Column(db.Integer)
def main():
Base.metadata.create_all(ENGINE)
if __name__ == "__main__":
main()
views.py
from flask import Flask, render_template, session
from app.models import session as db_session
from app import app, db
app = Flask(__name__)
@app.route('/')
def index():
return "Hello, World!"
if __name__ == "__main__":
app.run(debug=True)
Upvotes: 2
Views: 960
Reputation: 127180
You need to define your routes on the same flask app that you eventually call .run
on.
Move the if __name__ == '__main__':
block from views.py to __init.py__. Remove the app = Flask(__name__)
line from views.py.
Upvotes: 6