Code

Opened 5 years ago

Closed 5 years ago

#9413 closed (duplicate)

naming a template tag library the same as an app causes import problems

Reported by: akaihola Owned by: nobody
Component: Documentation Version: master
Severity: Keywords:
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: UI/UX:

Description

The Django documentation warns about name clashes between custom tag libraries. However, there's no mention about the following problem:

If you have the same name for an app and a tag library, you can't import the app from any other tag libraries. Python first looks up the name among all tag libraries, finds the module there and tries to import that.

This behavior and the incompleteness of the documentation was confirmed by Eric Holscher and lericson on #django-dev a few minutes ago.

Attachments (0)

Change History (2)

comment:1 Changed 5 years ago by akaihola

  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset

I had this problem with the photologue re-usable app and had to get this working. So here's a dirty work-around.

Say you have the following file structure under a directory which is in PYTHONPATH:

photologue/
  __init__.py
  models.py
  templatetags/
    __init__.py
    photologue.py
myapp/
  __init__.py
  models.py
  templatetags/
    __init__.py
    myapp_tags.py

In myapp_tags.py, this is going to fail:

from photologue.models import Photo

Instead, you can

photologue_models = __import__('photologue.models', {}, {}, [''])
Photo = photologue_models.Photo

comment:2 Changed 5 years ago by mtredinnick

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

This is a symptom of #6587.

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.