본문 바로가기

Programming/Python_Web26

Flask강좌5 - Flask form입력 form을 이용해 입력을 받고, 처리하여, 다시 결과를 보여주는 로직을 구현하겠습니다. 1. localhost:5000/all의 db.html 화면에서 입력 받고, 2. __init__.py에서 라우팅 처리하고, 3. db.html에 결과를 보여줍니다. 1. __init__.py form 으로부터 전달된 이름('nm')을 받아서 temp 변수에 저장하고, query.filter_by로 DB에서 조회한 후, 결과를 members로 저장합니다. 그리고 members의 갯수를 count에 저장합니다. render_template함수를 통해 db.html로 연결이 되는데, 이때 members와 count를 함께 넘겨줍니다. from flask import Flask, render_template, request.. 2020. 12. 3.
Flask강좌4 - Flask_SQLAlchemy MySQL연동 이번 강좌에서는 ORM 방식으로 Database와 연동하는 방법에 대해 포스팅하겠습니다. ORM은 Object-Relational Mapping 이라고 하여, Database 객체를 객체지향 프로그래밍 언어로 변환하는 기법입니다. (자세한 내용은 다른 포스팅들을 참조하시기 바랍니다.) 그 중에서도 Flask에서 ORM을 구현할 수 있게 해주는 라이브러리가 Flask-SQLAlchemy 입니다. 이 Flask-SQLAlchemy를 이용하여 MySql과 연동해보도록 하겠습니다. 1. Database 자료 생성 아래와 같이 id, name, email, phone, start(datetime), end(datetime)을 필드로 하는 자료를 생성해놓습니다. 2. 라이브러리 설치 먼저 말했듯이 이번 강좌의 메인.. 2020. 12. 2.
Flask강좌3 - Request 1. Request Event Handler 주요 Request Event Handler(Web Filter)는 아래와 같습니다. @app.before_first_requst : 첫번째 요청을 부를 때 @app.before_request : 매 요청에 대해 router가 받아서 모델이 처리하기 전에 @app.after_request : 응답이 나가기 직전에 (DB Close와 같은 작업 처리) @app.teardown_request : 응답이 나가고 나서 @app.teardown_appcontext : application Context가 끝났을 때 2. Request Parameter # GET request.args.get('q') .....> q파라미터를 GET으로 받음 # POST request... 2020. 12. 1.
Flask강좌2 - Global Object: g, Response객체 1. __init__.py 수정 from flask import Flask, g, Response, make_response app = Flask(__name__) app.debug = True #use only debug!! @app.before_request def before_request(); print("before_request!!!") g.str = "한글" @app.route("/gg") def helloworld(): return "Hello World!" +getattr(g, 'str', '111') @app.route("/") def helloworld(): return "Hello Flask World!!!!!!!" @app.route("/res1") def res1(): custo.. 2020. 12. 1.
Flask강좌1 - hello flask! 설치 pip install flask 셋업 webapp (Web application) /helloflask(Web context영역, blog 등) - /static -/css -/images -/js -/templates -application.html -__init__.py (임포트 하는 순간 자동 실행 됨) start_helloflask.py __init__.py from flask import Flask app = Flask(__name__) @app.route("/") def helloworld(): return "Hello Flask!" start_helloflask.py #../start_flask.py from helloflask import app app.run(host='localho.. 2020. 12. 1.
Flask - IIS연동 IIS와 Flask 연동하는 방법에 대한 기록을 남깁니다. 1. flask 설치 pip install flask 2. wfastcgi설치 pip install wfastcgi 3. wfastcgi실행 c:\>wfastcgi-enable python.exe | wfastcgi.py 파일 경로 복사/기록 4. 테스트용 hello.py파일 작성( 예: E:\02_Programming_Work\06_Python\08_flask) 주소창에서 "localhost:7000/" 과같이 입력하면 이 파일에 의해서 Hello Flask!라는 텍스트를 나타내게 합니다. #hello.py from flask import Flask app=Flask(__name__) @app.route('/',methods=['GET']) d.. 2020. 10. 23.