Tuesday, November 10, 2009

Django flatpages, templatetags and dynamic menus

One thing that Django's flatpages app is missing is a dynamic menu.

Coming from a Java background I am used to a huge user base and lots of open source code. The Django user base continues to meet my expectations.

I found this post detailing the creation of such a menu tag. I hadn't explored custom tags before anyway so I looked forward to trying it out.

I followed the instructions and got the following error :

TemplateSyntaxError at /menu/
'flatpage_menu' is not a valid tag library: Could not load template library from django.templatetags.flatpage_menu, No module named flatpage_menu
Request Method: GET
Request URL: http://127.0.0.1:8000/menu/
Exception Type: TemplateSyntaxError
Exception Value:
'flatpage_menu' is not a valid tag library: Could not load template library from django.templatetags.flatpage_menu, No module named flatpage_menu
Exception Location: /Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/site-packages/django/template/defaulttags.py in load, line 927
Python Executable: /Library/Frameworks/Python.framework/Versions/2.6/Resources/Python.app/Contents/MacOS/Python
Python Version: 2.6.4
Python Path: ['/Users/jdavis/Documents/django_workspace/cantina76', '/Library/Frameworks/Python.framework/Versions/2.6/lib/python26.zip', '/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6', '/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/plat-darwin', '/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/plat-mac', '/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/plat-mac/lib-scriptpackages', '/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/lib-tk', '/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/lib-old', '/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/lib-dynload', '/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/site-packages']

The post was from last year. I was using the Django 1.1.1. I figured something had changed since his post.

I found a couple of references in Google Groups to this same example, but their suggestions didn't work for me.

The solution was to add my own app to INSTALLED_APPS and "from django import template" to the "flatpage_menu.py" file.

It worked fine after that.

No comments: