0

I have a Google App Engine Python script trying to pass the variable "time" as a strftime() call. I have jinja2 set up to read the html file with {{time}} in it as that variables destination

class MainPage(BlogHandler):

    time = ''

    def get_time(you):
        return strftime('%U %A',gmtime())

    def get(self):
        time = self.get_time
        self.render('front.html',time = time)

When I render/write out the whole thing into a simple div tag I get an object memory locator rendered in html

<bound method MainPage.get_time of <main.MainPage object at 0x1030f0610>>

obviously its not processing this out as a string. Am I using the wrong time function, is this a GAE issue? is this a Jinja2 issue? Is this a python issue? I'm clearly not sure how to follow up and resolve this. Thanks or any good critical advice.

All I want is to render a formattable time function to a string so i can use it in GAE scripts.

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
underarock
  • 157
  • 6

1 Answers1

2

All you have to do is call the get_time() method:

time = self.get_time()

By not calling the method, all you do is store a reference to the method, and Jinja2 then takes the str() result of that method to include it in your template output:

>>> from time import strftime, gmtime
>>> class MainPage():
...     def get_time(self):
...         return strftime('%U %A',gmtime())
... 
>>> mp = MainPage()
>>> mp.get_time
<bound method MainPage.get_time of <__main__.MainPage instance at 0x1031c7320>>
>>> mp.get_time()
'07 Saturday'
Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
  • 1
    @underarock: Sure, look up the arguments to `strftime()` in the [documentation](http://docs.python.org/2/library/time.html#time.strftime). Note that `rd` would have to be generated by hand though. – Martijn Pieters Feb 23 '13 at 17:07
  • oh you mean add the (). this is extremely obvious though i tried this yesterday it was crashing the GAE server. not today... thanks! – underarock Feb 23 '13 at 17:09
  • @underarock: See this [older answer of mine](http://stackoverflow.com/questions/12588736/convert-date-to-natural-language-in-python/12589027#12589027) for generating ordinals for dates. – Martijn Pieters Feb 23 '13 at 17:11