Django表单小部件呈现

2024-10-01 07:11:42 发布

您现在位置:Python中文网/ 问答频道 /正文

嘿,伙计们,我想创建一个widget,以下面的格式呈现一个phone字段

box - box2 - box3

我试过在同一个网站上提供的不同的代码片段,但是它们没有覆盖html呈现,这是我想要做的

例如:

^{pr2}$

呈现html输入:

box|box2|box3

那么,如何覆盖渲染以使其渲染:

box - box2 - box3

我也很感谢任何解释如何创建自定义小部件的文档,到目前为止我还没有找到任何文档

在模型.py公司名称:

class Preference(models.Model):
    phone = models.PositiveIntegerField(blank=True)

class PreferenceForm(ModelForm):

    class Meta:
        model = Preference
        widgets = {
            'phone':USPhoneNumberMultiWidget(attrs={'class':'firstnumberbox', 'id':'firstcellphone', 'name':'firstphone'}),

Html呈现:

<dt><label for="firstcellphone"> Cell Phone:</label></dt> 
        <dd class="phones"><input name="firstphone" id="firstcellphone_0" maxlength="3" type="text" class="firstnumberbox" size="3" /> - <input name="firstphone" id="firstcellphone_1" maxlength="3" type="text" class="firstnumberbox" size="3" /> - <input name="firstphone" id="firstcellphone_2" maxlength="4" type="text" class="firstnumberbox" size="4" /></dd>

Tags: textnameboxidinputsizetypephone
1条回答
网友
1楼 · 发布于 2024-10-01 07:11:42

您可以重写MultiWidget的format_output方法:

def format_output(self, rendered_widgets):
        return u'%s - %s - %s' % \
            (rendered_widgets[0], rendered_widgets[1], rendered_widgets[2])

关于定制表单小部件,没有大量的文档。我只做了一对,他们做了很多修补。希望有帮助!在

相关问题 更多 >