python을 이용한 cgi프로그램 개발-html 출력 질문!

noruddk의 이미지

지금 python을 이용해서 cgi프로그램을 만들고 있습니다.

현재 html파일과 python 파일은 따로 있습니다.

html의 form 이용해서 변수를 받아오고

python에서 변수를 받아 계산합니다.

제가 지금 하고싶은것은....

python의 결과값을 html에 변수로 받고 싶습니다..

python 코드에서 html로 출력하면....출력이 되긴합니다.
print """
<script>
date1=result[1]
document.write(date1);
</script>
"""

하지만 저는 html에 변수로 받고 싶습니다.
python 코드에서는 변수설정해주고
print """
<script>
date1=result[1]
</script>
"""

아래와 같이 html 코드 에서 변수를 받고 싶습니다.
<script>
document.write(date1);
</script>

아시는 분은 답변부탁드립니다. ㅜㅜㅜ

LIM Daeho의 이미지

내용을 보니 Javascript 변수로 받고 싶으신 것 같네요.
몇몇 발전된 방법(Ajax 등)이 있긴 하지만, 질문 범위내에서 답변 드리겠습니다.

다음처럼 변수의 값을 직접 넣어주는 방법이 있습니다.
필요하면 배열, 객체 등도 Javascript 문법에 맞게 python 등의 언어에서 문자열로 만들어 줄 수 있습니다.
변수에 담을 문자열에 따옴표가 있을 경우 Javascript 문법에 맞게 이스케이프 시켜주시고요.

date1="2016-05-18";
document.write(date1);

twinwings의 이미지

client.html(client side):

<form action="hello.py" method="post">
  <input type="text" name="firstname">
</form>

hello.py(server side):

#!/usr/bin/env python
 
import cgi
 
form = cgi.FieldStorage() 
data = form.getvalue('firstname')
 
html_buffer   = open("hello.html").read()
 
print html_buffer % data

hello.html(server side):

<html>
<javascript>
// data는 %s 값으로 대입되며
// 이 %s 값은 파이썬 스크립트에 의해 채워짐
data = %s
document.write(date);
</javascript>
 
<head></head>
 
<body></body>
 
</html>

이런식으로 해주면 아마 동작할겁니다.

PS. post 등의 입력은 HTML에서 HTML로 전달해주는게 아니라
HTTP 간의 데이터 통신입니다. HTML에서 바로 값을 읽을 방법은 없습니다.
CGI를 통해 넘겨주거나, 웹서버(HTTP서버)에서 자체적으로 HTML 문서를 만들어
클라이언트에 줄 수 있어야 합니다.(아파치-PHP 처럼)

댓글 달기

Filtered HTML

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

BBCode

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param>
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

Textile

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • You can use Textile markup to format text.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Markdown

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • Quick Tips:
    • Two or more spaces at a line's end = Line break
    • Double returns = Paragraph
    • *Single asterisks* or _single underscores_ = Emphasis
    • **Double** or __double__ = Strong
    • This is [a link](http://the.link.example.com "The optional title text")
    For complete details on the Markdown syntax, see the Markdown documentation and Markdown Extra documentation for tables, footnotes, and more.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Plain text

  • HTML 태그를 사용할 수 없습니다.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 줄과 단락은 자동으로 분리됩니다.
댓글 첨부 파일
이 댓글에 이미지나 파일을 업로드 합니다.
파일 크기는 8 MB보다 작아야 합니다.
허용할 파일 형식: txt pdf doc xls gif jpg jpeg mp3 png rar zip.
CAPTCHA
이것은 자동으로 스팸을 올리는 것을 막기 위해서 제공됩니다.