Rahul Bali
Rahul Bali

Reputation: 752

Flask directory Structure

I have flask project which work on presently. (below)

Flask project directory

When I run this project using command

python run.py

I get following error.

Traceback (most recent call last): File "run.py", line 1, in from site import app ImportError: cannot import name 'app'

run.py

from site import app
import os

app.secret_key = os.urandom(24)
port = int(os.environ.get('PORT', 5000))


if __name__ == '__main__':
    app.run(debug="True")
# app.run(host='0.0.0.0', port=port)

__init__.py

from .views import app
from .models import db


db.create_all()

app.config['SQLALCHEMY_DATABASE_URI'] = 'postgresql://postgres:password@localhost:5432/db'
app.config['SECURITY_REGISTERABLE'] = True

views.py

from flask import Flask
from .models import User, db
from flask import render_template, request, redirect, url_for
from flask.ext.security import login_required

app = Flask(__name__, static_url_path='')

@app.route('/')
def index():
    return render_template('index.html')


@app.route('/profile/<email>')
@login_required
def user_index(email):
    user = User.query.filter_by(email=email).first()
    return render_template('profile.html', user=user)


@app.route('/post_user', methods=['POST'])
def post_user():
    if request.form["action"] == "submit_btn":
        user = User(request.form['username'], request.form['email'])
        db.session.add(user)
        db.session.commit()
        return redirect(url_for('index'))

models.py

from flask import Flask
from flask.ext.mail import Mail, Message
from flask_sqlalchemy import SQLAlchemy
from flask.ext.security import Security, SQLAlchemyUserDatastore, UserMixin, RoleMixin

app = Flask(__name__, static_url_path='')
db = SQLAlchemy(app)
mail = Mail()
class User(db.Model, UserMixin):
    id = db.Column(db.Integer, primary_key=True)
    email = db.Column(db.String(255), unique=True)
    password = db.Column(db.String(255))
    active = db.Column(db.Boolean())
    confirmed_at = db.Column(db.DateTime())
    roles = db.relationship('Role', secondary=roles_users,
                            backref=db.backref('users', lazy='dynamic'))

What should be the directory structure? Also how should I import the models and views in order to make the server work?

Please tell me if you need any other info, thanks in advance.

Upvotes: 1

Views: 5013

Answers (1)

lapinkoira
lapinkoira

Reputation: 8978

Rename the site's name so python dont try to import the site standard library, also is better to define the app inside the init.py file: Docs

Upvotes: 1

Related Questions