Django Widget Media doesn't work

Xidobix picture Xidobix · Oct 13, 2009 · Viewed 12.1k times · Source

I need a widget, which should only display a block, because i will add functionality with jQuery. I am trying to include the javascript and stylesheet trough the "Media" class of Widget and it doesn't work for me.

Here is the code:

class StarsWidget(Widget):
    """
    Widget with stars for rating
    """
    class Media:
        js = (
                settings.MEDIA_URL + 'js/rating.js',
              )

        css = {
                'screen': (
                    settings.MEDIA_URL + 'css/rating.css',
                    )
              }


    def render(self, name, value, attrs=None):
        if value is None: value = ''

        attrs = {'id':'ratingX', 'class':'rating'}
        final_attrs = self.build_attrs(attrs, name=name)
        if value != '':
            # Only add the 'value' attribute if a value is non-empty.
            final_attrs['value'] = force_unicode(value)
        return mark_safe(u'<div %s ></div>' % flatatt(final_attrs))

Any suggestions or help will be appreciated

Answer

Daniel Roseman picture Daniel Roseman · Oct 13, 2009

Are you actually including the form media in your template anywhere?

{% block extrahead %}
  {{ form.media }}
{% endblock %}