[Twisted-web] How to set the selected item in a select box?

Donovan Preston dp at ulaluma.com
Wed Jul 20 13:42:22 MDT 2005

On Jul 20, 2005, at 9:26 AM, Thomas HERVE wrote:

> Quoting Alberto Trujillo <alberto.trujillo at ucd.ie>:
>> The problem is that the attribute selected allways appear in my  
>> html code, and then allways the last item is the selected.
>> Can anybody tell me how to fix this problem? If possible don't use  
>> a attribute even when I have set this one in my template?
> selected is a strange attribute as it don't have a value. You can  
> use this :
>    def render_selectauthor(self, ctx, data):
>        author = data.firstname + " " + data.lastname
>        ctx.fillSlots("authorvalue", data.id)
>        ctx.fillSlots("authorlabel", author)
>        if data.id == self.idauthor:
>            return ctx.tag(selected=True)
>        else:
>            return ctx.tag
> and remove the slot "selected" in your template.

It is also a known "issue" that if a slot is in an html attribute,  
there is no way to prevent that attribute from showing up in the HTML  
output. To do so would require Nevow have the ability to rewind time,  
to erase the attribute name from the output stream (in this case,  
selectecd="). There is a way to do this which would involve rendering  
the attribute value to a temporary location before rendering the  
attribute name, and checking to see if the value is a string of 0  
length or not. However, nobody has yet taken the time to implement  
this properly (it's tricky; several people have tried).

Once this issue is fixed, you could go back to using the slot  
approach. For now, what Thomas suggests is a reasonable workaround.


More information about the Twisted-web mailing list