Django Widget Media 不起作用
我需要一个小部件,它应该只显示一个块,因为我将使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您实际上是否在模板中的任何位置都包含了表单媒体?
Are you actually including the form media in your template anywhere?
“class Media”中的路径会自动在前面添加 settings.MEDIA_URL。所以尝试这样:
如果它不起作用,我建议使用 FireBug 或类似的东西并检查浏览器是否能够加载 css 和 javascript。
根据评论:
看来您根本没有加载表单媒体。尝试在模板中添加:
在 {% block subheader %} (或加载脚本和 CSS 的任何位置),其中“my_form”是在视图中创建的表单实例的名称。
Paths in "class Media" are automatically prepended with settings.MEDIA_URL. So try like this:
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:
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.
小优化:您不需要在路径中添加 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.