views:

350

answers:

3

Hi!

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

+1  A: 

Paths in "class Media" are automatically prepended with settings.MEDIA_URL. So try like this:

class Media:
    js = ('js/rating.js',)
    css = {'screen': ('css/rating.css',),}

If it will not work, I suggest using FireBug or something like it and checking if browser is able to load css and javascript.

Based on comment: It appears, that you are not loading your form media at all. Try adding in the template:

{{ my_form.media }}

in {% block subheader %} (or wherever you are loading scripts and css), where "my_form" is the name of your form instance, created in the view.

Daniel Hernik
thx for the answer, but the MEDIA_URL is not the problem. When i debug it in the python console i get the correct output back, i mean>>> widget.mediaI think i need a special templatetag which should load all the media of the forms without repeat them.
Xidobix
+1  A: 

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

{% block extrahead %}
  {{ form.media }}
{% endblock %}
Daniel Roseman
A: 

Minor optimization: You don't need to prepend settings.MEDIA_URL to your paths, it's done automatically when it's printed out.

Emil Stenström