Custom django template tag issue on AppEngine
So I have beating my head against the wall on this. I feel like开发者_高级运维 I have interpretted the docs and examples I found, but this just won't seem to go away.
Here is the tag code:
from google.appengine.ext import webapp
register = webapp.template.create_template_register()
def test_tag():
return "TEST!"
register.simple_tag(test_tag)
Here is the main code:
from google.appengine.ext import webapp
from google.appengine.ext.webapp import util, template
webapp.template.register_template_library('my_tags')
class MainHandler(webapp.RequestHandler):
def get(self):
self.response.out.write(template.render("test.html", {}))
def main():
application = webapp.WSGIApplication([('/', MainHandler)], debug=True)
util.run_wsgi_app(application)
if __name__ == '__main__':
main()
Here is the template:
{% load my_tags %}
<html>{% test_tag %}></html>
Here is the error:
File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/lib/django/django/template/defaulttags.py", line 750, in load
raise TemplateSyntaxError, "'%s' is not a valid tag library: %s" % (taglib, e)
TemplateSyntaxError: 'my_tags' is not a valid tag library: Could not load template library from django.templatetags.my_tags, No module named my_tags
I really hate to flat out ask someone to fix my code, but I cannot seem to figure out why this won't work. Any hints or pointers would be greatly apprecated.
jc
So it turns out that when you use the method I used to register the custom tag, you don't need to use the load statement at the top of my example template.
For a well written explanation of this issue see http://www.hipatic.com/2012/11/appengine-python-27-django-custom.html
The heart of the issue here is that there are two Djangos in Google App Engine:
- AppEngines's Internal Django ("Webapp Django" if you like)
- Library Django ("regular" Django)
The article provides 2 examples that clarify the usage of each one.
It also goes on about how some of the available documentation leads to mashing the two approaches, which seems to be what is presented in the question, where {% load my_tags %}
(needed for Library Django) was used with WebApp Django, thereby producing the TemplateSyntaxError: 'my_tags' is not a valid tag library
error.
精彩评论