Murph
Murph

Reputation: 520

Django Templatetags Import Error

I've been banging my head against this for a while, but can't seem to figure it out. I've got an app with a set of custom template tags:

from django import template
from crowd.models import Payment, Project, ProjectCategory

register = template.Library()


@register.filter
def is_customer(user, project):
   try:
      return Payment.objects.filter(user=user, project=project).count() > 0
   except:
      return False

That throws:

'project_tags' is not a valid tag library: ImportError raised loading crowd.templatetags.project_tags: No module named models

The app tree looks like:

crowd/
   -- __init__.py
   -- models.py
   templatetags/
      -- __init__.py
      -- project_tags.py

Importing from just models and crowd.models both give me the same error.

Traceback: here

Update

I was working on something unrelated when I noticed this was broken, so I reverted to an earlier, known working version of the project. Still the same problem, so I think Daniels answer about the PYTHONPATH is correct, however, how can I repair this?

>>> import sys
>>> sys.path
['/Users/****/Documents/dev/product/src/Product', ...]

The __init__.py's are all there all the way down, and crowd is in Product, so shouldn't it be on the path?

Update 2

I've done some investigating in the shell:

>>> from crowd.models import *
>>> from crowd.managers import *
>>> from crowd.constants import *
>>> from crowd.templatetags import *
>>> from crowd.templatetags import project_tags
Traceback (most recent call last):
  File "<console>", line 1, in <module>
  File "/Users/Murph/Documents/dev/product/src/Product/crowd/templatetags/project_tags.py", line 4, in <module>
    from crowd.forms import SearchForm
  File "/Users/Murph/Documents/dev/product/src/Product/crowd/forms.py", line 5, in <module>
    from crowd.models import Payment, Project, ProjectUpdate, GalleryPhoto
ImportError: No module named models
>>> 

Still don't know why specifically that's failing, especially since the blanket import works.

Update 3

Took me a while to see that the shell command was giving a more useful message than the django one, which led to this:

Turns out it wasn't even related to project_tags directly, it just wasn't a very useful error message. The import in project_tags.py of:

 from crowd.forms import SearchForm

was calling:

from crowd.models import Payment, Project, RewardLevel, ProjectUpdate, GalleryPhoto

in forms.py, which should have been:

from models import Payment, Project, RewardLevel, ProjectUpdate, GalleryPhoto

I'll add this as the answer when I can, apparently can't until 8 hours later.

Upvotes: 0

Views: 3404

Answers (2)

Murph
Murph

Reputation: 520

Took me a while to see that the shell command was giving a more useful message than the django one, which led to this:

Turns out it wasn't even related to project_tags directly, it just wasn't a very useful error message. The import in project_tags.py of:

 from crowd.forms import SearchForm

was calling:

from crowdfunder.models import Payment, Project, RewardLevel, ProjectUpdate, GalleryPhoto

in forms.py, which should have been:

from models import Payment, Project, RewardLevel, ProjectUpdate, GalleryPhoto

Upvotes: 1

Daniel Roseman
Daniel Roseman

Reputation: 599610

Your crowd app itself is probably not on your Pythonpath. Either add it, or import from the project: from myproject.crowd.models import Foo, Bar.

Upvotes: 1

Related Questions