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 -~----------~----~----~----~------~----~------~--~---