le 11.04.2009 23:31 Hanjie a écrit: > Anybody can help me! > I have been driven mad by this error > > On Sat, Apr 11, 2009 at 7:25 PM, Hanjie <[email protected] > <mailto:[email protected]>> wrote: > > I have not solve this problem, please help me~~ > It is already two days~~ > > when I upload the file, the method "add" get the parameter"image", > I guess the "image" is the data of uploaded file. I try to write > the data to disk.(follow the wiki), > I tried like this "data = image.file.read()", from wiki I know the > TG should treat it as file-object. But, It doesn't work. I get the > traceback : > > data = image.file.read() > AttributeError: 'dict' object has no attribute 'file' > > -------------------following: detail---------------------------------- > > I upload the image. And I use widget > "file_fields.widgets.ImageField" to create a form_table class. > like following: > -------------------------------------------------------------------- > class AddFields(widgets.WidgetsList): > title = widgets.TextField( > label='Title', > validator=validators.NotEmpty(), > ) > image = file_fields.widgets.ImageField( > validator=validators.NotEmpty(), > ) > content = widgets.TextArea( > validator=validators.NotEmpty(), > attrs=dict(rows=10, cols=45) > ) > add_form = widgets.TableForm( > fields=AddFields(), > name="w_addform", > action="/add", > submit_text="Add new!" > ) > ----------------------------------------------------------- > > In the control.py, I create the method: > > @expose() > @error_handler(index) > def add(self,title,content,image,*args, **kw): > p = model.postTable.get(int(post_id)) > data = image.file.read() > newPost=model.postTable( > title = title, > content = content, > ) > # open file in binary mode for writing > targetFileName = os.path.join( > os.getcwd(), > UPLOAD_DIR, > "image.filename" > ) > f = open(targetFileName, 'wb') > f.write(data) > f.close() > tg.flash( > "File uploaded successfully:"%s saved as: %s" \ > % (image.filename, targetFileName) > ) > newUploadFile = model.uploadedFile( > filename=image.filename, > abspath=targetFileName, > size=0, > referenced_in_posts = p, > ) > raise redirect("/") > > > ---------------------------model.py----------------------------- > class postTable(SQLObject): > title = UnicodeCol(length=50) > content = UnicodeCol() > postDate = DateTimeCol(default=datetime.now()) > isPublished= BoolCol(default=False) > comments=MultipleJoin('commentTable') > pictures=MultipleJoin('uploadedFile') > > class uploadedFile(SQLObject): > filename = UnicodeCol(alternateID=True, length=100) > abspath = UnicodeCol() > size = IntCol() > referenced_in_posts = ForeignKey('postTable') > > --------------------traceback---------------------------------- > > Page handler: <bound method Root.add of <assignment.controllers.Root > object at 0x019FA4B0>> > > > Traceback (most recent call last): > File > "c:\python25\lib\site-packages\cherrypy-2.3.0-py2.5.egg\cherrypy\_cphttptools.py", > line 121, in _run > self.main() > File > "c:\python25\lib\site-packages\cherrypy-2.3.0-py2.5.egg\cherrypy\_cphttptools.py", > line 264, in main > > > body = page_handler(*virtual_path, **self.params) > File "<string>", line 3, in add > File > "c:\python25\lib\site-packages\TurboGears-1.0.8-py2.5.egg\turbogears\controllers.py", > line 360, in expose > > > *args, **kw) > File "<string>", line 5, in run_with_transaction > File > "c:\python25\lib\site-packages\TurboGears-1.0.8-py2.5.egg\turbogears\database.py", > line 359, in so_rwt > retval = func(*args, **kw) > > > File "<string>", line 5, in _expose > File > "c:\python25\lib\site-packages\TurboGears-1.0.8-py2.5.egg\turbogears\controllers.py", > line 373, in <lambda> > mapping, fragment, args, kw))) > > > File > "c:\python25\lib\site-packages\TurboGears-1.0.8-py2.5.egg\turbogears\controllers.py", > line 410, in _execute_func > output = errorhandling.try_call(func, *args, **kw) > File > "c:\python25\lib\site-packages\TurboGears-1.0.8-py2.5.egg\turbogears\errorhandling.py", > line 77, in try_call > > > return func(self, *args, **kw) > File "C:\Documents and > Settings\hanjie\assignment\assignment\controllers.py", line 36, in add > data = image.file.read() > AttributeError: 'dict' object has no attribute 'file' > > 2009/4/9 Florent Aide <[email protected] > <mailto:[email protected]>> > > > On Thu, Apr 9, 2009 at 10:55 AM, Diez B. Roggisch > <[email protected] <mailto:[email protected]>> wrote: > > > > On Thursday 09 April 2009 10:38:21 跑步鱼 wrote: > >> Why does it happen > >> I followed the introduction from wiki~ > >> Anybody can help me? > > > > Please show us what the video-object actually is, by > printing out it's repr or > > something like that. > > from the traceback it seems to be a dict. Maybe you're missing an > @validate on your exposed save method? > > Florent. > > > > As Florent said before, you first need to @validate your form in your "add" method :
something like : @expose() @error_handler(index) @validate(form=add_form) def add(self,title,content,image,*args, **kw): And then, you should have a look to what "image" looks like... "print image" could help... Also, you can have a look to the ImageField widget source to see what it is suppsed to give you back...... --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "TurboGears" group. To post to this group, send email to [email protected] To unsubscribe from this group, send email to [email protected] For more options, visit this group at http://groups.google.com/group/turbogears?hl=en -~----------~----~----~----~------~----~------~--~---

