Electronics & Programming

develissimo

Open Source electronics development and programming

  • You are not logged in.
  • Root
  • » Django
  • » problem loading custom template tag [RSS Feed]

#1 Dec. 20, 2010 01:40:50

Nate R.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

problem loading custom template tag


Hi, I'm using Django 1.2.1 and I'm having problems trying to load my
template tags:

{% load mytags %}

TemplateSyntaxError at /myapp/

'mytags' is not a valid tag library: Template library mytags not
found, tried django.templatetags.mytags

It's defined in myproject/myapp/templatetags/mytags.py.

n...@nate-desktop:~/work/django-projects/myproject$ ls myapp/templatetags/
mytags.py __init.py__
n...@nate-desktop:~/work/django-projects/myproject$ more
myapp/templatetags/mytags.py
from django import template

register = template.Library()

@register.simple_tag
def myclass(request):
return request.path

I added 'myapp' to INSTALLED_APPS, and updated TEMPLATE_LOADERS (as
per a suggestion from StackOverflow):
TEMPLATE_LOADERS = (
'django.template.loaders.filesystem.Loader',
'django.template.loaders.app_directories.Loader',
'django.template.loaders.eggs.Loader',
'django.template.loaders.app_directories.load_template_source',
)

When I start the server I see this warning message:

/usr/local/lib/python2.6/dist-packages/django/template/loaders/eggs.py:4:
UserWarning: Module _mysql was already imported from
/usr/lib/pymodules/python2.6/_mysql.so, but
/usr/lib/pymodules/python2.6 is being added to sys.path

When I try to import my module in the shell I also am unable to import it:

>>> from django.templatetags import mytags
Traceback (most recent call last):
File "<console>", line 1, in <module>
ImportError: cannot import name mytags
>>> from myapp.templatetags import mytags
Traceback (most recent call last):
File "<console>", line 1, in <module>
ImportError: No module named templatetags

Does this mean something is wrong with my path or setup? Any ideas?

Thanks,
Nate

--
You received this message because you are subscribed to the Google Groups
"Django users" group.
To post to this group, send email to django-us...@googlegroups.com.
To unsubscribe from this group, send email to
django-users+unsubscr...@googlegroups.com.
For more options, visit this group athttp://groups.google.com/group/django-users?hl=en.

Offline

#2 Dec. 20, 2010 02:59:11

Karen T.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

problem loading custom template tag


On Sun, Dec 19, 2010 at 8:40 PM, Nate Reed <natereed....@gmail.com> wrote:

> When I try to import my module in the shell I also am unable to import it:
>
> >>> from django.templatetags import mytags
> Traceback (most recent call last):
> File "<console>", line 1, in <module>
> ImportError: cannot import name mytags
> >>> from myapp.templatetags import mytags
> Traceback (most recent call last):
> File "<console>", line 1, in <module>
> ImportError: No module named templatetags
>
> Does this mean something is wrong with my path or setup? Any ideas?
>

Looks like the myapp/templatetags directory is missing an __init__.py file.
You need this in order for Python to include it in the search for modules on
import.

(As a side note, the error message that implies only django.templatetags was
searched for your custom template tags was misleading and has been improved
in newer releases.)

Karen
--http://tracey.org/kmt/--
You received this message because you are subscribed to the Google Groups
"Django users" group.
To post to this group, send email to django-us...@googlegroups.com.
To unsubscribe from this group, send email to
django-users+unsubscr...@googlegroups.com.
For more options, visit this group athttp://groups.google.com/group/django-users?hl=en.

Offline

#3 Dec. 20, 2010 03:20:11

Nate R.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

problem loading custom template tag


Karen, thanks for your reply. The module myapp/templatetags does
have an __init__.py file. There is no __init__.pyc file, though.

n...@nate-desktop:~/work/django-projects/myproject/myapp$ ls templatetags/
mytags.py __init.py__

