6.읽기
from flask import Flask import random app = Flask(__name__) topics = [ {'id': 1, 'title': 'html', 'body': 'html is ...'}, {'id': 2, 'title': 'css', 'body': 'css is ...'}, {'id': 3, 'title': 'javascruot', 'body': 'javascript is ...'} {'id': 3, 'title': 'javascript', 'body': 'javascript is ...'} ] @app.route('/') def index(): liTags = '' for topic in topics: liTags = liTags + f'
WEB
-
{liTags}
{contents}
Welcome
Hello, Web {content} ''' @app.route('/create/') def create(): return 'Create' def getContents(): liTags = '' for topic in topics: liTags = liTags + f'Welcome
Hello, WEB') @app.route('/read/{title}
{body}') @app.route('/create/') def create(): return 'Create' app.run(debug=True)템플릿 함수를 이용하여 중복을 해결한다.
7-1.쓰기
from flask import Flask app = Flask(__name__) topics = [ {'id': 1, 'title': 'html', 'body': 'html is ...'}, {'id': 2, 'title': 'css', 'body': 'css is ...'}, {'id': 3, 'title': 'javascript', 'body': 'javascript is ...'} ] def template(contents, content): return f'''
WEB
-
{contents}
Welcome
Hello, WEB') @app.route('/read/{title}
{body}') @app.route('/create/') def create(): return 'Create' content = ''' ''' return template(getContents(), content) app.run(debug=True)CRUD는 create, Read, Update, Delete의 약자이다. template에 <a>를 추가하고 create를 구현한다.
7-2.쓰기
from flask import Flask from flask import Flask, request, redirect app = Flask(__name__) nextId = 4 topics = [ {'id': 1, 'title': 'html', 'body': 'html is ...'}, {'id': 2, 'title': 'css', 'body': 'css is ...'}, {'id': 3, 'title': 'javascript', 'body': 'javascript is ...'} ] def template(contents, content): return f'''
WEB
-
{contents}
Welcome
Hello, WEB') @app.route('/read/{title}
{body}') @app.route('/create/') @app.route('/create/', methods=['GET', 'POST']) def create(): content = ''' ''' return template(getContents(), content) if request.method == 'GET': content = ''' ''' return template(getContents(), content) elif request.method == 'POST': global nextId title = request.form['title'] body = request.form['body'] newTopic = {'id': nextId, 'title': title, 'body': body} topics.append(newTopic) url = '/read/'+str(nextId)+'/' nextId = nextId + 1 return redirect(url) app.run(debug=True)get을 이용한다.
8.수정
from flask import Flask, request, redirect app = Flask(__name__) nextId = 4 topics = [ {'id': 1, 'title': 'html', 'body': 'html is ...'}, {'id': 2, 'title': 'css', 'body': 'css is ...'}, {'id': 3, 'title': 'javascript', 'body': 'javascript is ...'} ] def template(contents, content): def template(contents, content, id=None): contextUI = '' if id != None: contextUI = f'''
WEB
-
{contents}
- create {contextUI}
Welcome
Hello, WEB') @app.route('/read/{title}
{body}') return template(getContents(), f'{title}
{body}', id) @app.route('/create/', methods=['GET', 'POST']) def create(): if request.method == 'GET': content = ''' ''' return template(getContents(), content) elif request.method == 'POST': global nextId title = request.form['title'] body = request.form['body'] newTopic = {'id': nextId, 'title': title, 'body': body} topics.append(newTopic) url = '/read/'+str(nextId)+'/' nextId = nextId + 1 return redirect(url) @app.route('/update/template를 수정하고 update를 만든다.
9.삭제
from flask import Flask, request, redirect app = Flask(__name__) nextId = 4 topics = [ {'id': 1, 'title': 'html', 'body': 'html is ...'}, {'id': 2, 'title': 'css', 'body': 'css is ...'}, {'id': 3, 'title': 'javascript', 'body': 'javascript is ...'} ] def template(contents, content, id=None): contextUI = '' if id != None: contextUI = f'''
WEB
-
{contents}
- create {contextUI}
Welcome
Hello, WEB') @app.route('/read/{title}
{body}', id) @app.route('/create/', methods=['GET', 'POST']) def create(): if request.method == 'GET': content = ''' ''' return template(getContents(), content) elif request.method == 'POST': global nextId title = request.form['title'] body = request.form['body'] newTopic = {'id': nextId, 'title': title, 'body': body} topics.append(newTopic) url = '/read/'+str(nextId)+'/' nextId = nextId + 1 return redirect(url) @app.route('/update/post를 사용하고 delete를 추가한다.