Well, a little step ahead: the filter receives an empty string.

In the template the line:
    {{ request.path|dovesono }}

passes to the "dovesono" filter a string with length 0.

For completeness:
The template:
===========================================================================
{% extends "base_index.html" %}
{% block coltype %}funzioni{% endblock %}

{% block bodyclass %}{% endblock %}

{% block breadcrumbs %}
    {% load mytags %}
    <div class="breadcrumbs">
    {{ request.path|dovesono }}
    </div>
{% endblock %}

{% block content %}

[...snip...]
===========================================================================

The filter
===========================================================================
#!/usr/bin/python
# encoding: utf-8
from django import template
from django.utils.safestring import mark_safe
from django.utils.translation import ugettext
from django.template.defaultfilters import stringfilter
from types import *

register = template.Library()

@register.filter
@stringfilter
def dovesono(aurl):
  
   bc = u''
   parti=aurl.split('/')
  
   bc= u' aurl0=%s len=%d char= %s isstring=%s' % 
('no',len(aurl),aurl.count('/'),(isinstance(aurl, basestring)))
     
   for i in range(len(aurl)):
      bc=bc+' &raquo '+aurl[i]
  
   return mark_safe(bc)
===========================================================================

The html result

===========================================================================

        <div class="breadcrumbs">
     aurl0=no len=0 char= 0 isstring=True 
    </div>

===========================================================================

Any hint?

Thanks
    Mirto

Mirto Silvio Busico ha scritto:
> Thanks; but I was not clear.
> I'll try to explain: the code is not intended to really create the 
> breadcrumbs. The code tries to identify the problem.
>
> The problem is that the statement:
>     parti=aurl.split('/')
> doesn't generate a list
>
> Moreover the statement:
>     aurl.count('/') (with the supposed url that is 
> "http://localhost/zsite/ancore/";) gives the value 0 (I expected 4)
>
> Worst, I used your suggestion and now the bc assignement line is
>     bc= u' - %s - len=%d char= %s %s' % 
> (parti[0],len(parti),aurl.count('/'),(isinstance(aurl, basestring)))
> and isinstance(aurl, basestring) id evaluated as True (so aurl is a 
> string; but split('/') doesn't work)
>
> I don't know what to look for: I receive a string, the string contains 
> '/', the string.split('/') doesn't generate the expected 5 elements.
> Obvoiusly, the same code pasted in the shell (python manage.py shell) 
> works as expected.
>
> Any Hint?
>
> Thanks
>     Mirto
>
> P.S. BTW the urls are different from development framework and production:
>
>     * runserver: http://localhost:8000/ancore/
>     * apache:     http://localhost/zsite/ancore/
>
>
> bruno desthuilliers ha scritto:
>   
>> On 14 oct, 16:34, Mirto Silvio Busico <[EMAIL PROTECTED]> wrote:
>>   
>>     
>>> Hi all,
>>> I'm trying to pass a string to a custom tag to create custom breadcrumbs.
>>>
>>> I'm using as a base the snippet 
>>> athttp://www.djangosnippets.org/snippets/656/
>>> and using the documentation 
>>> fromhttp://docs.djangoproject.com/en/dev/howto/custom-template-tags/
>>>
>>> But it seems that I'm not able to pass the URL as a string.
>>>
>>> In ... zsite/ancore/templatetags/mytags.py (zsite is the django project
>>> directory) I have:
>>> ==============================================================
>>> #!/usr/bin/python
>>> # encoding: utf-8
>>> from django import template
>>> from django.utils.safestring import mark_safe
>>> from django.utils.translation import ugettext
>>> from django.template.defaultfilters import stringfilter
>>> from types import *
>>>
>>> register = template.Library()
>>>
>>> @register.filter(name='dovesono')
>>> @stringfilter
>>> def dovesono(aurl):
>>>
>>>    bc = u''
>>>    parti=aurl.split('/')
>>>
>>>    bc= u' - %s - len=%d char= %s %s' %
>>> (parti[0],len(parti),aurl.count('/'),(type(aurl) == StringTypes))
>>>     
>>>       
>> StringTypes is a tuple (<type 'str'>, <type 'unicode'>). So you want :
>>   type(aurl) in StringTypes
>>
>> which is better written:
>>   isinstance(aurl, basestring)
>>
>> but anyway, given your example use case, this test is more than
>> probably more than useless...
>>
>>   
>>     
>>>    for i in range(len(parti)):
>>>       bc=bc+' &raquo '+parti[i]
>>>     
>>>       
>> The canonical way to iterate over a sequence is:
>>
>>      for elt in sequence:
>>         do_whatever_with(elt)
>>
>> but you don't need such a low-level construct here - just use
>> str.join(seq):
>>
>>      bc = u' &raquo '.join(parti)
>>
>> Note that since request.path is an absolute path (iow with leading
>> slash), and may have a trailing one too, you may want to get rid of
>> both before splitting the request.path, ie:
>>
>>     parti = aurl.strip('/').split('/')
>>
>>     # code here
>>
>>   
>>     
>>>    return mark_safe(bc)
>>> ==============================================================
>>>
>>> In the calling template I have:
>>> ==============================================================
>>> {% block breadcrumbs %}
>>>     {% load mytags %}
>>>     <div class="breadcrumbs">
>>>     {{ request.get_full_path|dovesono }}
>>>     
>>>       
>> request.get_full_path will also gives you the querystring part if
>> there's one. I think you want request.path instead.
>>
>>   
>>     
>>>     </div>
>>> {% endblock %}
>>> ==============================================================
>>>
>>> and the generated html is:
>>> ==============================================================
>>>
>>>         <div class="breadcrumbs">
>>>      -  - len=1 char= 0 False &raquo
>>>     </div>
>>>
>>> ==============================================================
>>>
>>> So (type(aurl) == StringTypes) is evaluated False
>>>
>>> What I'm doing wrong?
>>> What is the method to pass the actual url to a filter as a string?
>>>     
>>>       
>> It is already one. The bug is elsewhere.
>>
>> HTH
>>
>>
>>     
>>   
>>     
>
>
>   


-- 

_________________________________________________________________________
Busico Mirto Silvio
Consulente ICT
cell. 333 4562651
email [EMAIL PROTECTED]


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