생활코딩

Coding Everybody

코스 전체목록

닫기

활용 - 함수를 글목록 기능을 정리 정돈하기

수업소개

Python의 함수를 이용해서 글목록의 코드를 정리 정돈해서 재사용성을 높이는 방법을 살펴봅니다. 

 

 

강의

 

 

소스코드

변경사항

create.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
39
#!/usr/local/bin/python3
print("Content-Type: text/html")
print()
import cgi, os
def getList():
files = os.listdir('data')
listStr = ''
for item in files:
listStr = listStr + '<li><a href="index.py?id={name}">{name}</a></li>'.format(name=item)
return listStr
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_create.py" method="post">
<p><input type="text" name="title" placeholder="title"></p>
<p><textarea rows="4" name="description" placeholder="description"></textarea></p>
<p><input type="submit"></p>
</form>
</body>
</html>
'''.format(title=pageId, desc=description, listStr=getList()))

 

댓글

댓글 본문
  1. scientist
    220819
  2. labis98
    2022.02.27 좋은 강의 한번 더 들어요~~~!
  3. pmxsg
    2022.02.07
  4. labis98
    20220119 좋은 강의 감사합니다.
  5. 2021.09.11 완료
  6. 이택진
    210521
  7. 초딩 개발자
    2021/05/10
  8. jeisyoon
    2021.04.09 글목록 기능을 함수로 만들기 - OK
  9. 박민지
    2021.01.16
  10. cgoing
    cgoing / 2010.10.04
  11. 로빈
    너무 잘 공부하고 있습니다
  12. 안병혁
    고맙습니다.
  13. 악어
    2020-03-28 완료
  14. 스티븐잡숴
    완료
  15. oxugi
    감사합니다. 열심히 끝까지 가볼게요~
  16. 쑤우
    수강완료. 감사합니다~
  17. 굼벵이
    완료
  18. 20191007 Ted 완료!
  19. JuicyFresh
    감사합니다.
  20. 폭스킴
    글제목에서 "함수를 -> 함수로" 변경하시는게 어색하지 않을 거 같습니다~ ^^
공동공부
graphittie 자세히 보기