I modified some code so I made mistake again. -_-#

The right result looks like this:

$ ./manage.py shell
Python 2.5.1 (r251:54863, Apr 15 2008, 22:57:26)
[GCC 4.0.1 (Apple Inc. build 5465)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
(InteractiveConsole)
>>> from addressbook.core.templatetags.extra_filters import cut_by_string
>>> cut_by_string("Hello World", 3)
u'...'
>>> cut_by_string("Hello World", 5)
u'He...'
>>> cut_by_string("Hello World", 10)
u'Hello W...'
>>> cut_by_string("Hello World", 15)
u'Hello World'
>>>


On Nov 19, 6:09 pm, Daniel Roseman <[EMAIL PROTECTED]>
wrote:
> On Nov 19, 9:52 am, "K*K" <[EMAIL PROTECTED]> wrote:
>
>
>
> > Hi, All.
>
> > I create a custom filter for cut too long string for my app.
>
> > Like this:
>
> > from django import template
> > from django.template.defaultfilters import stringfilter
>
> > register = template.Library()
>
> > @register.filter(name='cut_by_string')
> > @stringfilter
> > def cut_by_string(value, arg):
> >         if len(value) < arg:
> >                 return value
> >         else:
> >                 return value[:arg-3] + "..."
>
> > And in the template I load the filter .py normally, and set below
> > codes:
>
> >         <div><a href="#">{{ testplan.name|cut_by_string:"a" }}</a></div>
>
> > But it doesn't work, do I make any mistake in this code ?
>
> I'm not quite sure what the filter is supposed to do, but you're
> passing a string and then checking if its length is 'less than'
> another string. This doesn't make sense. This line would seem to be
> the problem:
>     if len(value) < arg:
> According to the values you've passed, this evaluates to:
>     if len('string') < 'a':
> which will always be true.
>
> I wonder if you meant
>    if len(value) < len(arg)
> ??
>
> --
> DR.
--~--~---------~--~----~------------~-------~--~----~
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 [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to