关于这一点有很多问题,但过去7个小时我一直在尝试在这里写的所有内容,而且在尝试渲染最基本的时候,我仍然在Flask中遇到404错误index.html的。
编辑:我剥离了我的代码并且仍然得到了404. COde反映了最新版本......
我的代码:
初始化的.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)
答案 0 :(得分:6)
您需要在最终调用.run
的同一个烧瓶应用中定义您的路线。
将if __name__ == '__main__':
块从views.py移至__init.py__。从views.py中删除app = Flask(__name__)
行。