기본 콘텐츠로 건너뛰기

Flask 17. CRUD

Flask 17. CRUD

Read => GET

Update => PUT

Delete => DELETE

@api.route('/users/', methods=['GET', 'PUT', 'DELETE']) def user_detail(uid): if request.method == 'GET': # GET은 조회(read) user = Fcuser.query.filter(Fcuser.id == uid).first() # first로 하나만 뽑아낸다. return jsonify(user.serialize) # user는 객체이므로 serialize하게 만들어야한다. elif request.method == 'DELETE' : # DELETE는 삭제(delete) Fcuser.query.delete(Fcuser.id == uid) return jsonify(), 204 # PUT은 수정(update) # json데이터는 request.form이 아닌 request.get_json()을 통해 얻을 수 있다. updated_data = request.get_json() # PUT요청은 수정이 돼야하기 때문에 filter를 해야한다. Fcuser.query.filter(Fcuser.id == uid).update(updated_data) user = Fcuser.query.filter(Fcuser.id == uid).first() return jsonify(user.serialize)

url에 uid가 user_datail(uid) 함수의 인자값으로 들어간다.

from http://ohdowon064.tistory.com/133 by ccl(A) rewrite - 2020-03-23 17:20:12

댓글

이 블로그의 인기 게시물

Flask 13. pythonanywhere에 배포하기

Flask 13. pythonanywhere에 배포하기 Login: PythonAnywhere It's always a pleasure to hear from you! Ask us a question, or tell us what you love or hate about PythonAnywhere. We'll get back to you over email ASAP. Sorry, there was an error connecting to the server. Please try again in a few moments... www.pythonanywhere.com from http://ohdowon064.tistory.com/124 by ccl(A) rewrite - 2020-03-11 15:54:10

외래어 정리

외래어 정리 [A] acacia 아카시아 academic 아카데믹 academy 아카데미 acanthus 아칸서스 accelerator 액셀러레이터 accent 악센트 acceptor 억셉터 access 액세스 accessory 액세서리 accordion 아코디언 ace 에이스 acetate 아세테이트 acetaldehyde 아세트알데히드 acetic acid 아세트(산) acetone 아세톤 acetyl 아세틸 acetylene 아세틸렌 Achilles tendon 아킬레스(건) acre 에이커 acrylic acid 아크릴(산) action 액션 active 액티브 acyl 아실 AD 에이디 adagio 아다지오 adapter 어댑터 ad + balloon 애드벌룬 address 어드레스 adenine 아데닌 adrenaline 아드레날린 advantage 어드밴티지 aerobic dance 에어로빅 댄스 aerofoil 에어로포일 aerosol 에어로졸 afghan 아프간 [편물] after + service 애프터서비스 agape 아가페 Ainu 아이누 air conditioner 에어컨(디셔너) airspray 에어스프레이 album 앨범 albumin 알부민 alcohol 알코올 aldehyde 알데히드 ALGOL 알골 algorism 알고리즘 alibi 알리바이 alkali 알칼리 alkaloid 알칼로이드 Allah 알라 allegory 알레고리 allegretto 알레그레토 allegro 알레그로 alleluia 알렐루야 Allergie 알레르기 alligator 앨리게이터 all-in-one 올인원 almond 아몬드 aloha 'oe 알로하 오에 Alpenhorn 알펜호른 alpha 알파 alphabet 알파벳 ...