kaki1013
7. 삭제 기능 구현 본문
# 예시 코드
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'''
<li><a href="/update/{id}/">update</a></li>
<li><form action="/delete/{id}/" method="POST"><input type="submit" value="delete"></form></li>
'''
return f'''<!doctype html>
<html>
<body>
<h1><a href="/">WEB</a></h1>
<ol>
{contents}
</ol>
{content}
<ul>
<li><a href="/create/">create</a></li>
{contextUI}
</ul>
</body>
</html>
'''
def getContents():
liTags = ''
for topic in topics:
liTags = liTags + f'<li><a href="/read/{topic["id"]}/">{topic["title"]}</a></li>'
return liTags
@app.route('/')
def index():
return template(getContents(), '<h2>Welcome</h2>Hello, WEB')
@app.route('/read/<int:id>/')
def read(id):
title = ''
body = ''
for topic in topics:
if id == topic['id']:
title = topic['title']
body = topic['body']
break
return template(getContents(), f'<h2>{title}</h2>{body}', id)
@app.route('/create/', methods=['GET', 'POST'])
def create():
if request.method == 'GET':
content = '''
<form action="/create/" method="POST">
<p><input type="text" name="title" placeholder="title"></p>
<p><textarea name="body" placeholder="body"></textarea></p>
<p><input type="submit" value="create"></p>
</form>
'''
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/<int:id>/', 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'''
<form action="/update/{id}/" method="POST">
<p><input type="text" name="title" placeholder="title" value="{title}"></p>
<p><textarea name="body" placeholder="body">{body}</textarea></p>
<p><input type="submit" value="update"></p>
</form>
'''
return template(getContents(), content)
elif request.method == 'POST':
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/<int:id>/', methods=['POST'])
def delete(id):
for topic in topics:
if id == topic['id']:
topics.remove(topic)
break
return redirect('/')
app.run(debug=True)
# 요약
1. template 함수 수정
: delete 버튼 추가
: url 형태는 X (크롬 플러그인 등 링크에 방문하는 프로그램이 방문할 시 삭제 가능)
2. delete 함수 작성
: 위의 이유로 GET 이 아닌 POST 방식으로만 접근 가능
'버그바운티 스터디 > FLASK' 카테고리의 다른 글
6. 수정 기능 구현 (0) | 2023.07.26 |
---|---|
5. 쓰기 기능 구현 (2) (0) | 2023.07.22 |
4. 쓰기 기능 구현 (1) (0) | 2023.07.21 |
3. 읽기 기능 구현 (0) | 2023.07.21 |
2. 홈페이지 구현 (0) | 2023.07.21 |