[Grok-dev] Re: Displaying an image stored on a grok.Model

Philipp von Weitershausen philipp at weitershausen.de
Mon Jan 21 13:01:37 EST 2008


Philipp von Weitershausen wrote:
> Dirceu Pereira Tiegs wrote:
>> Hey,
>>
>> I'm using z3c.widget.image with the following code:
>>
>> import grok
>> from zope import interface, schema
>> from zope.app.file.interfaces import IImage
>> from z3c.widget.image.widget import ImageWidget
>>
>> class z3cwidget_test(grok.Application, grok.Container):
>>     pass
>>
>> class IEvent(interface.Interface):
>>     title = schema.TextLine(title=u"Title")
>>     image = schema.Object(title=u"Image", schema=IImage, required=False)
>>
>> class Event(grok.Model):
>>     interface.implements(IEvent)
>>
>>     def __init__(self, title, image=None):
>>         super(Event, self).__init__()
>>         self.title = title
>>         self.image = image
>>
>> class AddEvent(grok.AddForm):
>>     form_fields = grok.AutoFields(Event)
>>     form_fields['image'].custom_widget = ImageWidget
>>
>>     @grok.action('Add event')
>>     def add(self, **data):
>>         obj = Event(**data)
>>         name = data['title'].lower().replace(' ', '_')
>>         self.context[name] = obj
>>
>> class Index(grok.DisplayForm):
>>     grok.context(Event)
>>
>> It works, but I can't figure out how to show the image on a page
>> template. Any hints?
> 
> You have two choices:
> 
...

I forgot to mention that in both cases, you'll be able to access the 
image data via  http://.../some_event/image. In solution a), this URL 
will resolve to the ShowEventImage view. In solution b), this URL 
resolves to the actual IImage object for which there's a default view 
that will display it.

Concering security, you'd probably want to do solution a) because you're 
  exposing as few data objects as possible.


More information about the Grok-dev mailing list