[Grok-dev] (no subject)

J. Cameron Cooper jccooper at gmail.com
Tue Jul 27 11:49:49 EDT 2010


>
> Date: Tue, 27 Jul 2010 12:15:15 +0200
> From: Lumir Jasiok <lumir.jasiok at vsb.cz>
> Subject: [Grok-dev] Whats wrong wit my update method
> To: grok-dev at zope.org
> Message-ID: <4C4EB1B3.5030203 at vsb.cz>
> Content-Type: text/plain; charset="utf-8"
>
>  Hi,
>
> I am pretty new in Grok Development, so sorry for (probably) trivia
> question. I have small testing application in app.py
>
> import grok
>
> class Awbarcode(grok.Application,grok.Container):
>
>     def __init__(self):
>         super(Awbarcode, self).__init__()
>         self.title = 'AWBarcode manager'
>         self.text = "basic text"
>         self.next_id = 0
>
>     def addBarcode(self,text):
>         id = str(self.next_id)
>         self.next_id = self.next_id + 1
>         self[id] = Barcode(text)
>
>     def deleteBarcode(self,barode):
>         del self[barcode]
>
> class Barcode(grok.Model):
>
>     def __init__(self,text):
>         super(Barcode,self).__init__()
>         self.text = text
>
> class AwbarcodeIndex(grok.View):
>     grok.context(Awbarcode)
>     grok.name('index')
>
> class AddBarcode(grok.View):
>     grok.context(Awbarcode)
>     grok.name('addbarcode')
>
>     def update(self,text):
>         self.context.addBarcode(text)
>         self.redirect(self.url('index'))
>
> Also I have two .pt files, awbarcodeindex.pt:
>
> <html>
> <head>
> <title tal:content="context/title">Barcode manager</title>
> </head>
> <body>
> <h1 tal:content="context/title">Barcode manager</h1>
> <h2>Here you can see barcodes:</h2>
> <tal:block repeat="barcode context/values">
> <ul>
> <li tal:content="barcode/text">Barcode</li>
> </ul>
> </tal:block>
> </body>
> </html>
>
> and addbarcode.pt:
>
> <html>
> <body>
> <h2>Add barcode</h2>
> <form tal:attributes="action view/url"  method="POST">
>             New Barcode text: <input type="text" name="text" value=""
> /><br />
> <input type="submit" value="Add Barcode Text" />
> </form>
> </body>
> </html>
>
> So, if I manually add some barcode text, I can see it listed on the
> index page, but when I try to add new barcode text using url:
>
> http://localhost:8080/test/addbarcode
>
> I can see just "A system error occurred.", not form I have in
> addbarcode.pt  and in log file is "TypeError: Missing argument to
> update(): text". I must use this to add new text to the storage:
>
> http://localhost:8080/test/addbarcode?text=text1
>
> After that I can finally see form and add new barcode text.
>
> What I am doing wrong? Thanks for any help.
>

The 'update' method runs before the template is displayed. See
http://grok.zope.org/doc/current/tutorial.html#doing-some-calculation-before-viewing-a-page

When you say "/test/addbarcode" you are going to the 'AddBarcode' view,
which uses its 'update' method first, which demands the data it needs. If it
fails, it fails, if it succeeds, you see  the 'addbarcode.pt' template.

You could create another view for the form and have it submit to
'addbarcode', which then becomes a "thank you" page. Or, perhaps, you could
not require 'text' in the method params (text=None), and look for the submit
param value and test for proper data (text is not empty/false) yourself
inside the method before changing the model. That would allow the form to
load without params and also submit to itself. This is (close to) what
happens in http://grok.zope.org/doc/current/tutorial.html#simple-forms

As already mentioned, though, it would probably be a good idea to look into
automatic forms.

                    --jcc
-- 
J Cameron Cooper
jccooper at gmail.com
anything at jcameroncooper.com
mobile: 713.882.7395
Skype: jcameroncooper
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.zope.org/pipermail/grok-dev/attachments/20100727/b174f4ac/attachment.html 


More information about the Grok-dev mailing list