커뮤니티

고용노동부, 산업인력공단과 함께하는 강원도 유일한 기업중심 IT전문교육기관 ICT융합캠퍼스만의 특별한교육입니다.
공인 IT숙련기술인의 다양한 접근방법으로 전문가다운 실무교육을 받을 수 있습니다.

Category

교육강좌

WEB WEB2 - Python - 활용 - 수정 구현

페이지 정보

작성자 관리자 댓글 0건 조회 3,653회 작성일 20-06-03 17:31

본문

활용 - 수정 구현

수업소개

여기서는 Python 웹애플리케이션에서 어떻게 수정 기능을 구현하는가를 살펴봅니다. 


 

강의

 

 

소스코드

변경사항 

update.py

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#!/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()
else:
pageId = 'Welcome'
description = 'Hello, web'
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>
<form action="process_update.py" method="post">
<input type="hidden" name="pageId" value="{form_default_title}">
<p><input type="text" name="title" placeholder="title" value="{form_default_title}"></p>
<p><textarea rows="4" name="description" placeholder="description">{form_default_description}</textarea></p>
<p><input type="submit"></p>
</form>
</body>
</html>
'''.format(title=pageId, desc=description, listStr=listStr, form_default_title=pageId, form_default_description=description))

 

process_update.py

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#!/usr/local/bin/python3
import cgi, os
form = cgi.FieldStorage()
pageId = form["pageId"].value
title = form["title"].value
description = form['description'].value
opened_file = open('data/'+pageId, 'w')
opened_file.write(description)
opened_file.close()
os.rename('data/'+pageId, 'data/'+title)
#Redirection
print("Location: index.py?id="+title)
print()

 

index.py

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#!/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)
else:
pageId = 'Welcome'
description = 'Hello, web'
update_link = ''
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}
<h2>{title}</h2>
<p>{desc}</p>
</body>
</html>
'''.format(title=pageId, desc=description, listStr=listStr, update_link=update_link))

 

  • 트위터로 보내기
  • 페이스북으로 보내기
  • 구글플러스로 보내기

답변목록

등록된 답변이 없습니다.