2016-06-23 1 views
0

파이썬 안에있는 HTML 코드에 변수를 삽입하는 데 문제가 있습니다. 코드 % (myVar)에 오류가 있습니다. % (myVar)를 제거하면 % s 웹 사이트가 표시됩니다. % (myVar)를 삽입하면 500 내부 서버 오류가 발생합니다. %(myVar)은 문이 아니기 때문에파이썬 코드 내의 HTML 변수가 표시되지 않습니다

당신은 문자열의 끝보다 같은 줄에 %myvar이 필요
 #!/usr/bin/python 
    import os 

    virtenv = os.environ['OPENSHIFT_PYTHON_DIR'] + '/virtenv/' 
    virtualenv = os.path.join(virtenv, 'bin/activate_this.py') 
    try: 
     execfile(virtualenv, dict(__file__=virtualenv)) 
    except IOError: 
     pass 
    # 
    # IMPORTANT: Put any additional includes below this line. If placed above this 
    # line, it's possible required libraries won't be in your searchable path 
    # 
    import time 
    def application(environ, start_response): 

     ctype = 'text/plain' 
     if environ['PATH_INFO'] == '/health': 
      response_body = "1" 
     elif environ['PATH_INFO'] == '/env': 
      response_body = ['%s: %s' % (key, value) 
         for key, value in sorted(environ.items())] 
      response_body = '\n'.join(response_body) 
     else: 
      ctype = 'text/html' 
      myVar = "Howdy Partner" 
      response_body = '''<!doctype html> 
    <html lang="en"> 

    <body> 

     <h1> My Change - Hello World!</h1> 
     <p1>%s</p1> 

    </body> 
    </html>''' 

     %(myVar) 
     status = '200 OK' 
     response_headers = [('Content-Type', ctype), ('Content-Length', str(len(response_body)))] 
     # 
     start_response(status, response_headers) 
     return [response_body] 
+0

그래서 SyntaxError가 발생합니까? myVar는 닫는'' '''과 같은 행에 필요합니다. –

답변

2

그렇지 않으면 당신은 구문 오류 :

다음은 내 코드입니다.

 response_body = '''<!doctype html> 
<html lang="en"> 

<body> 

    <h1> My Change - Hello World!</h1> 
    <p1>%s</p1> 

</body> 
</html>''' %myVar 
관련 문제