我正在尝试为Django构建一个更好/更强大的表单类。除了这些子窗体,它工作得很好。实际上,它在我重新启动Apache之后就可以很好地工作,但是在我刷新页面几次之后,我的HTML输出开始如下所示:
<input class="text" type="text" id="pickup_addr-pickup_addr-pickup_addr-id-pickup_addr-venue" value="" name="pickup_addr-pickup_addr-pickup_addr-pickup_addr-venue" />
这个
pickup_addr-
部分开始重复多次。我在找环
prefix
可能导致这种情况发生的代码,但是刷新页面时输出甚至不一致,所以我认为有些东西正在缓存到某个地方,但我甚至无法想象这是怎么可能的。这个
前缀
初始化类时应重置var,否?除非是某种原因
不
初始化什么?
class Form(object):
count = 0
def __init__(self, data={}, prefix='', action='', id=None, multiple=False):
self.fields = {}
self.subforms = {}
self.data = {}
self.action = action
self.id = fnn(id, 'form%d' % Form.count)
self.errors = []
self.valid = True
if not empty(prefix) and prefix[-1:] not in ('-','_'): prefix += '-'
for name, field in inspect.getmembers(self, lambda m: isinstance(m, Field)):
if name[:2] == '__': continue
field_name = fnn(field.name, name)
field.label = fnn(field.label, humanize(field_name))
field.name = field.widget.name = prefix + field_name + ife(multiple, '[]')
field.id = field.auto_id = field.widget.id = ife(field.id==None, 'id-') + prefix + fnn(field.id, field_name) + ife(multiple, Form.count)
field.errors = []
val = fnn(field.widget.get_value(data), field.default)
if isinstance(val, basestring):
try:
val = field.coerce(field.format(val))
except Exception, err:
self.valid = False
field.errors.append(escape_html(err))
field.val = self.data[name] = field.widget.val = val
for rule in field.rules:
rule.fields = self.fields
rule.val = field.val
rule.name = field.name
self.fields[name] = field
for name, form in inspect.getmembers(self, lambda m: ispropersubclass(m, Form)):
if name[:2] == '__': continue
self.subforms[name] = self.__dict__[name] = form(data=data, prefix='%s%s-' % (prefix, name))
Form.count += 1
如果您需要更多代码,请告诉我…我知道很多,但我就是不知道是什么造成的!我甚至没有使用任何缓存中间件。
复制/克隆字段首先会提供此输出:
<label for="None">None</label>
<input class="text" type="text" id="id-pickup_address-venue" value="" name="pickup_address-venue" />
field.name
和
field.label
以完全相同的方式设置…事实上,
field.id
正确显示在
<input>
但当我试图打印标签时,同样的值突然消失了……区别在于
<输入& GT;
位由打印
Widget
类,而
label
直接从模板打印…我想是……哦,我明白了,
那个
一个是指未设置的类级/静态字段,而不是实例字段…