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


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