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|Get GET data
    #----------------------------------------------------------
    http_requests = cgi.FieldStorage()

    #----------------------------------------------------------
    #HTML generation
    #----------------------------------------------------------
    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 template engine
kafka python
Python basics ⑤
python + lottery 6
Python Summary
Built-in python
jinja2 memorandum
Python comprehension
Studying python
Python 2.7 Countdown
Python memorandum
Python FlowFishMaster
Python service
python tips
Python memo
Python comprehension
Python Singleton
Python basics ④
Python Memorandum 2
Python increment
atCoder 173 Python
[Python] function
Python installation
python tips
Installing Python 3.4.3.
Try python
Python memo
Python iterative
Python algorithm
Python2 + word2vec
[Python] Variables
Python functions
Python sys.intern ()
Python tutorial
Python decimals
python underscore
Python summary
Start python
[Python] Sort
Note: Python
Python basics ③
python log
Python basics
[Scraping] Python scraping
Python update (2.6-> 2.7)
python memo
Python memorandum
Python # sort
ufo-> python
Python nslookup
python learning
Hannari Python 2020
[Rpmbuild] Python 3.7.3.
Prorate Python (1)
python memorandum
Download python
python memorandum
Python memo
started python