Flask-wtf:使用kwarg设置字段的初始值

时间:2015-06-15 20:31:31

标签: python flask wtforms flask-wtforms

我想使用kwarg来设置表单字段的初始值。我之前做过类似的事情:

from wtforms import Form
from wtforms.fields import StringField

class BasicForm(Form):
    inputField = StringField("Name")

basic_form_kwargs = {"inputField" : "Example"}
basic_form = BasicForm(**basic_form_kwargs)
print basic_form.inputField
#<input id="inputField" name="inputField" type="text" value="Example">

此代码按预期工作。 value属性设置为“Example”。但是,当我开始使用FormFields时,它无法正常工作。

from wtforms import Form
from wtforms.fields import StringField

class ChildForm(Form):
    inputField = StringField("Name")
class ParentForm(Form):
    childWrapper = FormField(ChildForm)

complex_form_kwargs = {"childWrapper-inputField" : "Example"}
complex_form = ParentForm(**complex_form_kwargs)
print complex_form.childWrapper.inputField
#<input id="childWrapper-inputField" name="childWrapper-inputField" type="text" value="">

需要传递什么样的kwarg才能在使用FormFields时设置inputField的值?

1 个答案:

答案 0 :(得分:1)

就像你将dict传递给父级表单一样,你需要将dict传递给子表单,如下所示:

13:53:39,653 DEBUG [org.mule.transport.jms.MultiConsumerJmsMessageReceiver] (TIBCO EMS Session Dispatcher (741)) Message received it is of type: TibjmsTextMessage
13:53:39,653 DEBUG [org.mule.transport.jms.MultiConsumerJmsMessageReceiver] (TIBCO EMS Session Dispatcher (741)) Message received on Queue[M.ESS.ITM.LOYALTY.STARS.ItemMaint] (com.tibco.tibjms.TibjmsQueue)
13:53:39,653 DEBUG [org.mule.transport.jms.MultiConsumerJmsMessageReceiver] (TIBCO EMS Session Dispatcher (741)) Message CorrelationId is: 
13:53:39,653 DEBUG [org.mule.transport.jms.MultiConsumerJmsMessageReceiver] (TIBCO EMS Session Dispatcher (741)) Jms Message Id is: ID:EMS-SERVER.A305502CE7A4:995
13:53:39,653 DEBUG [org.mule.transport.jms.transformers.JMSMessageToObject] (TIBCO EMS Session Dispatcher (741)) Applying transformer JMSMessageToObject#218219976 (org.mule.transport.jms.transformers.JMSMessageToObject)
13:53:39,653 DEBUG [org.mule.transport.jms.transformers.JMSMessageToObject] (TIBCO EMS Session Dispatcher (741)) Object before transform: 
org.mule.DefaultMuleMessage
{
  id=998d2f50-13a0-11e5-b0f9-ac87a31af20c
  payload=com.tibco.tibjms.TibjmsTextMessage
  correlationId=
  correlationGroup=-1
  correlationSeq=-1
  encoding=UTF-8
  exceptionPayload=<not set>

Message properties:
  INVOCATION scoped properties:
  INBOUND scoped properties:
    JMSCorrelationID=
    JMSDeliveryMode=1
    JMSDestination=Queue[M.ESS.ITM.LOYALTY.STARS.ItemMaint]
    JMSExpiration=0
    JMSMessageID=ID:EMS-SERVER.A305502CE7A4:995
    JMSPriority=4
    JMSRedelivered=false
    JMSTimestamp=1426256459016
    JMSType=
    MULE_CORRELATION_ID=
    MULE_MESSAGE_ID=ID:EMS-SERVER.A305502CE7A4:995
  OUTBOUND scoped properties:
    MULE_ENCODING=UTF-8
  SESSION scoped properties:
}
13:53:39,654 DEBUG [org.mule.transport.jms.transformers.JMSMessageToObject] (TIBCO EMS Session Dispatcher (741)) Source object is TibjmsTextMessage
13:53:39,654 DEBUG [org.mule.transport.jms.transformers.JMSMessageToObject] (TIBCO EMS Session Dispatcher (741)) Message type received is: TibjmsTextMessage
13:53:39,654 DEBUG [org.mule.transport.jms.transformers.JMSMessageToObject] (TIBCO EMS Session Dispatcher (741)) Resulting object is String
13:53:39,654 DEBUG [org.mule.transport.jms.transformers.JMSMessageToObject] (TIBCO EMS Session Dispatcher (741)) Object after transform: <?xml version="1.0" encoding="UTF-8" ?>
<ns0:RewardOfferDetail xmlns:ns0="http://example.com/schemas/v1_0/RewardOffer">
  <ns0:Offer>
    <ns0:OperationCode>U</ns0:OperationCode>
  </ns0:Offer>
  <ns0:Offer>
    <ns0:OperationCode>A</ns0:OperationCode>
  </ns0:Offer>
