I am creating a custom widget for a datetime field:
我正在为datetime字段创建自定义窗口小部件:
class MySplitDateTimeWidget(forms.SplitDateTimeWidget):
def format_output(self, rendered_widgets):
mytimeid = self.widgets[1].attrs['id'] #####NEED HELP HERE
temp = "javascript:$('%s').val(new Date().getHours());" % mytimeid
temp1 = '<a href="%s">Now</a>' % temp
return mark_safe(u'%s %s<br />%s %s %s' % \
(_('Date:'), rendered_widgets[0], _('Time:'), rendered_widgets[1],
temp1
))
I need the "id" attribute of the widget, however self.widgets doesn't include the "id" attribute in attrs. It includes every other attribute though. I'm not sure where this attribute comes from?
我需要小部件的“id”属性,但self.widgets不包括attrs中的“id”属性。它包括所有其他属性。我不确定这个属性来自哪里?
2 个解决方案
#1
1
I was just grappling with the exact same thing; hopefully this is useful for other people. The "id" attr is set via:
我正在努力解决同样的事情;希望这对其他人有用。 “id”attr通过以下方式设置:
- Form is asked to render itself
- Form iterates through its fields
- For each field, the form calls its custom
__getitem__()
which wraps the field as a BoundField - The BoundField, in the
as_widget()
method, is what actually sets the "id" attribute (see also theauto_id()
method) - The MultiWidget then performs its
render()
method, which renders each of its child widgets and then joins them withformat_output()
要求表单呈现自己
表单遍历其字段
对于每个字段,表单调用其自定义__getitem __(),该字段将字段包装为BoundField
as_widget()方法中的BoundField实际上是设置“id”属性(另请参见auto_id()方法)
然后,MultiWidget执行其render()方法,该方法呈现其每个子窗口小部件,然后使用format_output()连接它们
So, to answer your question, you want to get the ID in the render()
method and not the format_output()
method:
因此,要回答您的问题,您希望在render()方法中获取ID,而不是format_output()方法:
class MySplitDateTimeWidget(forms.SplitDateTimeWidget):
def render(self, name, value, attrs=None):
widgets_html = super(MySplitDateTimeWidget, self).render(name, value, attrs)
# attrs['id'] is the ID of the entire widget, append the prefix to chose the sub-widget
mytimeid = attrs['id'] + '_0'
temp = "javascript:$('%s').val(new Date().getHours());" % mytimeid
temp1 = '<a href="%s">Now</a>' % temp
return mark_safe(widgets_html + ' ' + temp1)
def format_output(self, rendered_widgets):
return mark_safe(u'%s %s<br />%s %s' % (_('Date:'), rendered_widgets[0], _('Time:'), rendered_widgets[1]))
#2
0
Unless you've overwritten it, the ID should be:
除非你覆盖它,否则ID应该是:
id_[name]
So try:
mytimeid = 'id_'+self.widgets[1].attrs['name']
#1
1
I was just grappling with the exact same thing; hopefully this is useful for other people. The "id" attr is set via:
我正在努力解决同样的事情;希望这对其他人有用。 “id”attr通过以下方式设置:
- Form is asked to render itself
- Form iterates through its fields
- For each field, the form calls its custom
__getitem__()
which wraps the field as a BoundField - The BoundField, in the
as_widget()
method, is what actually sets the "id" attribute (see also theauto_id()
method) - The MultiWidget then performs its
render()
method, which renders each of its child widgets and then joins them withformat_output()
要求表单呈现自己
表单遍历其字段
对于每个字段,表单调用其自定义__getitem __(),该字段将字段包装为BoundField
as_widget()方法中的BoundField实际上是设置“id”属性(另请参见auto_id()方法)
然后,MultiWidget执行其render()方法,该方法呈现其每个子窗口小部件,然后使用format_output()连接它们
So, to answer your question, you want to get the ID in the render()
method and not the format_output()
method:
因此,要回答您的问题,您希望在render()方法中获取ID,而不是format_output()方法:
class MySplitDateTimeWidget(forms.SplitDateTimeWidget):
def render(self, name, value, attrs=None):
widgets_html = super(MySplitDateTimeWidget, self).render(name, value, attrs)
# attrs['id'] is the ID of the entire widget, append the prefix to chose the sub-widget
mytimeid = attrs['id'] + '_0'
temp = "javascript:$('%s').val(new Date().getHours());" % mytimeid
temp1 = '<a href="%s">Now</a>' % temp
return mark_safe(widgets_html + ' ' + temp1)
def format_output(self, rendered_widgets):
return mark_safe(u'%s %s<br />%s %s' % (_('Date:'), rendered_widgets[0], _('Time:'), rendered_widgets[1]))
#2
0
Unless you've overwritten it, the ID should be:
除非你覆盖它,否则ID应该是:
id_[name]
So try:
mytimeid = 'id_'+self.widgets[1].attrs['name']