본문 바로가기

웹해킹

8주차 웹해킹

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'
  • {topic["title"]}
  • ' def template(contents, content): return 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'
  • {topic["title"]}
  • ' return liTags @app.route('/read//') @app.route('/') def index(): return template(getContents(), '

    Welcome

    Hello, WEB') @app.route('/read//') def read(id): print(id) return 'Read '+id title = '' body = '' for topic in topics: if id == topic['id']: title = topic['title'] body = topic['body'] break return template(getContents(), f'

    {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}
    {content}
    ''' def getContents(): liTags = '' for topic in topics: liTags = liTags + f'
  • {topic["title"]}
  • ' return liTags @app.route('/') def index(): return template(getContents(), '

    Welcome

    Hello, WEB') @app.route('/read//') def read(id): title = '' body = '' for topic in topics: if id == topic['id']: title = topic['title'] body = topic['body'] break return template(getContents(), f'

    {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}
    {content}
    ''' def getContents(): liTags = '' for topic in topics: liTags = liTags + f'
  • {topic["title"]}
  • ' return liTags @app.route('/') def index(): return template(getContents(), '

    Welcome

    Hello, WEB') @app.route('/read//') def read(id): title = '' body = '' for topic in topics: if id == topic['id']: title = topic['title'] body = topic['body'] break return template(getContents(), f'

    {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'''
  • update
  • ''' return f'''

    WEB

      {contents}
    {content}
    ''' def getContents(): liTags = '' for topic in topics: liTags = liTags + f'
  • {topic["title"]}
  • ' return liTags @app.route('/') def index(): return template(getContents(), '

    Welcome

    Hello, WEB') @app.route('/read//') def read(id): title = '' body = '' for topic in topics: if id == topic['id']: title = topic['title'] body = topic['body'] break return template(getContents(), f'

    {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//', methods=['GET', 'POST']) def update(id): if request.method == 'GET': title = '' body = '' for topic in topics: if id == topic['id']: title = topic['title'] body = topic['body'] break content = f'''

    ''' return template(getContents(), content) elif request.method == 'POST': global nextId title = request.form['title'] body = request.form['body'] for topic in topics: if id == topic['id']: topic['title'] = title topic['body'] = body break url = '/read/'+str(id)+'/' return redirect(url) app.run(debug=True)

    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'''
  • update
  • ''' return f'''

    WEB

      {contents}
    {content}
    ''' def getContents(): liTags = '' for topic in topics: liTags = liTags + f'
  • {topic["title"]}
  • ' return liTags @app.route('/') def index(): return template(getContents(), '

    Welcome

    Hello, WEB') @app.route('/read//') def read(id): title = '' body = '' for topic in topics: if id == topic['id']: title = topic['title'] body = topic['body'] break 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//', methods=['GET', 'POST']) def update(id): if request.method == 'GET': title = '' body = '' for topic in topics: if id == topic['id']: title = topic['title'] body = topic['body'] break content = f'''

    ''' return template(getContents(), content) elif request.method == 'POST': global nextId title = request.form['title'] body = request.form['body'] for topic in topics: if id == topic['id']: topic['title'] = title topic['body'] = body break url = '/read/'+str(id)+'/' return redirect(url) @app.route('/delete//', methods=['POST']) def delete(id): for topic in topics: if id == topic['id']: topics.remove(topic) break return redirect('/') app.run(debug=True)

    post를 사용하고 delete를 추가한다.

    '웹해킹' 카테고리의 다른 글

    웹해킹 3주차  (2) 2024.09.23
    웹해킹 1주차  (1) 2024.09.07
    3주차_웹해킹  (0) 2024.07.22
    웹해킹 2주  (0) 2024.07.14
    웹해킹 1주차  (0) 2024.07.04