</ns0:RewardOfferDetail>
13:53:39,654 DEBUG [org.mule.transport.jms.transformers.JMSMessageToObject] (TIBCO EMS Session Dispatcher (741)) The transformed object is of expected type. Type is: String
13:53:39,669 DEBUG [org.mule.expression.DefaultExpressionManager] ([loyalty-nebula].receiveMessageFlow.stage1.02) Result of expression: xpath-branch:/rewardns:RewardOfferDetail/rewardns:Offer is: [[ns0:Offer: null], [ns0:Offer: null]]
13:53:39,669 DEBUG [org.mule.processor.chain.DefaultMessageProcessorChain] ([loyalty-nebula].receiveMessageFlow.stage1.02) Invoking DefaultMessageProcessorChain '(inner iterating chain) of 'receiveMessageFlow' processor chain' 
[ 
  AddFlowVariableTransformer{this=589f1380, name='AddFlowVariableTransformer', ignoreBadInput=false, returnClass=SimpleDataType{type=java.lang.Object, mimeType='*/*'}, sourceTypes=[SimpleDataType{type=java.lang.Object, mimeType='*/*'}]}, 
  org.mule.api.processor.LoggerMessageProcessor@be3c4ce, 
  org.mule.routing.requestreply.AsyncReplyToPropertyRequestReplyReplier
] with event MuleEvent: 0-998d2f53-13a0-11e5-b0f9-ac87a31af20c, stop processing=false, jms://M.ESS.ITM.LOYALTY.STARS.ItemMaint
13:53:39,669 DEBUG [org.mule.transformer.simple.AddFlowVariableTransformer] ([loyalty-nebula].receiveMessageFlow.stage1.02) Applying transformer AddFlowVariableTransformer (org.mule.transformer.simple.AddFlowVariableTransformer)
13:53:39,670 DEBUG [org.mule.transformer.simple.AddFlowVariableTransformer] ([loyalty-nebula].receiveMessageFlow.stage1.02) Object before transform: 
org.mule.DefaultMuleMessage
{
  id=998fa050-13a0-11e5-b0f9-ac87a31af20c
  payload=org.apache.xerces.dom.ElementNSImpl
  correlationId=<not set>
  correlationGroup=2
  correlationSeq=1
  encoding=UTF-8
  exceptionPayload=<not set>

Message properties:
  INVOCATION scoped properties:
  INBOUND scoped properties:
  OUTBOUND scoped properties:
    MULE_CORRELATION_GROUP_SIZE=2
    MULE_CORRELATION_SEQUENCE=1
    MULE_ENCODING=UTF-8
  SESSION scoped properties:
}
13:53:39,670 DEBUG [org.mule.expression.DefaultExpressionManager] ([loyalty-nebula].receiveMessageFlow.stage1.02) Result of expression: xpath-branch://rewardns:OperationCode is: [[ns0:OperationCode: null], [ns0:OperationCode: null]]
13:53:39,670 DEBUG [org.mule.transformer.simple.AddFlowVariableTransformer] ([loyalty-nebula].receiveMessageFlow.stage1.02) Object after transform: 
org.mule.DefaultMuleMessage
{
  id=998fa050-13a0-11e5-b0f9-ac87a31af20c
  payload=org.apache.xerces.dom.ElementNSImpl
  correlationId=<not set>
  correlationGroup=2
  correlationSeq=1
  encoding=UTF-8
  exceptionPayload=<not set>

Message properties:
  INVOCATION scoped properties:
    action=[[ns0:OperationCode: null], [ns0:OperationCode: null]]
  INBOUND scoped properties:
  OUTBOUND scoped properties:
    MULE_CORRELATION_GROUP_SIZE=2
    MULE_CORRELATION_SEQUENCE=1
    MULE_ENCODING=UTF-8
  SESSION scoped properties:
}
13:53:39,671 DEBUG [org.mule.transformer.simple.AddFlowVariableTransformer] ([loyalty-nebula].receiveMessageFlow.stage1.02) The transformed object is of expected type. Type is: DefaultMuleMessage
13:53:39,672 WARN  [org.mule.api.processor.LoggerMessageProcessor] ([loyalty-nebula].receiveMessageFlow.stage1.02) Action is: '[[ns0:OperationCode: null], [ns0:OperationCode: null]]'