在Django中只渲染一个MultiWidget的一部分 [英] Render only one part of a MultiWidget in Django

查看:298
本文介绍了在Django中只渲染一个MultiWidget的一部分的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



在模板中呈现表单时,使用方便的符号

  {{formname.fieldname}} 

用于渲染的单个字段。当我使用它的字段与MultiWidget,它将显示了HTML的输入元件。是否有将只显示第一HTML输入元件的表示法的一个轻微的修改? ( {{formname.fieldname.0}} 不起作用。)

解决方案

我发现这个问题的解决方案需要两段代码。



首先



将MultiWidget转换为字符串的 render 方法相对较长。我们需要在最后一行复制并粘贴一个微小的修改,以使其重新生成数组。

  class OurMultiWidget form.MultiWidget):
...

def render(self,name,value,attrs = None):
从原始渲染方法复制并过去
if self.is_localized:
for widget在self.widgets中:
widget.is_localized = self.is_localized
#value是一个值列表,每个对应一个小部件
#在self.widgets。
如果不是isinstance(value,list):
value = self.decompress(value)
output = []
final_attrs = self.build_attrs(attrs)
id_ = final_attrs.get('id',None)
for i,widget in enumerate(self.widgets):
try:
widget_value = value [i]
除了IndexError:
widget_value =无
如果id_:
final_attrs = dict(final_attrs,id ='%s_%s'%(id_,i))
output.append(widget.render (name +'_%s'%i,widget_value,final_attrs))
#原始:
#返回mark_safe(self.format_output(输出))
#只有这一行是由我自己写的:
return [mark_safe(self.format_output(x))for x in output]

第二个



在模板中写入 {{formname.fieldname}} 将自动调用场小号unicode的方法和治疗结果为字符串。当我们想要一个数组时,我们需要规避unicode方法并直接访问它将返回的内容。而这是方法 as_widget 。因此,要调用 OurMultiWidget 的第一部分,我们将在模板中需要此代码:

  {{formname.fieldname.as_widget.0}} 


I have a Django Form Field with a MultiWidget that consists of two TextInputs.

When rendering the form in a template there is the handy notation

{{ formname.fieldname }}

for rendering a single field. When I use it for the field with the MultiWidget, it will display both HTML input elements. Is there a slight modification of the notation that will display only the first HTML input element? ({{ formname.fieldname.0 }} does not work.)

解决方案

I found a solution to this problem that needs two pieces of code.

First

The render method that turns the MultiWidget into a string is relatively long. We need to copy&paste it with a tiny modification in the last line to make it returing an array instead.

class OurMultiWidget(forms.MultiWidget):
    ...

    def render(self, name, value, attrs=None):
        """Copy and past from original render method"""
        if self.is_localized:
            for widget in self.widgets:
                widget.is_localized = self.is_localized
        # value is a list of values, each corresponding to a widget
        # in self.widgets.
        if not isinstance(value, list):
            value = self.decompress(value)
        output = []
        final_attrs = self.build_attrs(attrs)
        id_ = final_attrs.get('id', None)
        for i, widget in enumerate(self.widgets):
            try:
                widget_value = value[i]
            except IndexError:
                widget_value = None
            if id_:
                final_attrs = dict(final_attrs, id='%s_%s' % (id_, i))
            output.append(widget.render(name + '_%s' % i, widget_value, final_attrs))
        # Original:
        # return mark_safe(self.format_output(output))
        # Only this line was written by myself:
        return [mark_safe(self.format_output(x)) for x in output]

Second

Writing {{ formname.fieldname }} in the template will automatically call the field's unicode method and treat the result as a string. As we want an array instead, we need to circumvent the unicode method and access directly what it will return. And this is the method as_widget. Thus, to call the first part of OurMultiWidget, we will need this code in the template:

{{ formname.fieldname.as_widget.0 }}

这篇关于在Django中只渲染一个MultiWidget的一部分的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