On Sun, Dec 19, 2010 at 8:59 PM, Karen Tracey <kmtra...@gmail.com> wrote:
> On Sun, Dec 19, 2010 at 8:40 PM, Nate Reed <natereed....@gmail.com> wrote:
>>
>> When I try to import my module in the shell I also am unable to import it:
>>
>> >>> from django.templatetags import mytags
>> Traceback (most recent call last):
>>  File "<console>", line 1, in <module>
>> ImportError: cannot import name mytags
>> >>> from myapp.templatetags import mytags
>> Traceback (most recent call last):
>>  File "<console>", line 1, in <module>
>> ImportError: No module named templatetags
>>
>> Does this mean something is wrong with my path or setup?  Any ideas?
>
> Looks like the myapp/templatetags directory is missing an __init__.py file.
> You need this in order for Python to include it in the search for modules on
> import.
>
> (As a side note, the error message that implies only django.templatetags was
> searched for your custom template tags was misleading and has been improved
> in newer releases.)
>
> Karen
> --
>http://tracey.org/kmt/>
> --
> You received this message because you are subscribed to the Google Groups
> "Django users" group.
> To post to this group, send email to django-us...@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.
>

--
You received this message because you are subscribed to the Google Groups
"Django users" group.
To post to this group, send email to django-us...@googlegroups.com.
To unsubscribe from this group, send email to
django-users+unsubscr...@googlegroups.com.
For more options, visit this group athttp://groups.google.com/group/django-users?hl=en.

Offline

#4 Dec. 20, 2010 03:30:30

Nate R.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

problem loading custom template tag


My mistake. There was a small typo. It was called __init.py__, not
__init__.py. I fixed it and it works now.

Thanks,
Nate


On Sun, Dec 19, 2010 at 9:20 PM, Nate Reed <natereed....@gmail.com> wrote:
> Karen, thanks for your reply.   The module myapp/templatetags does
> have an __init__.py file.  There is no __init__.pyc file, though.
>
> n...@nate-desktop:~/work/django-projects/myproject/myapp$ ls templatetags/
> mytags.py  __init.py__
>
> On Sun, Dec 19, 2010 at 8:59 PM, Karen Tracey <kmtra...@gmail.com> wrote:
>> On Sun, Dec 19, 2010 at 8:40 PM, Nate Reed <natereed....@gmail.com> wrote:
>>>
>>> When I try to import my module in the shell I also am unable to import it:
>>>
>>> >>> from django.templatetags import mytags
>>> Traceback (most recent call last):
>>>  File "<console>", line 1, in <module>
>>> ImportError: cannot import name mytags
>>> >>> from myapp.templatetags import mytags
>>> Traceback (most recent call last):
>>>  File "<console>", line 1, in <module>
>>> ImportError: No module named templatetags
>>>
>>> Does this mean something is wrong with my path or setup?  Any ideas?
>>
>> Looks like the myapp/templatetags directory is missing an __init__.py file.
>> You need this in order for Python to include it in the search for modules on
>> import.
>>
>> (As a side note, the error message that implies only django.templatetags was
>> searched for your custom template tags was misleading and has been improved
>> in newer releases.)
>>
>> Karen
>> --
>>http://tracey.org/kmt/>>
>> --
>> You received this message because you are subscribed to the Google Groups
>> "Django users" group.
>> To post to this group, send email to django-us...@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.
>>
>

--
You received this message because you are subscribed to the Google Groups
"Django users" group.
To post to this group, send email to django-us...@googlegroups.com.
To unsubscribe from this group, send email to
django-users+unsubscr...@googlegroups.com.
For more options, visit this group athttp://groups.google.com/group/django-users?hl=en.

Offline

Board footer

Moderator control

Enjoy the 13th of December
PoweredBy

The Forums are managed by develissimo stuff members, if you find any issues or misplaced content please help us to fix it. Thank you! Tell us via Contact Options
Leave a Message
Welcome to Develissimo Live Support