#! /usr/bin/python2 # coding=utf-8 ''' from jinja2 import Template if __name__=='__main__': template = Template('Hello {{ name }}!') print template.render(name='John Doe') ''' from jinja2 import Environment, PackageLoader import socket global listen_socket def init_web(): global listen_socket HOST, PORT = '', 8888 listen_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) listen_socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) listen_socket.bind((HOST, PORT)) listen_socket.listen(1) print 'Serving HTTP on port %s ...' % PORT def fun(str): global listen_socket while True: client_connection, client_address = listen_socket.accept() request = client_connection.recv(2048) print "re--", request print "-----------------------------------------------------", str http_response = '''HTTP/1.x 200 OK Content-Type: text/html; charset=utf-8 '''+str ''' <head> <title>WOWzh</title> </head> <html> <p>Wow, Pythond 说的Sserver</p> </html> ''' client_connection.sendall(http_response) client_connection.close() if __name__=='__main__': env = Environment(loader=PackageLoader('main', 'templates')) template = env.get_template('c1.html') http_response = '''HTTP/1.x 200 OK Content-Type: text/html; charset=utf-8 <head> <title>WOWzh</title> </head> <html> <p>Wow, Pythond 说的Sserver</p> </html> ''' st=u"1士大夫" output = template.render(Index=st).encode("utf-8") print output init_web() #output2 = env.get_template('base.html').render().encode('utf-8') fun(output)--------------------------------c1.html-----------------------------{% extends "base.html" %} {% block title %}Index{% endblock %} {% block head %} {{ super() }} <style type="text/css"> .important { color: #336699; } </style> {% endblock %} {% block content %} <h1>{{Index}} 的说的</h1> <p class="important"> Welcome to my awesome homepage. </p> {% endblock %}----------------------base.html-----------------<!DOCTYPE html> <html lang="utf-8"> <head> {% block head %} <link rel="stylesheet" href="style.css" /> <title>{% block title %}{% endblock %} - My Webpage</title> {% endblock %} </head> <body> <div id="content">{% block content %}{% endblock %}</div> <div id="footer"> {% block footer %} © Copyright 2008 by <a href="http://domain.invalid/">you</a>. {% endblock %} </div> </body> </html>--------------------------------浏览器打开-----------http://127.0.0.1:8888/
python jinja 简单调试
最新推荐文章于 2024-07-18 19:54:51 发布