Thanks Russell, that works well.
On Thu, Nov 13, 2014 at 11:30 PM, Russell Keith-Magee < [email protected]> wrote: > Hi Michael, > > The best bet would be to write a custom widget, and override the render() > method. render() takes the name of the widget, the value to render, and a > dictionary of attr values. In your subclass, you can override this method > to inject the additional attributes based on the provided value - something > like: > > class MyWidget(TextInput): > def render(self, name, value, attrs=None): > if value == SPECIAL VALUE: > attrs['class'] = 'class1 class2' > return super(MyWidget, self).render(name, value, attrs) > > This is an oversimplification - there's a few extra edge cases you'll need > to account for - if attrs is none, or if 'class' already exists - but the > idea should hopefully make sense. > > Yours, > Russ Magee %-) > > > > On Wed, Nov 12, 2014 at 9:25 PM, Michael <[email protected]> > wrote: > >> Hi, >> >> The ``attrs`` attribute of a widget allows to define the html attributes. >> For example, setting it to ``widget.attrs['class'] = 'class1 class2'`` will >> add ``class="class1 class2"`` on the rendered widget. >> >> However, how can I set them based on the data? I display an update form >> and I'd like to change the CSS class based on the data that is going to be >> displayed when the form is rendered. >> >> Where could I check for the data in order to change the ``attrs`` >> property of the widget before it is rendered? >> >> Thanks >> >> -- >> You received this message because you are subscribed to the Google Groups >> "Django users" group. >> To unsubscribe from this group and stop receiving emails from it, send an >> email to [email protected]. >> To post to this group, send email to [email protected]. >> Visit this group at http://groups.google.com/group/django-users. >> To view this discussion on the web visit >> https://groups.google.com/d/msgid/django-users/3569acd7-5bec-4df9-8960-4952b3fee8fb%40googlegroups.com >> <https://groups.google.com/d/msgid/django-users/3569acd7-5bec-4df9-8960-4952b3fee8fb%40googlegroups.com?utm_medium=email&utm_source=footer> >> . >> For more options, visit https://groups.google.com/d/optout. >> > > -- > You received this message because you are subscribed to a topic in the > Google Groups "Django users" group. > To unsubscribe from this topic, visit > https://groups.google.com/d/topic/django-users/YADHgsrm3Gk/unsubscribe. > To unsubscribe from this group and all its topics, send an email to > [email protected]. > To post to this group, send email to [email protected]. > Visit this group at http://groups.google.com/group/django-users. > To view this discussion on the web visit > https://groups.google.com/d/msgid/django-users/CAJxq849qJkr6GrqK-6FQ7phpgAAR%3Dk3c_0CWNsv%3DyuJy9rXWzg%40mail.gmail.com > <https://groups.google.com/d/msgid/django-users/CAJxq849qJkr6GrqK-6FQ7phpgAAR%3Dk3c_0CWNsv%3DyuJy9rXWzg%40mail.gmail.com?utm_medium=email&utm_source=footer> > . > > For more options, visit https://groups.google.com/d/optout. > -- You received this message because you are subscribed to the Google Groups "Django users" group. To unsubscribe from this group and stop receiving emails from it, send an email to [email protected]. To post to this group, send email to [email protected]. Visit this group at http://groups.google.com/group/django-users. To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CALa-rBxfz3H19_POKGc5fZRPD_c_vLxk3JhAeoDMzt7ZkytQ8A%40mail.gmail.com. For more options, visit https://groups.google.com/d/optout.

