Merci beaucoup!
it works fine now.

>Now, and while this is none of my problems, I'd seriously question the
decision of building html that way. This would be better done using
templates IMVHO.

-> I'm using this function in a custom tag to build a menu

On 12 nov, 10:59, bruno desthuilliers <[EMAIL PROTECTED]>
wrote:
> On 12 nov, 09:22, gontran <[EMAIL PROTECTED]> wrote:
>
>
>
> > Hello,
>
> > I've got a problem with a recursive function:
> > in my model, I have a class named myClass: each object in my class has
> > an id, a parentId, and a name.
> > I want to make a list of all my objects that looks like this:
> > <ul>
> >     <li>object1</li>
> >     <li>object2</li>
> >     <li>object3
> >         <ul>
> >             <li>object4</li>
> >             <li>object6</li>
> >         </ul></li>
> >     <li>object5</li>
> >     ...
> > </ul>
>
> > So I wrote a function displayListObject():
> > def displayListObject(parentId, displayString):
> >     displayString += '<ul>'
> >     elements = myClass.objects.filter(parentId=parentId)
> >     for element in elements:
> >         if  myClass.objects.filter(parentId=page.id):
> >             displayString += '<li>' + page.id + '</li>'
> >             displayListObject(page.id, displayString)
>
> (snip)
>
> > Any idea to fix this?
>
> Yes : don't pass displayString as an argument, just use the return
> value of the function, ie:
>
> def displayListObject(parentId):
>     displayString = '<ul>'
>     elements = myClass.objects.filter(parentId=parentId)
>     for element in elements:
>         if  myClass.objects.filter(parentId=page.id):
>             displayString += '<li>' + page.id + '</li>'
>             displayString += displayListObject(page.id, displayString)
>         else:
>             displayString += '<li>' + page.id + '</li>'
>     displayString += '</ul>'
>     return displayString
>
> The root of your problem is that python strings are immutable. So the
> augmented assignment ('+=') rebind the name displayString to a new
> string object. And since parameters names are local to the function,
> this rebinding (hopefully) only affect the local namespace.
>
> Now, and while this is none of my problems, I'd seriously question the
> decision of building html that way. This would be better done using
> templates IMVHO.
>
> 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