Python Jinja2

index.cgi


#!/usr/local/bin/python2.7
# -*- coding: utf-8 -*-

import cgi
from   jinja2 import Environment, FileSystemLoader

#----------------------------------------------------------
# CONFIG
#----------------------------------------------------------
_ENCODE             = 'utf-8'
_HTML_TEMPLATE_FILE = 'index.tmpl'

#----------------------------------------------------------
# Jinja2 Wrapper
#----------------------------------------------------------
class HTTPtemplate(object):

    def __init__(self, template_file):

        self.endocing      = 'utf-8'
        self.data          = {}
        self.template_dir  = './'
        self.template_file = template_file

    def set_encode(self,encode):

        self.encode = encode

    def set_template_dir(self, template_dir):

        self.template_dir = template_dir

    def set_data(self, key, value):

        try :
            value = unicode(value, self.encode)

        except :
            value = value

        self.data[key] = value

    def http_output(self):

        env  = Environment(loader=FileSystemLoader(self.template_dir, encoding=self.encode))
        tmpl = env.get_template(self.template_file)
        print tmpl.render(data=self.data).encode(self.encode)


#----------------------------------------------------------
# MAIN
#----------------------------------------------------------
def main():

    #----------------------------------------------------------
    # POST|Holen Sie sich GET-Daten
    #----------------------------------------------------------
    http_requests = cgi.FieldStorage()

    #----------------------------------------------------------
    #HTML-Generierung
    #----------------------------------------------------------
    http = HTTPtemplate(_HTML_TEMPLATE_FILE)
    http.set_encode(_ENCODE)
    http.set_data('charset', _ENCODE)
    http.set_data('request_get' , http_requests.getvalue('get'  , None))
    http.set_data('request_post', http_requests.getvalue('post' , None))
    http.http_output()

if __name__ == '__main__' : main()

index.tmpl


Content-Type: text/html

<!DOCTYPE html>
<head>
<meta charset="{{data.charset}}">
<title>INDEX</title>
</head>
  <body>
    <form method="post" action="./?get=Ah">
    <textarea name="post" cols="50" rows="1"></textarea>
    <input type="submit" value="submit">
    <p>GET :{{data.request_get}}</p>
    <p>POST:{{data.request_post}}</p>
    </form>
  </body>
</html>

Recommended Posts

Python Jinja2
Python
Jinja2 | Python-Vorlagen-Engine
Kafka Python
Python-Grundlagen ⑤
Python-Zusammenfassung
Eingebaute Python
jinja2 Memorandum
Python-Einschlussnotation
Python studieren
Python 2.7 Countdown
Python-Memorandum
Python FlowFishMaster
Python-Dienst
Python-Tipps
Python-Memo
Python-Einschlussnotation
Python Singleton
Python-Grundlagen ④
Python-Memorandum 2
Python-Inkrement
atCoder 173 Python
[Python] -Funktion
Python-Installation
Python installieren 3.4.3.
Versuchen Sie Python
Python-Memo
Python iterativ
Python-Algorithmus
Python2 + word2vec
[Python] -Variablen
Python-Funktionen
Python sys.intern ()
Python-Tutorial
Python-Fraktion
Python Underbar Das ist was
Python-Zusammenfassung
Starten Sie Python
[Python] Sortieren
Hinweis: Python
Python-Grundlagen ③
Python-Protokoll ausgeben
Python-Grundlagen
[Scraping] Python-Scraping
Python-Update (2.6-> 2.7)
Python-Memo
Python-Memorandum
Python #sort
Ufo-> Python
Python nslookup
Python lernen
[Rpmbuild] Python 3.7.3.
Prorate Python (1)
Python Memorandum
Laden Sie Python herunter
Python Memorandum
Python-Memo
Python gestartet