On Thu, Oct 1, 2009 at 6:16 PM, shacker <shac...@birdhouse.org> wrote:
> > Google apparently won't let me reply to an older thread: > > http://groups.google.com/group/django-users/browse_thread/thread/94239f017df4b12f > so starting a new one. > > I too was hitting the "No module named models" problem when creating a > template tag. I had the tag working perfectly when it communicated > exclusively with the models belong to the app the templatetag lived > in. But I needed to modify the tag to also get data from models in > another of the project's apps. That other app imports fine from > wherever I use it, but I could not get its models to be imported into > the template tag. No amount of path tweaking, debugging, deleting .pyc > files etc. would help. > > The docs say: "Custom template tags and filters must live inside a > Django app. If they relate to an existing app it makes sense to bundle > them there; otherwise, you should create a new app to hold them." > > They do not say whether a template tag should be able to talk to > models from two different apps, but I assume they would be able to. My > final solution was to merge the two apps into one. Not ideal, but not > horrible in this particular circumstance. I did not try creating a new > app just to hold the templatetag. > > Anyway, the question is, shouldn't template tags that live inside one > app's directory also be able to get data from other apps? > > Yes, you must simply ensure you name things in such a way that the models module you want to access is not hidden by something else. The original problem noted in the thread you reference resulted from re-using the application's name (bookmarks) as the name of the template tags file (bookmarks.py), which then attempted to import from the applications models (bookmarks.models). Renaming the bookmarks.py template tags file to bookmark_tags, and deleting all old .pyc files resulting from the previous name, fixed the problem. (Why it took "many refreshes" to discover an errant .pyc file that continued to cause trouble is unclear to me, I'm unfamiliar with the tool mentioned as being used.) Since you didn't include any specifics of the names you were using for anything here, it's hard to say what exactly was causing the problem in your case. But there are no restrictions placed by Django on what a template tags library is "allowed" to access. Django doesn't attempt to enforce any sort of "only touch stuff from your own app" requirement. Karen --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-users@googlegroups.com To unsubscribe from this group, send email to django-users+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/django-users?hl=en -~----------~----~----~----~------~----~------~--~---