Code

Opened 6 years ago

Closed 6 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: master
Severity: Keywords: templatetag
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: UI/UX:

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.

Attachments (0)

Change History (2)

comment:1 Changed 6 years ago by luftyluft@…

  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset
  • Summary changed from Naming templatetag file same can lead to undesired behavior to Naming templatetag file same as app name can lead to undesired behavior

comment:2 Changed 6 years ago by guettli

  • Resolution set to duplicate
  • Status changed from new to closed

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

Add Comment

Modify Ticket

Change Properties
<Author field>
Action
as closed
as The resolution will be set. Next status will be 'closed'
The resolution will be deleted. Next status will be 'new'
Author


E-mail address and user name can be saved in the Preferences.

 
Note: See TracTickets for help on using tickets.