Skip to content

Latest commit

 

History

History
94 lines (51 loc) · 1.36 KB

Flask.md

File metadata and controls

94 lines (51 loc) · 1.36 KB

Flask

설치

> pip install flask

app.py

from flask import Flask
app = Flask(__name__)

@app.route('/')
def home():
   return 'Flask Server'

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

html랜더링(mvc 패턴)

  1. templates폴더에 html파일 생성

  2. flask 내장함수 render_template 사용

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

html 이미지, css 랜더링

  1. static폴더 생성

  2. {{ url_for('static', filename='image.jpg') }}사용하여 url 표시

    <img src="{{ url_for('static', filename='image.jpg') }}"/>
    <link rel="stylesheet" href={{ url_for('static', filename='styple.css') }}>

GET, POST 요청

@app.route('/test', methods=['GET'])
def test_get():
   return jsonify({'result':'success', 'msg': '이 요청은 GET!'})

@app.route('/test', methods=['POST'])
def test_post():
   return jsonify({'result':'success', 'msg': '이 요청은 POST!'})

mongoDB 연동

설치

> pip install pymongo

app.py

from pymongo import MongoClient

app = Flask(__name__)

client = MongoClient('localhost', 27017)  # mongoDB는 27017 포트로 돌아갑니다.
db = client.testdb  # 'testdb'라는 이름의 db를 만들거나 사용합니다.