Django template context not working with imported class
Posted
by
Andy Hume
on Stack Overflow
See other posts from Stack Overflow
or by Andy Hume
Published on 2012-03-24T17:18:27Z
Indexed on
2012/03/24
17:29 UTC
Read the original article
Hit count: 191
I'm using Django's templating on appengine, and am having a problem whereby a class I'm importing from another package is not correctly being made available to the template context.
Broadly speaking, this is the code. The prop1 is not available in the template in the first example below, but is in the second. MyClass is identical in both cases.
This does not work:
from module import MyClass
context = MyClass()
self.response.out.write(template.render(path, context))
This does:
class MyClass(object):
def __init__(self):
self.prop1 = "prop1"
context = MyClass()
self.response.out.write(template.render(path, context))
If I log the context in the above code I get:
<module.MyClass object at 0x107b1e450>
when it's imported, and:
<__main__.MyClass object at 0x103759390>
when it's defined in the same file.
Any clues as to what might cause this kind of behaviour?
© Stack Overflow or respective owner