On 25 February 2010 07:05, leoz01 <leozleo...@gmail.com> wrote:
> Hello,
>
> i have a simple question, how can i get the id attribute (which
> correspond to html id) from a form's field ?

I have this problem too when I wrote my wrapper for my django-ckeditor
app. It seems like an odd oversight, and from what I've read in the
source code, there's no clean way of getting it. I'm saying it is an
oversight because having a definitive way of getting the final html id
of a widget is important when using javascript widgets that replace
the form widget.

In my case, I was rendering a custom widget (derived from
widgets.Textarea), so I overrode the render method of that class, and
used the following code to get the id attribute -

# determine id of widget if possible, otherwise use name
        identifier = attrs.get('id')
        if identifier == None:
            identifier = name

I'm not quite sure what edge case caused the attrs dict to not have an
'id' attribute; just the first line might work for you.

Note that the attrs dictionary is generated right before render is
called, so there is no way of accessing it prior to that (it is quite
entwined with the rest of the form code).

Once I had the html id attribute value in the 'identifier' variable, I
added it to a template context and rendered my widget from there.

If someone has a better solution, I'd be happy to hear it.

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-us...@googlegroups.com.
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en.

Reply via email to