Opened 16 years ago

Closed 16 years ago

#6549 closed (duplicate)

Naming templatetag file same as app name can lead to undesired behavior

Reported by: luftyluft@… Owned by: nobody
Component: Template system Version: dev
Severity: Keywords: templatetag
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

In a custom template tag, I am attempting to import a model from a 3rd party app. The 3rd party app has a templatetag file named the same as the app (app name=coltrane, tag file=coltrane.py)

If I'm correct in my diagnosis, what's happening is that the
django.templatetags package is altering the import path:

#from django.templatetags.__init__.py
for a in settings.INSTALLED_APPS:
    try:
        __path__.extend(__import__(a + '.templatetags', {}, {},
['']).__path__)
    except ImportError:
        pass

We have:

/coltrane
   /templatetags
      coltrane.py
   models.py
   ...
/myapp
  /templatetags
      my_tags.py

Now when my_tags does an "from coltrane.models import Link", I believe
this is occurring within the scope of django.templatetags and hence
our import path looks like:

(Pdb) import sys
(Pdb) sys.modules['django.templatetags'].__path__
['C:\\Python25\\lib\\site-packages\\django-svn\\django\\templatetags',
'C:\\Python25\\lib\\site-packages\\django-svn\\django\\contrib\\admin\\templatetags', 
'c:\\documents and settings\\brian\\workspace\\tagging\\templatetags', 
'c:\\documents and settings\\brian\\workspace\\lunnova\\apps\\utils\\templatetags', 
'c:\\documents and settings\\brian\\workspace\\coltrane\\templatetags']

The result is that "from coltrane import Link" is winding up in the coltrane
\templatetags directory, seeing the coltrane module and leading to the
undesired behavior of executing there. I have not seen any
documentation that states that naming templatetag files the same as an
app but it clearly has undesired behavior if a templatetag file tries
to import modules from another app that happens to have a templatetag
file named the same as the app. I will report this as a bug. The
workaround is to rename templatetag files so that they don't clash
with the project name, however it seems like django should perhaps
check for this case and warn the developer or else the documentation
should have a clear warning.

Change History (2)

comment:1 by luftyluft@…, 16 years ago

Summary: Naming templatetag file same can lead to undesired behaviorNaming templatetag file same as app name can lead to undesired behavior

comment:2 by Thomas Güttler, 16 years ago

Resolution: duplicate
Status: newclosed

Please try the patch from #6587 and report there, if it worked for you.

Note: See TracTickets for help on using tickets.
Back to Top