Django widget HTML

In the widget code, I need to access the HTML identifier of the created element. I know that I can run regexp on the displayed line and get the identifier , but I believe that there should be an easy way.

Suppose this is the widget that I have:

class TextInputWithHint(TextInput): ... def render(self, name, value, attrs): res = super(TextInputWithHint, self).render(name, value, attrs = attrs) res += mark_safe(u'<script type="text/javascript">alert(%s)</script>' \ % self.attrs['id']) return res 

Except that self.attrs['id'] does not work.

Is there an easy way to get the id here?

Thanks!

+4
source share
1 answer

In your use case, you can find the id attribute in the attrs argument, which is passed to render() . It is a good idea to test its existence before trying to use it:

 def render(self, name, value, attrs=None): # ... if attrs and 'id' in attrs: # Use attrs['id'] 
+4
source

All Articles