How to make custom template tags resolve

I've got a simple bunch of custom template tags in my Django project that I've been using on my site for a while now without problems.  However, now that I'm trying out PyCharm, I find that it's complaining that these template tags can't be found.  Specifically, the error I'm getting when I mouseover the {% load hacks %} tag is: "Unresolved library: 'hacks'".

Here's what I've got:

/mysite

    appname/

        __init__.py

        templatetags/

            hacks.py # contains a function called "flaturl"

    templates/

        mytemplate.html


# hacks.py


from django import template


register = template.Library()


@register.simple_tag
def flaturl(title):

    # Some magic

    return "some string"

# mytemplate.html

{% load hacks %}

{% flaturl 'About' %}

PyCharm complains that hacks can't be resolved, and neither can flaturl.  Did I do something wrong?  From the looks of this ticket: http://youtrack.jetbrains.net/issue/PY-2239 it should work... right?

7 comments
Comment actions Permalink

Hello Daniel,

The way PyCharm resolves custom tag libraries is by looking at the INSTALLED_APPS

setting in settings.py, appending ".templatetags" to each of the app names,

and trying to find such a file in the project. So, everything should work

as long as you have the correct settings.py selected in Settings | Django

Support and you aren't doing anything fancy to build the list of installed

apps dynamically.

I've got a simple bunch of custom template tags in my Django project

that I've been using on my site for a while now without problems.

However, now that I'm trying out PyCharm, I find that it's complaining

that these template tags can't be found.  Specifically, the error I'm

getting when I mouseover the {% load hacks %} tag is: "Unresolved

library: 'hacks'".

Here's what I've got:

/mysite

appname/

__init__.py

templatetags/

hacks.py # contains a function called "flaturl"

templates/

mytemplate.html

  1. hacks.py

from django import template

register = template.Library()

@register.simple_tag

def flaturl(title):

  1. Some magic

return "some string"

  1. mytemplate.html

{% load hacks %}

{% flaturl 'About' %}

PyCharm complains that hacks can't be resolved, and neither can

flaturl.  Did I do something wrong?  From the looks of this ticket:

http://youtrack.jetbrains.net/issue/PY-2239 it should work... right?

--

Dmitry Jemerov

Development Lead

JetBrains, Inc.

http://www.jetbrains.com/

"Develop with Pleasure!"

1
Comment actions Permalink

Hi Daniel,

templatetags directory should be Python package, so make sure you have __init__.py there.

Adding to that what Dmitry has said everything should work fine.

1
Comment actions Permalink

That's the thing, I have the app in INSTALLED_APPS (It's called "core") and the templatetags directory is inside that directory.  Both "core" and "templatetags" have an __init__.py file, which of course they'd have to, since Django executes the template tag with no problems at all.  Here is the cropped portion of my directory tree.

./oxyor/core
./oxyor/core/templatetags
./oxyor/core/templatetags/__init__.py
./oxyor/core/templatetags/hacks.py
./oxyor/core/views.py
./oxyor/core/fixtures
./oxyor/core/fixtures/initial_data.json
./oxyor/core/__init__.py
./oxyor/core/context_processors.py
./oxyor/core/models.py
./oxyor/core/admin.py
./oxyor/core/forms.py
./oxyor/core/helpers.py
./oxyor/core/fields.py
./oxyor/manage.py
./oxyor/settings.py
./templates
./templates/index.html

I guess what I don't understand is how Django can find the templatetag and use it properly, but PyCharm can't.  I feel like I've misconfigured something, or I'm storing my files in a non-standard way... hence my questions here.

0
Comment actions Permalink

Daniel,

What version of PyCharm are you using?

If you can create a small demo of this behavior (in its own project) and send to me, I can try to help you out with this.

I've created custom tags and they seem to resolve fine.

Let me know..

Christian

0
Comment actions Permalink

...and you aren't doing anything fancy to build the list of installed

apps dynamically.

This is good to know.  Is it possible that a minor release of PyCharm could support dynamic INSTALLED_APPS settings?

I use a generic project that I release as open source, which contains appropriate settings for the project including INSTALLED_APPS, then I have a proprietary repository that has deployment-specific settings file that imports from the generic settings, as well as development-specific settings that imports from the generic settings and also adds to INSTALLED_APPS.

In the meantime, for my variation of this problem, I can confirm that I can use custom template tags by placing a tuple-of-strings declaration in INSTALLED_APPS for the settings.py that I'm pointing PyCharm to.

This ironically adds another point of maintenance and duplication, which I am using PyCharm to help me avoid.  :-)

0
Comment actions Permalink

Hello Matthew,

Please file a YouTrack issue and provide the code that you use to build the

list of INSTALLED_APPS. Supporting all ways to define the list dynamically

would essentially require us to implement a full Python interpreter, which

we don't currently plan to do, but specific cases can probably be supported.

...and you aren't doing

> anything fancy to build the list of installed

>

> apps dynamically.

This is good to know.  Is it possible that a minor release of PyCharm

could support dynamic INSTALLED_APPS settings?

I use a generic project that I release as open source, which contains

appropriate settings for the project including INSTALLED_APPS, then I

have a proprietary repository that has deployment-specific settings

file that imports from the generic settings, as well as

development-specific settings that imports from the generic settings

and also adds to INSTALLED_APPS.

In the meantime, for my variation of this problem, I can confirm that

I can use custom template tags by placing a tuple-of-strings

declaration in INSTALLED_APPS for the settings.py that I'm pointing

PyCharm to.

This ironically adds another point of maintenance and duplication,

which I am using PyCharm to help me avoid.  :-)

--

Dmitry Jemerov

Development Lead

JetBrains, Inc.

http://www.jetbrains.com/

"Develop with Pleasure!"

0
Comment actions Permalink

In an effort to post a simplified copy of my project with the problem, I appear to have inadvertently solved it.  I make a number of changes to settings.py, and one of them, though I have no idea which (sorry) seems to have solved the issue.

Sorry to bother you guys, but I wanted to let you know that it appears to be a PEBKAC issue.  Great job on PyCharm.

0

Please sign in to leave a comment.