[Zope3-Users] Problem with @@absolute_url

Maciej Wisniowski maciej.wisniowski at coig.katowice.pl
Mon Apr 30 16:23:16 EDT 2007


> class StudentDetails:
>     """Helper to return a student photo."""
>     def getPhoto(self):
>         student = self.context
>         return StudentPhoto(student)
> 
> In my page template, I can see that the IImage object is being created 
> correctly..
> 
> <div tal:define="photo view/getPhoto">
>     <span tal:content="photo">#</span>
> </div>
> 
> gives me <zope.app.file.image.Image object at 0xb75c6c6c>.  But to turn this 
> object into a URL so that I can put it in an <img> tag is beyond me.  When I 
> try to do an @@absolute_url on the photo object I get the error saying 
> there's not enough context.
> 
> Am I going about this the wrong way?  Is there some other thing I need to be 
> doing in order for there to be enough context?
Hm... not sure if it is good solution but it is just a quick though so
possibly somebody may give you something better. Maybe there are ready
solutions for this. In general if you want
to return image then you have to return it's data to the browser (not
Image object) and you also have to set proper headers like content-type
and content-length. I don't remember exactly how it should go but you
should find examples with google or see how z3c.image does this etc.
This should be something like:


class StudentDetails:
     """Helper to return a student photo."""
     def __call__(self):
         student = self.context
         self.request.setHeader('content-type', 'image/png')
         # determine and set content-length here
         return StudentPhoto(student).data # not sure if it was 'data'
                                           # check image.py

If you declare this in zcml as:

<browser:view
   name='myphoto'
   class=StudentDetails'
   permission='zope.Public'
   for=.....

then you may use url just like:

student_object/myphoto

and this will execute __call__ method of the view class.

-- 
Maciej Wisniowski



More information about the Zope3-users mailing list