[Zope3-Users] Getting view result from Python

Marius Gedminas mgedmin at b4net.lt
Wed Jun 27 13:17:19 EDT 2007


On Wed, Jun 27, 2007 at 04:59:07PM +0200, Thierry Florac wrote:
> Le mercredi 27 juin 2007 à 14:31 +0200, Christophe Combelles a écrit :
> > Thierry Florac a écrit :
> > > I'd like to get a view output from some Python code.
> > > 
> > > Actually, I don't have any problem if I use a page template with a
> > > simple <tal:x content="structure context/@@viewname" /> expression and
> > > return "self.template()" in Python.
> > > But it should certainly be "cleaner" and less expensive to get view
> > > output directly from Python code, which I don't actually manage to do...
> > 
> > It depends on what you call a view,
> > if this is a BrowserPage, you have a __call__ method that returns the output.
> > so just call your view().
> > If this is a BrowserView, there is no __call__ because a BrowserView is not 
> > intended to be published as is. But you can have any method that returns HTML.

If you use getMultiAdapter to get the view, all these differences are
hidden and you can call the view to get its output.

> > If this is a Viewlet or a ContentProvider, there is a render method that returns 
> > the output. (provided you first call the update method)

Then it's not really a view, is it?  ;-)

> In fact it's a little bit more complicated...
> What I have at first is a BrowserPage view, which is generally called
> from a page template with "context/@@viewname" syntax, without any
> problem.
> 
> But I need also to be able to get the same view output from a JSON call,
> so that I can update a little bit of HTML code dynamically. I don't have
> any problem when defining a template with the same syntax. But if I call
> in python :
> 
>   view = zapi.getMultiAdapter((myContext,myRequest), Interface,
>                               name=u'viewname')

This should work.  You don't have to pass Interface explicitly, by the
way, view = getMultiAdapter((context, request), name='...') will work as
well.

> I receive an error message :
> 
>   __init__() takes at least 3 arguments (1 given)

I don't believe you :-)

Can you show the full traceback?  getMultiAdapter definitely passes the
context tuple to the adapter's __init__.  There must be some other piece
of code that fails for you.

> So zapi.getMultiAdapter creates the view, but how can I initialize it
> with it's required parameters (context and request) ?

getMultiAdapter does that.  I speak from personal experience.

Marius Gedminas
-- 
"...the only place for 63,000 bugs is a rain forest"
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 189 bytes
Desc: Digital signature
Url : http://mail.zope.org/pipermail/zope3-users/attachments/20070627/447099be/attachment.bin


More information about the Zope3-users mailing list