google-app-engine


Google App Engine WebApp2 template not working


I am learning Google App Engine, and have started with WebApp2 framework + Jinja2 template.
I have written the following code:
Main.py
import webapp2
import os
import jinja2
from google.appengine.ext.webapp import template
from google.appengine.api import users
from google.appengine.ext import ndb
template_dir = os.path.join(os.path.dirname(__file__), 'templates')
jinja_env = jinja2.Environment(loader = jinja2.FileSystemLoader(template_dir))
class Handler(webapp2.RequestHandler):
def write(self, *a, **kw):
self.response.out.write(*a, **kw)
def render_str(self, template, **params):
t = jinja_env.get_template(template)
return t.render(params)
def render(self, template, **kw):
self, write(self.render_str(template, **kw))
class MainHandler(webapp2.RequestHandler):
def get(self):
self.response.write('Hello world!')
class PageOne(Handler):
def get(self):
self.render('pageone.html')
app = webapp2.WSGIApplication([('/', MainHandler),('/pageone', PageOne)], debug=True)
app.yaml
application: tbapp
version: 1
runtime: python27
api_version: 1
threadsafe: yes
handlers:
- url: /favicon\.ico
static_files: favicon.ico
upload: favicon\.ico
- url: .*
script: main.app
libraries:
- name: webapp2
version: "2.5.2"
- name: jinja2
version: latest
There is also a pageone.html HTML file in templates folder.
Now, when I run the files, the main page is showing Hello world!. But, when I run http://localhost:8080/pageone..... it returns the following error..
File "E:\gae_apps\tbapp\tbapp\main.py", line 50, in get
self.render('pageone.html')
File "E:\gae_apps\tbapp\tbapp\main.py", line 40, in render
self, write(self.render_str(template, **kw))
NameError: global name 'write' is not defined
Can anyone please guide me, what am In doing wrong. I am not able to figure out. TIA
As Amber pointed out, you have a typo in your Handler's render method.
Replace
def render(self, template, **kw):
self, write(self.render_str(template, **kw))
With
def render(self, template, **kw):
self.write(self.render_str(template, **kw))
The error message does give you a hint
NameError: global name 'write' is not defined

Related Links

Moving HTML files to their own directory in Google App Engine (Using Jinja2 Templates) - Error 13
AppEngine local unit testing guide for Python 2.7 Runtime
No persistence providers available for “transactions-optional”
List Children of null parent?
provisioning_oauth_example in gdata-python: 2-legged and 3-legged errors
GWT Compile everytime I deploy to Appengine
How to get free domain names for GWT APP?
Reduce the size of Google app engine Datastore Stored Data
Google App Engine Datastore / NoSQL example with ancestor queries
Form with file only uploads filename
How to save a stream on google drive?
GAE and GWT - response.sendRedirect is not redirect
XMPP sendMessage does not send the message
how to make a connection to datastore inside jstl tags
Accessing self.request.get() from a Function in Python Webapp2 App Engine
Javascript file (nocache.js) not uploaded to appengine for gwt project

Categories

HOME
porting
rdbms
youtube-data-api-v3
static
mstest
pclxl
bookmarks
azure-container-service
gluon-mobile
nstableheaderview
title
recordrtc
octave
heap
jxl
selenium-ide
vuejs
connector
delphi-xe5
topology
asp.net-core-webapi
drive
asciidoctor-pdf
squashfs
imagemap
href
systemtime
easeljs
highstock
xunit
mount
ckfinder
dapper-extensions
asymptotic-complexity
entity-relationship-model
carrierwave
testcomplete
aws-codecommit
kodi
laravel-eloquent
mailkit
sammy.js
twos-complement
fossil
pop3
sharding
parse-tree
sim-card
css-position
portforwarding
postgresql-8.4
subclipse
lpsolve
placeholder
dapper-simplecrud
x++
opennms
mschart
compiler-design
boo
sundials
objectscript
django-1.10
bootstrap-tags-input
rsa-archer-grc
donations
cvxpy
vcloud-director-rest-api
slackware
flex-monkey
seek
xdoclet
appscale
embedding
google-query-language
lua-telegram-bot
turbo-prolog
html-to-pdf
exim4
contentcontrol
asyncdisplaykit
qt5.2
flock
rate-limiting
dronekit-android
google-refine
kubuntu
dbaccess
big-ip
trdion2011
lambda-architecture
jjaql
maven-scm
cfcache
gae-quotas
xmltodict
rfc5322
node-blade
android-sdk-2.3
android-loadermanager
extreme-programming
pstack
oracle-enterprise-linux
azman
landscape-portrait
creole
timthumb
http-daemon
msr
socketserver
silent
vs-android
web-garden
opml
qtabbar
django-piston
fault-tolerance
flvplayback
image-capture

Resources

Encrypt Message