수업소개
글 삭제 기능을 구현하는 방법을 알아보겠습니다.
강의
소스코드
index.py
#!/usr/local/bin/python3 print("Content-Type: text/html") print() import cgi, os files = os.listdir('data') listStr = '' for item in files: listStr = listStr + '<li><a href="index.py?id={name}">{name}</a></li>'.format(name=item) form = cgi.FieldStorage() if 'id' in form: pageId = form["id"].value description = open('data/'+pageId, 'r').read() update_link = '<a href="update.py?id={}">update</a>'.format(pageId) delete_action = ''' <form action="process_delete.py" method="post"> <input type="hidden" name="pageId" value="{}"> <input type="submit" value="delete"> </form> '''.format(pageId) else: pageId = 'Welcome' description = 'Hello, web' update_link = '' delete_action = '' print('''<!doctype html> <html> <head> <title>WEB1 - Welcome</title> <meta charset="utf-8"> </head> <body> <h1><a href="index.py">WEB</a></h1> <ol> {listStr} </ol> <a href="create.py">create</a> {update_link} {delete_action} <h2>{title}</h2> <p>{desc}</p> </body> </html> '''.format(title=pageId, desc=description, listStr=listStr, update_link=update_link, delete_action=delete_action))
process_delete.py
#!/usr/local/bin/python3 import cgi, os form = cgi.FieldStorage() pageId = form["pageId"].value os.remove('data/'+pageId) #Redirection print("Location: index.py") print()