Reputation: 449
Following microblog tutorial on Flask: http://blog.miguelgrinberg.com/post/the-flask-mega-tutorial-part-i-hello-world
In Pycharm, no matter how I structure or name the files, I cannot get the dev server to run if I separate the code and import the files. I also can't get it to inherit the codes no matter where I move the init, views, run files. The only way for me to get the server to run is to have all the commands execute on the same file. What am I doing wrong?
I have it setup as: Project 1 > app(directory) > tmp(directory) > run.py(file)
app(directory) > static(directory) > templates(directory) > init.py(file) > views.py(file) (I have tried different arrangements.)
Inside views.py: from app import app
Inside run.py: from app import app
Inside init.py: from flask import Flask from app import views
(I have tried many different combinations such as from app import app.views. from app import views as app_views. I have also tried renaming the directories/files, nothing is working.)
Upvotes: 0
Views: 533
Reputation: 290
Build the new project with PyCharm, it will create a virtual environment for you. Then put these into a run.py in a root of your a project like that (don't forget to turn debugging mode off in prod)
from app import create_app
app = create_app()
if __name__ == '__main__':
app.run(debug=True)
Set up init.py file inside you 'app':
def create_app(config_class=Config):
app = Flask(__name__)
app.config.from_object(Config)
db.init_app(app)
bcrypt.init_app(app)
login_manager.init_app(app)
mail.init_app(app)
Store your credentials into Config class:
class Config: SECRET_KEY = 'your key... ' SQLALCHEMY_DATABASE_URI = 'your db...' SQLALCHEMY_TRACK_MODIFICATIONS = False MAIL_SERVER = 'smtp.google.com' MAIL_PORT = 587 MAIL_USE_TLS = True MAIL_USERNAME = 'your email' MAIL_PASSWORD = 'email password'
Structure your project, placing an empty init.py into each directory( accordingly to your architecture). Here is an example below, how to structure your project in Flask. It runs with no problem on
.
├── README.md
├── app
│ ├── __init__.py
│ ├── config.py
│ ├── errors
│ │ ├── 403.html
│ │ ├── 404.html
│ │ ├── 500.html
│ │ ├── __init__.py
│ │ └── handlers.py
│ ├── main
│ │ ├── __init__.py
│ │ └── routes.py
│ ├── models.py
│ ├── posts
│ │ ├── __init__.py
│ │ ├── forms.py
│ │ └── routes.py
│ ├── site.db
│ ├── static
│ │ ├── main.css
│ │ └── profile_pics
│ │ ├── 3c4feb2bb50d90df.png
│ │ ├── ba3d328163a8125e.png
│ │ └── default.jpg
│ ├── templates
│ │ ├── about.html
│ │ ├── account.html
│ │ ├── create_post.html
│ │ ├── home.html
│ │ ├── layout.html
│ │ ├── login.html
│ │ ├── post.html
│ │ ├── register.html
│ │ ├── reset_request.html
│ │ ├── reset_token.html
│ │ └── user_posts.html
│ └── users
│ ├── __init__.py
│ ├── forms.py
│ ├── routes.py
│ └── utils.py
└── run.py
Upvotes: 1