WEB2 - Python

활용 - 생성 구현 1 - form

수업소개

사용자의 정보를 서버로 전송하는 장치인 form의 사용법을 알아봅니다.

 

 

강의

 

 

소스코드

변경사항 

create.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()
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=listStr))

 

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()
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>
  <h2>{title}</h2>
  <p>{desc}</p>
</body>
</html>
'''.format(title=pageId, desc=description, listStr=listStr))

 

댓글

댓글 본문
  1. scientist
    220817
  2. 윤재승
    재밌네요
  3. labis98
    2022.02.25 좋은 강의 한번 더 들어요~~~!
  4. pmxsg
    2022.02.06
  5. labis98
    20220113 좋은 강의 합니다.
  6. 2021.09.11 와우 신기하지만 어렵다
  7. 이택진
    210520
  8. 초딩 개발자
    2021/05/10
  9. jeisyoon
    2021.04.09 생성 구현 ; from - OK
  10. VIBOT
    ok
  11. 박민지
    2021.01.15
  12. seongsu
    2020.10.15
  13. dagel0102
    20.10.13
  14. cgoing
    cgoing / 2020.10.04
  15. jiyeong
    2020.08.14.(웹연동이 안되서 몇일째 헤매다 알파벳 하나 지우는 것으로 모든게 해결되는 마법)
  16. 악어
    2020-03-14 완료
  17. 스티븐잡숴
    완료
  18. new_3
    완료 - 사용자가 입력한 것을 서버로 전달하기 위해 form 태그를 사용할 수 있습니다. 여기서는 새로운 페이지를 생성하기 위해, html페이지에 form(그리고 그 내부에 '제목'과 '내용')을 만들었습니다. 그런데 form tag의 attribute중 method란 것이 있는데, 기본값으로 설정된 'get'방식을 선택하면 사용자가 입력한 내용이 url을 통해 그대로 드러나게 됩니다. 이 경우 링크 클릭만으로도 글이 써지거나 삭제가 되거나 하는 문제가 발생합니다. 따라서 method를 'post'로 선택해야 하는데 이렇게 하면 데이터는 url이 아닌 다른 방식으로 전달되어 위와 같은 문제가 발생하지 않게 됩니다.
    (복습을 위한 내용 정리입니다. 틀린 내용이 있을수 있으며 지적해주시면 감사하겠습니다 ^)^)
  19. xuanxi
    20191129
  20. 쑤우
    수강완료. 감사합니다.
  21. 굼벵이
    완료
  22. 20191004 Ted 완료!
  23. JuicyFresh
    감사합니다.
  24. 연준희
    컴퓨터 환경마다 정렬하는 순서가 틀려서 다르게 보일 수 있습니다.
    파이썬에 정렬 순서를 바꾸는 법도 있을거에요.
    대화보기
    • goyacommae
      아아 감수성이 점점 좋아집니다...~.~
    • illliilllliillliii
      감사합니다 ~
    • 굿잡
      리눅스상에서는 알파벳순으로 정렬이 되고 파이썬으로 긁어올때는 역순으로 긁어와지지 않나요?
      저는 그렇게 되는데...
      대화보기
      • 마샤
        data 폴더에 어떻게 python이 최상위로 올 수 있죠?
        P는 알파벳 뒤쪽이라 항상 JavaScript 뒤로 오던데(제 거에서는요). 어떤 비밀이 숨어있는지 궁금해요
      버전 관리
      egoing
      현재 버전
      선택 버전
      graphittie 자세히 보기