Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 290
Joel Clark Avatar asked Oct 30 '10 16:10

Joel Clark


People also ask

How do I register a template tag?

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).

What is with tag in Django template?

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.

What is {% include %} in Django?

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.


2 Answers

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.

like image 175
Joel Clark Avatar answered Oct 01 '22 01:10

Joel Clark


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:

  1. AppEngines's Internal Django ("Webapp Django" if you like)
  2. 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.

like image 42
jmsgomes Avatar answered Oct 01 '22 01:10

jmsgomes