1

I'm a newbie to app engine and python and am just trying to get a basic idea for how things work.

I have a simple app, with one mapped url (/). All the classes I'm trying to use are in the base directory of the app.

This is my main.py - all i want to do is use the middleware class to pass a variable to the template so I can render different parts of the page depending on device type.

import webapp2
import jinja2
import os
from useragents import search_strings

jinja_environment = jinja2.Environment(
    loader=jinja2.FileSystemLoader(os.path.dirname(__file__)))

class MainPage(webapp2.RequestHandler):
    def get(self):

    template = jinja_environment.get_template('templates/index.html')
    self.response.out.write(template.render())

app = webapp2.WSGIApplication([('/', MainPage)],
                          debug=True)


class Middleware(object):
@staticmethod
def process_request(request):
    """Adds a "mobile" attribute to the request which is True or False
       depending on whether the request should be considered to come from a
       small-screen device such as a phone or a PDA


    //rest of class is [here][1]
    """
rix
  • 10,104
  • 14
  • 65
  • 92

1 Answers1

1
import webapp2
import jinja2
import os
from useragents import search_strings

jinja_environment = jinja2.Environment(
    loader=jinja2.FileSystemLoader(os.path.dirname(__file__)))

class MainPage(webapp2.RequestHandler):
    def get(self):
    #i don't know if you want to overwrite self.request but here it is
    self.request = Middleware.process_request(self.request)
    template = jinja_environment.get_template('templates/index.html')
    self.response.out.write(template.render())

app = webapp2.WSGIApplication([('/', MainPage)],
                          debug=True)


class Middleware(object):
@staticmethod
def process_request(request):
    """Adds a "mobile" attribute to the request which is True or False
       depending on whether the request should be considered to come from a
       small-screen device such as a phone or a PDA


    //rest of class is [here][1]
    """
Jimmy Kane
  • 16,223
  • 11
  • 86
  • 117
  • Thanks, but I'm getting this error: File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/lib/webob_1_1_1/webob/request.py", line 1238, in __getattr__ raise AttributeError(attr) AttributeError: META – rix Jan 15 '13 at 14:04
  • I just added an example on how to use this class though it's static method. It would help if you inform me what class is this so I can replicate your situation. I am new to middle-ware classes as well. – Jimmy Kane Jan 15 '13 at 14:22
  • Right, I get it I think - thanks a lot. I need to rewrite the middleware class I'm using for app engine - http://code.google.com/p/minidetector/source/browse/trunk/minidetector/__init__.py - is there a good webapp2 equivalent for 'request.META.has_key'? – rix Jan 15 '13 at 14:47
  • @rix replace with `in` This should do it. has_key is removed as well in python3x http://stackoverflow.com/questions/1323410/has-key-or-in – Jimmy Kane Jan 15 '13 at 14:51