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
Registering the tagThe tag() method takes two arguments: The name of the template tag – a string. If this is left out, the name of the compilation function will be used. The compilation function – a Python function (not the name of the function as a string).
The template tags are a way of telling Django that here comes something else than plain HTML. The template tags allows us to to do some programming on the server before sending HTML to the client.
The include tag allows you include a template inside the current template. This is useful when you have a block of content that are the same for many pages.
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:
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With