Django Widget Media 不起作用

发布于 2024-08-07 11:45:30 字数 900 浏览 1 评论 0原文

我需要一个小部件,它应该只显示一个块,因为我将使用 jQuery 添加功能。我试图通过 Widget 的“Media”类包含 javascript 和样式表,但它对我不起作用。

这是代码:

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))

任何建议或帮助将不胜感激

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

七颜 2024-08-14 11:45:30

您实际上是否在模板中的任何位置都包含了表单媒体?

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

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

{% block extrahead %}
  {{ form.media }}
{% endblock %}
匿名。 2024-08-14 11:45:30

“class Media”中的路径会自动在前面添加 settings.MEDIA_URL。所以尝试这样:

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

如果它不起作用,我建议使用 FireBug 或类似的东西并检查浏览器是否能够加载 css 和 javascript。

根据评论:
看来您根本没有加载表单媒体。尝试在模板中添加:

{{ my_form.media }}

在 {% block subheader %} (或加载脚本和 CSS 的任何位置),其中“my_form”是在视图中创建的表单实例的名称。

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.

初相遇 2024-08-14 11:45:30

小优化:您不需要在路径中添加 settings.MEDIA_URL,它会在打印时自动完成。

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文