<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 <<a href="mailto:lumir.jasiok@vsb.cz">lumir.jasiok@vsb.cz</a>><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: <<a href="mailto:4C4EB1B3.5030203@vsb.cz">4C4EB1B3.5030203@vsb.cz</a>><br>
Content-Type: text/plain; charset="utf-8"<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 = 'AWBarcode manager'<br>
self.text = "basic text"<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>('index')<br>
<br>
class AddBarcode(grok.View):<br>
grok.context(Awbarcode)<br>
<a href="http://grok.name" target="_blank">grok.name</a>('addbarcode')<br>
<br>
def update(self,text):<br>
self.context.addBarcode(text)<br>
self.redirect(self.url('index'))<br>
<br>
Also I have two .pt files, <a href="http://awbarcodeindex.pt" target="_blank">awbarcodeindex.pt</a>:<br>
<br>
<html><br>
<head><br>
<title tal:content="context/title">Barcode manager</title><br>
</head><br>
<body><br>
<h1 tal:content="context/title">Barcode manager</h1><br>
<h2>Here you can see barcodes:</h2><br>
<tal:block repeat="barcode context/values"><br>
<ul><br>
<li tal:content="barcode/text">Barcode</li><br>
</ul><br>
</tal:block><br>
</body><br>
</html><br>
<br>
and <a href="http://addbarcode.pt" target="_blank">addbarcode.pt</a>:<br>
<br>
<html><br>
<body><br>
<h2>Add barcode</h2><br>
<form tal:attributes="action view/url" method="POST"><br>
New Barcode text: <input type="text" name="text" value=""<br>
/><br /><br>
<input type="submit" value="Add Barcode Text" /><br>
</form><br>
</body><br>
</html><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 "A system error occurred.", not form I have in<br>
<a href="http://addbarcode.pt" target="_blank">addbarcode.pt</a> and in log file is "TypeError: Missing argument to<br>
update(): text". 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 'update' 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 "/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 '<a href="http://addbarcode.pt">addbarcode.pt</a>' template.</div>
<div><br></div><div>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 <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>