<div class="gmail_quote"><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;">Date: Tue, 27 Jul 2010 12:15:15 +0200<br>
From: Lumir Jasiok &lt;<a href="mailto:lumir.jasiok@vsb.cz">lumir.jasiok@vsb.cz</a>&gt;<br>
Subject: [Grok-dev] Whats wrong wit my update method<br>
To: <a href="mailto:grok-dev@zope.org">grok-dev@zope.org</a><br>
Message-ID: &lt;<a href="mailto:4C4EB1B3.5030203@vsb.cz">4C4EB1B3.5030203@vsb.cz</a>&gt;<br>
Content-Type: text/plain; charset=&quot;utf-8&quot;<br>
<br>
  Hi,<br>
<br>
I am pretty new in Grok Development, so sorry for (probably) trivia<br>
question. I have small testing application in app.py<br>
<br>
import grok<br>
<br>
class Awbarcode(grok.Application,grok.Container):<br>
<br>
     def __init__(self):<br>
         super(Awbarcode, self).__init__()<br>
         self.title = &#39;AWBarcode manager&#39;<br>
         self.text = &quot;basic text&quot;<br>
         self.next_id = 0<br>
<br>
     def addBarcode(self,text):<br>
         id = str(self.next_id)<br>
         self.next_id = self.next_id + 1<br>
         self[id] = Barcode(text)<br>
<br>
     def deleteBarcode(self,barode):<br>
         del self[barcode]<br>
<br>
class Barcode(grok.Model):<br>
<br>
     def __init__(self,text):<br>
         super(Barcode,self).__init__()<br>
         self.text = text<br>
<br>
class AwbarcodeIndex(grok.View):<br>
     grok.context(Awbarcode)<br>
     <a href="http://grok.name" target="_blank">grok.name</a>(&#39;index&#39;)<br>
<br>
class AddBarcode(grok.View):<br>
     grok.context(Awbarcode)<br>
     <a href="http://grok.name" target="_blank">grok.name</a>(&#39;addbarcode&#39;)<br>
<br>
     def update(self,text):<br>
         self.context.addBarcode(text)<br>
         self.redirect(self.url(&#39;index&#39;))<br>
<br>
Also I have two .pt files, <a href="http://awbarcodeindex.pt" target="_blank">awbarcodeindex.pt</a>:<br>
<br>
&lt;html&gt;<br>
&lt;head&gt;<br>
&lt;title tal:content=&quot;context/title&quot;&gt;Barcode manager&lt;/title&gt;<br>
&lt;/head&gt;<br>
&lt;body&gt;<br>
&lt;h1 tal:content=&quot;context/title&quot;&gt;Barcode manager&lt;/h1&gt;<br>
&lt;h2&gt;Here you can see barcodes:&lt;/h2&gt;<br>
&lt;tal:block repeat=&quot;barcode context/values&quot;&gt;<br>
&lt;ul&gt;<br>
&lt;li tal:content=&quot;barcode/text&quot;&gt;Barcode&lt;/li&gt;<br>
&lt;/ul&gt;<br>
&lt;/tal:block&gt;<br>
&lt;/body&gt;<br>
&lt;/html&gt;<br>
<br>
and <a href="http://addbarcode.pt" target="_blank">addbarcode.pt</a>:<br>
<br>
&lt;html&gt;<br>
&lt;body&gt;<br>
&lt;h2&gt;Add barcode&lt;/h2&gt;<br>
&lt;form tal:attributes=&quot;action view/url&quot;  method=&quot;POST&quot;&gt;<br>
             New Barcode text: &lt;input type=&quot;text&quot; name=&quot;text&quot; value=&quot;&quot;<br>
/&gt;&lt;br /&gt;<br>
&lt;input type=&quot;submit&quot; value=&quot;Add Barcode Text&quot; /&gt;<br>
&lt;/form&gt;<br>
&lt;/body&gt;<br>
&lt;/html&gt;<br>
<br>
So, if I manually add some barcode text, I can see it listed on the<br>
index page, but when I try to add new barcode text using url:<br>
<br>
<a href="http://localhost:8080/test/addbarcode" target="_blank">http://localhost:8080/test/addbarcode</a><br>
<br>
I can see just &quot;A system error occurred.&quot;, not form I have in<br>
<a href="http://addbarcode.pt" target="_blank">addbarcode.pt</a>  and in log file is &quot;TypeError: Missing argument to<br>
update(): text&quot;. I must use this to add new text to the storage:<br>
<br>
<a href="http://localhost:8080/test/addbarcode?text=text1" target="_blank">http://localhost:8080/test/addbarcode?text=text1</a><br>
<br>
After that I can finally see form and add new barcode text.<br>
<br>
What I am doing wrong? Thanks for any help.<br></blockquote><div><br></div><div>The &#39;update&#39; method runs before the template is displayed. See <a href="http://grok.zope.org/doc/current/tutorial.html#doing-some-calculation-before-viewing-a-page">http://grok.zope.org/doc/current/tutorial.html#doing-some-calculation-before-viewing-a-page</a></div>
<div><br></div><div>When you say &quot;/test/addbarcode&quot; you are going to the &#39;AddBarcode&#39; view, which uses its &#39;update&#39; method first, which demands the data it needs. If it fails, it fails, if it succeeds, you see  the &#39;<a href="http://addbarcode.pt">addbarcode.pt</a>&#39; template.</div>
<div><br></div><div>You could create another view for the form and have it submit to &#39;addbarcode&#39;, which then becomes a &quot;thank you&quot; page. Or, perhaps, you could not require &#39;text&#39; 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 <a href="http://grok.zope.org/doc/current/tutorial.html#simple-forms">http://grok.zope.org/doc/current/tutorial.html#simple-forms</a><br>
</div></div><div><br></div><div>As already mentioned, though, it would probably be a good idea to look into automatic forms.</div><br clear="all">                    --jcc<br>-- <br>J Cameron Cooper<br><a href="mailto:jccooper@gmail.com">jccooper@gmail.com</a><br>
<a href="mailto:anything@jcameroncooper.com">anything@jcameroncooper.com</a><br>mobile: 713.882.7395<br>Skype: jcameroncooper<br>