Skip to content

Instantly share code, notes, and snippets.

@oiojin831
Last active September 20, 2015 23:56
Show Gist options
  • Save oiojin831/634671a2638204ce938d to your computer and use it in GitHub Desktop.
Save oiojin831/634671a2638204ce938d to your computer and use it in GitHub Desktop.
Flask 설정

APP_SETTINGS=config.DevelopmentConfig
config란는 모듈에서 DevelopmentConfig라는게 있는거임

app= Flask(name)
app.config 하면 Flask에서 제공하는 config변수임

$VIRTUAL_ENV/bin/postactivate
여기에 env변수 설정 가능
export APP_SETTINGS="config.DevelopmentConfig"
export DATABASE_URL="postgresql://localhost/이름"

check database

psql
CREATE DATABASE db이름
check the database naem maybe you need to source $VIRTUAL_ENV/bin/postactivate
python db_create.py

psql
\c fourm_dev (db name) \dt

python
from models import BlogPost
posts = BLogPost.query.all()
posts

from app import db
db.session.add(BlogPost("fjdklas","afjldk fadlkj")
db.session.commit //여기서도 등록할수있음

pip install psycopg2 //postgres 위한 설치;

heroku depoly

gunicorn 설치
gunicorn -b 0.0.0.0:4000 app:app 로 테스트해볼수있음
Procfile 만들어야함 - heroku가 app을 어떻게 실행해야하는지 이파일로 인식함

heroku create nameOfServer  
git push heroku master

heroku config:set APP_SETTINGS=config.ProductionConfig --remote heroku   
heroku addons:add heroku-postgresql:dev 
//이커맨드로 나온 대문자 디비 이름 
heroku pg:promote 대문자 디비이름  
heroku run python run.py //테스트 하기 
//500 이 생김
//data가 없어서 그럼 
//psycopg2 설치후  
heroku run python db_create.py  
heroku run python
from app import db  
from models import BlogPost  
db.session.add(BlogPost('hi','hihi'))  
db.session.commit()  

도움이 될만한 명령어들 in heroku

heroku ps:scale web=1 ///adding dyno- run processes 여기서 web command는 Procfile에서 쓴거랑 같은거임  
heroku ps //확인 뭐가 돌아가나  
heroku config | grep HEROKU_POSTGRESQL  //환경 변수 확인하기  

flask-migrate

touch manage.py
add codes form discoverflsk
python manage.py db init //make folders and stuffs
python manage.py db migrate //make migration script
//migrations/versions/ will have migration script which includes upgrading and downgrading mechnism
python manage.py db update //using update method in script

then add user and add user field in posts

insert into users values (1, 'admin','[email protected]','admin','corzej');
select * from users;
update posts set author_id = 1; 

it will add user to all the posts

heroku migration

heroku run python manage.py db upgrade //since we have script files
//need to upgrade the data
heroku pg:psql
// you don't have to do \c fourm_dev just do the \dt to check table
insert into users values...
same with local....

to read libraries

psycopg2
flask-migrate alembic

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment