基本上,你可以從窗體中獲得處理程序並手動調用。
這是一個常規z3c.form和敏捷添加表單的例子。
>>> form_view = self.rf.restrictedTraverse('add-file')
# If your form is a Dexterity DefaultAddForm view.
>>> form_view
<plone.dexterity.browser.add.DefaultAddView object at 0x10cbf0950>
# Get the form from the instance
>>> form_view.form
<class 'plone.dexterity.browser.add.DefaultAddForm'>
# Than you can get all handlers
>>> form_view.form.handlers
<Handlers [<Handler for <Button 'save' u'Save'>>, <Handler for <Button 'cancel' u'Cancel'>>]
# and all buttons
form_view.form.buttons.items()
[('save', <Button 'save' u'Save'>), ('cancel', <Button 'cancel' u'Cancel'>)]
# In _handlers you can see the buttons, with the corresponding handlers
form_view.form.handlers._handlers
((<Button 'save' u'Save'>, <Handler for <Button 'save' u'Save'>>), (<Button 'cancel' u'Cancel'>, Handler for <Button 'cancel' u'Cancel'>>))
# You can also get the handler by button
>>> save_button = form_view.form.buttons.items()[0]
>>> save_handler = form_view.form.handlers.getHandler(save_button)
<Handler for <Button 'save' u'Save'>>
# Once you have your handler, you can call it directly
save_handler.func(form_view.form_instance, save_button)
這取決於你在做什麼,如果你必須設置一點點,讓你的測試工作。 你沒有給我們足夠的關於你在做什麼的信息。
這是取自z3c.form文檔: 我沒有爲自己運行此代碼。
# You can test your actions also this, probably more readable :-)
from z3c.form.testing import TestRequest
from z3c.form import button
>>> request = TestRequest(form={'form.buttons.save': 'Save'})
>>> actions = button.ButtonActions(form_view.form_instance, request, None)
>>> actions.update()
>>> actions.execute()
# This executes your Save actions.
這正是我在找的東西,但是鏈接到的z3c.form文檔實際上可能更合適。非常好的答案,謝謝 – Esoth 2014-10-28 21:09:35