daGrevis daGrevis - 1 year ago 77
Python Question

Importing Markdown2 in template tag

I needed Markdown syntax for my blog. I decided to use python-markdown2 library. I follow TDD process so I created tests in which I imported Markdown library and it worked. Then all I needed was to convert some of the output so that all tests pass. I think that it's more like a presentation logic, so I created template tag in which I want to import library and... that as far as I got. Problem is that I can't import that library! I debugged for hours and I still can't find a reason why.

Few facts:

  1. It's imported in tests.py before (so it does exist),

  2. Without using external library, template tag works fine;

The precise error message:

TemplateSyntaxError at /article/1/foo-bar/
'cheese' is not a valid tag library: ImportError raised loading blog.templatetags.cheese: cannot import name markdown

Here's templatetags/cheese.py:

from django import template
import markdown2

register = template.Library()

def eggs(value):
return value

Here's template:

{% load cheese %}

P.S. If needed, full error and all code.

Answer Source

The traceback shows something that isn't present in your github repo: there is a file in the templatetags directory called markdown2.py, which is imported when the cheese file does import markdown2. Remove this file, and it should find the right one.