问题描述
因此存在一个代码段:
我打算重用它,但是当我在forms.py中声明它并渲染html时。
I was going tor reuse this, but when I declare this in my forms.py, and I render the html..
submit_button = SubmitButtonField()
value
未提供(转义为字符串 None
),而 name
默认为 submit_button
,可能是由于 forms.Widget
构造函数所致。
value
is not supplied (it's escaped to string None
), while name
is default to submit_button
, probably because of the forms.Widget
constructor.
如何提供名称和价值?我试图为小部件编写一个初始化方法...
How can I supply the name and the value? I tried to write an init method for the widget...
def __init__(self, name, value, label, attrs):
print '%s, %s, %s, %s' %(name, value, label, attrs)
self.name, self.value,self.label = name, value, label
self.attrs = attrs
但是如果该字段是总是(默认)默认使用小部件 SubmitButtonWidget
...代替,我应该在字段init方法中提供这些值。但这不允许我这样做。
But this doesn't quite make sense if the field is always (supposedly) default to use the widget SubmitButtonWidget
... instead I should supply these values in the field init method. But it doesn't allow me to do.
我应该如何重写此代码段,使其可以接受我提供的值?
How should I rewrite this snippet so it can accept my supplied values?
推荐答案
如摘要顶部的注释中所述,用法是
As written in the comment at the very top of the snippet, the usage is
submit_button = SubmitButtonField(label="", initial=u"Your submit button text")
您缺少的值来自初始
关键字参数。名称是表单字段的名称。
Your missing value comes from the initial
keyword argument. The name is the name of the form field.
这篇关于制作提交按钮表单字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!