How to load .html page with python on app-engine -
How to load .html page with python on app-engine -
in next example, .html
info in same file python code (as variable main_page_html
). if want .html
content in own different file, must utilize jinja2
load it? or there simpler approach?
import cgi google.appengine.api import users import webapp2 main_page_html = """\ <html> <body> <form action="/sign" method="post"> <div><textarea name="content" rows="3" cols="60"></textarea></div> <div><input type="submit" value="sign guestbook"></div> </form> </body> </html> """ class mainpage(webapp2.requesthandler): def get(self): self.response.write(main_page_html) class guestbook(webapp2.requesthandler): def post(self): self.response.write('<html><body>you wrote:<pre>') self.response.write(cgi.escape(self.request.get('content'))) self.response.write('</pre></body></html>') application = webapp2.wsgiapplication([ ('/', mainpage), ('/sign', guestbook), ], debug=true)
again, want have .html
file. .html
file contains form user can fill , send me. question how nowadays html page? other defining variable such main_page_html
, required utilize jinja2
or there simpler lay content of .html
, pass self.response.write
? give thanks much helping.
jinja2 it's template engine, merge variables before served in client, webapp2 include template engine itself
import webapp2 import os #added google.appengine.ext.webapp import template #also added class mainpage(webapp2.requesthandler): def get(self): path = os.path.join(os.path.dirname(__file__), 'templates/index.html') self.response.out.write(template.render(path, {})) class guestbook(webapp2.requesthandler): def post(self): #didn't alter self.response.write('<html><body>you wrote:<pre>') self.response.write(cgi.escape(self.request.get('content'))) self.response.write('</pre></body></html>') application = webapp2.wsgiapplication([ ('/', mainpage), ('/sign', guestbook), ], debug=true)
so can utilize webapp2, jinja or others templates engines, out of box app engine offers webapp2 (django) , jinja2
to serve static files (images, js, css, etc.) , in handlers section in app.yaml file
handlers: - url: /images # in html can access localhost:8080/images static_dir: templates/images # folder template, subfolder images - url: /js static_dir: templates/js - url: /css static_dir: templates/css - url: /fonts static_dir: templates/fonts - url: /assets static_dir: templates/assets
according yaml file, construction in project
- myappfolder -- templates ---- images ---- js ---- css ---- fonts ---- assets
python html google-app-engine
Comments
Post a Comment