Thanks Yarko.
Note I have no issues run the examples in the book. For example it
works if I open http://127.0.0.1/images/default/show/1
it works great.
the problem is the example seems not quite robust, if I point the
browser to htttp://127.0.0.1:8000/images/default/show
It gives me an error.
Ticket 127.0.0.1.2010-04-27.11-16-18.9e6c05a6-6ac6-446d-9e59-
f1c8b209343d
Error traceback
Traceback (most recent call last):
File "gluon/restricted.py", line 178, in restricted
File "C:/Documents and Settings/frank.zhu/My Documents/Downloads/
web2py/applications/images/controllers/default.py", line 60, in
<module>
File "gluon/globals.py", line 96, in <lambda>
File "C:/Documents and Settings/frank.zhu/My Documents/Downloads/
web2py/applications/images/controllers/default.py", line 16, in show
File "gluon/sql.py", line 3438, in __getitem__
IndexError: list index out of range
In file: C:\Documents and Settings\frank.zhu\My Documents\Downloads
\web2py\applications\images/controllers/default.py
# -*- coding: utf-8 -*-
#########################################################################
## This is a samples controller
## - index is the default action of any application
## - user is required for authentication and authorization
## - download is for downloading files uploaded in the db (does
streaming)
## - call exposes all registered services (none by default)
#########################################################################
def index():
images = db().select(db.image.ALL, orderby=db.image.title)
return dict(images=images)
def show():
image = db(db.image.id==request.args(0)).select()[0]
if len(image) == 0: print "No images found!"
form = SQLFORM(db.comment)
form.vars.image_id = image.id
if form.accepts(request.vars, session):
response.flash = 'your comment is posted'
comments = db(db.comment.image_id==image.id).select()
return dict(image=image, comments=comments, form=form)
I think now I know why, the request.args(0) doesn't contain any image
id,
so the following is one way to get around this issue
def show():
id = request.args(0) or 1 # id default to 1
image = db(db.image.id==id).select()[0]
form = SQLFORM(db.comment)
form.vars.image_id = image.id
if form.accepts(request.vars, session):
response.flash = 'your comment is posted'
comments = db(db.comment.image_id==image.id).select()
return dict(image=image, comments=comments, form=form)
On Apr 26, 6:55 pm, Yarko Tymciurak <[email protected]>
wrote:
> On Apr 26, 4:38 pm, frankz <[email protected]> wrote:
>
> > Hi,
> > in the web2py official book, in the image blog example,
>
> Referring tohttp://web2py.com/book/default/section/3/6?search=image+blog
>
> > Could anyone help explain what the following line mean?
> > image = db(db.image.id==request.args(0)).select()[0]
>
> This example code assume's you've entered an image in the database; if
> you have not,
> then the ....select()[0] reference would give you index out of
> range.
>
> Here's why:
>
> - This is a database select query; it's form is basically:
>
> your_db_connection( WHERE clause; in this case where ID of the image
> table is same as what requested).select( fields - "ALL" by default)
>
> This returns an array of records found in the database - if none, it
> returns an empty array, [].
>
> A more robust way to write this (not used, so the example would be
> short) would be something like this:
>
> images = db(db.image.id==request.args(0)).select()
> if len(images) == 0: print "No images found!"
> else: image = images[0]
>
> I hope this has been helpful.
> Regards,
> - Yarko
>
>
>
> > I duplicate the same code but I find URL
> > htttp://127.0.0.1:8000/images/default/show
> > gives me an error, "index out of range"
>
> > How do I fix it?
>
> > Thanks,
>
> > --
> > Subscription settings:http://groups.google.com/group/web2py/subscribe?hl=en