Two problems:
1. You are using partnum as a GET variable, but it is also the name of
one of the form fields, so when you submit the form, there is a "partnum"
in both the GET and the POST variables. When the same variable appears in
both the GET and POST variables, web2py puts them in a list in request.vars
-- so, in this case, upon form submission, request.vars.partnum is
['12345', '12345'] instead of just '12345'. To avoid that problem, you
should refer specifically to request.get_vars.partnum instead of
request.vars.partnum.
2. Issue #1 caused your query to return no results, but you don't have
any logic to check for no results (which would be a problem even without
issue #1), which is why you got an error ticket.
Your code also includes some unnecessary checks and variable assignments.
Here's a much simpler version of the code:
def update_inventory():
if request.get_vars.partnum:
row = db(db.invent.partnum == request.get_vars.partnum).select().
first()
return dict(record=crud.update(db.invent, row.id) if row
else 'Part #%s not found' % request.get_vars.partnum)
else:
return dict(record="No part number. URL should read
update_inventory?part=12345")
Note, I removed the check for duplicates:
# check to make sure there are no double entries in database
if(len(rows) > 1):
record = "Database Error"
return dict(record=record)
That shouldn't be necessary, because in your model definition, you have:
Field('partnum', unique=True),
Setting unique=True tells the database not to allow duplicates, so if you
try to enter a duplicate, you'll get an operational error from the
database. However, getting an error from the database isn't a friendly user
experience, so in addition to setting unique=True, you should also add an
IS_NOT_IN_DB
validator<http://web2py.com/books/default/chapter/29/7#Database-validators>to
the field -- the validator will be enforced at the form level and will
result in a friendly error message being displayed on the form when a
duplicate value is submitted:
Field('partnum', unique=True,
requires=IS_NOT_IN_DB(db, 'invent.partnum')),
Anthony
On Thursday, June 7, 2012 3:33:51 PM UTC-4, Joel Robinson wrote:
>
> Here's the code in a web2py module. You'll have to enter a few things in
> the database with the
> enter_inventory page.
>
> To see the problem enter, for example, part number 12345 and a quantity of
> 3 on the enter_inventory page.
> Once that is entered you can enter the following URL:
> http://localhost:8000/GTV/default/update_inventory?partnum=12345
>
> the page will initially come up correctly. But if you change the quantity
> from 3 to 2, for example, and then
> hit the submit button, you will get an error.
>
> Thanks again for you help with this
>
> Joel Robinson
>
>
> On Thursday, June 7, 2012 12:38:46 PM UTC-5, Anthony wrote:
>>
>> Yeah, I think you'll have to show more code.
>>
>> On Thursday, June 7, 2012 12:58:45 PM UTC-4, Joel Robinson wrote:
>>>
>>> Thanks for the reply. I tried you sugestion but still no joy. I haven't
>>> included all the code
>>> so that the examples as simple as possible. I can try to attach some of
>>> my code and send it
>>> to you if you wish.
>>>
>>> If you add some code such as:
>>> else :
>>> record = 'No part number URL should read
>>> "update_inventory?partnum=something"'
>>> return dict(record=record)
>>>
>>> then it returns that message when you submit an update, but the database
>>> is still not being updated.
>>>
>>>
>>> On Thursday, June 7, 2012 11:11:41 AM UTC-5, Anthony wrote:
>>>>
>>>> What are you returning in case no rows are found? Also, you can instead
>>>> do:
>>>>
>>>> row = db(db.invent.partnum == request.vars.partnum).select().first()
>>>> if row:
>>>> etc.
>>>>
>>>> .first() returns the first row or None if there are no records.
>>>>
>>>> Anthony
>>>>
>>>> On Thursday, June 7, 2012 10:49:54 AM UTC-4, Joel Robinson wrote:
>>>>>
>>>>> Thanks for the reply, but its still not working correctly. I changed
>>>>> the code to the following:
>>>>>
>>>>> if rows:
>>>>> row = rows[0]
>>>>> record = crud.update(db.invent, row.id )
>>>>> return dict(record=record)
>>>>>
>>>>> with a URL like:
>>>>>
>>>>> http://localhost:8000/GeorgesTV/qrinv/update_inventory?partnum=LAMP-4
>>>>>
>>>>> I will get the correct web page, but when I change a value and hit the
>>>>> submit button I just get sent to
>>>>> a page that says "None". If I reload the page the values are
>>>>> unchanged. Nothing was updated.
>>>>>
>>>>> Joel Robinson
>>>>>
>>>>> On Thursday, June 7, 2012 9:33:57 AM UTC-5, Anthony wrote:
>>>>>>
>>>>>> Same problem -- everything has to go inside your "if rows:" block --
>>>>>> don't attempt to create a crud.update form if no rows have been returned.
>>>>>>
>>>>>> Anthony
>>>>>>
>>>>>> On Thursday, June 7, 2012 10:30:17 AM UTC-4, Joel Robinson wrote:
>>>>>>>
>>>>>>> Thanks for the reply. I tried your sugestion but am still getting an
>>>>>>> error, though a different one:
>>>>>>>
>>>>>>> Traceback (most recent call last):
>>>>>>> File "/home/joel/Source/web2py/gluon/restricted.py", line 205, in
>>>>>>> restricted
>>>>>>> exec ccode in environment
>>>>>>> File
>>>>>>> "/home/joel/Source/web2py/applications/GeorgesTV/controllers/qrinv.py"
>>>>>>> <http://127.0.0.1:8000/admin/default/edit/GeorgesTV/controllers/qrinv.py>,
>>>>>>> line 136, in <module>
>>>>>>> File "/home/joel/Source/web2py/gluon/globals.py", line 173, in
>>>>>>> <lambda>
>>>>>>> self._caller = lambda f: f()
>>>>>>> File
>>>>>>> "/home/joel/Source/web2py/applications/GeorgesTV/controllers/qrinv.py"
>>>>>>> <http://127.0.0.1:8000/admin/default/edit/GeorgesTV/controllers/qrinv.py>,
>>>>>>> line 131, in update_inventory
>>>>>>> record = crud.update(db.invent, row.id )
>>>>>>> UnboundLocalError: local variable 'row' referenced before assignment
>>>>>>>
>>>>>>> Just to be clear the page is coming up initialy. This error only
>>>>>>> happens when I click
>>>>>>> on the submit button. Thanks again for your help.
>>>>>>>
>>>>>>> Joel Robinson
>>>>>>>
>>>>>>>
>>>>>>>
>>>>>>> On Wednesday, June 6, 2012 4:14:29 PM UTC-5, Massimo Di Pierro wrote:
>>>>>>>>
>>>>>>>> Looks like the error is at
>>>>>>>>
>>>>>>>> row = rows[0]
>>>>>>>>
>>>>>>>> because rows is empty (no records matching query). You need a check
>>>>>>>> like
>>>>>>>>
>>>>>>>> if rows:
>>>>>>>> row = rows[0]
>>>>>>>>
>>>>>>>>
>>>>>>>>
>>>>>>>> On Wednesday, 6 June 2012 12:19:14 UTC-5, Joel Robinson wrote:
>>>>>>>>>
>>>>>>>>> First, let me give some background on what I've been trying to do.
>>>>>>>>> We wanted to set up an inventory system using QR codes which would be
>>>>>>>>> put
>>>>>>>>> on stickers on our stock and then scaned with smartphone when
>>>>>>>>> removing
>>>>>>>>> inventory bring up the appropiate web page. The QR code should
>>>>>>>>> contain
>>>>>>>>> something like:
>>>>>>>>>
>>>>>>>>> http://GeorgesTV/qrinv/update_inventory?partnum=12345
>>>>>>>>>
>>>>>>>>> and the following is the code I wrote for update_inventory:
>>>>>>>>>
>>>>>>>>> rows = db(db.invent.partnum == request.vars.partnum).select()
>>>>>>>>> # change rows object to row object
>>>>>>>>> row = rows[0]
>>>>>>>>>
>>>>>>>>> # display queried object
>>>>>>>>> record = crud.update(db.invent, row.id)
>>>>>>>>> return dict(record=record)
>>>>>>>>>
>>>>>>>>> initialy it would work fine it would run the query and pull up the
>>>>>>>>> correct record but
>>>>>>>>> if I tried to update the record I would get the folling error:
>>>>>>>>>
>>>>>>>>>
>>>>>>>>> Traceback (most recent call last):
>>>>>>>>> File "/home/joel/Source/web2py/gluon/restricted.py", line 205, in
>>>>>>>>> restricted
>>>>>>>>> exec ccode in environment
>>>>>>>>> File
>>>>>>>>> "/home/joel/Source/web2py/applications/GeorgesTV/controllers/qrinv.py"
>>>>>>>>>
>>>>>>>>> <http://127.0.0.1:8000/admin/default/edit/GeorgesTV/controllers/qrinv.py>,
>>>>>>>>> line 123, in <module>
>>>>>>>>> File "/home/joel/Source/web2py/gluon/globals.py", line 173, in
>>>>>>>>> <lambda>
>>>>>>>>> self._caller = lambda f: f()
>>>>>>>>> File
>>>>>>>>> "/home/joel/Source/web2py/applications/GeorgesTV/controllers/qrinv.py"
>>>>>>>>>
>>>>>>>>> <http://127.0.0.1:8000/admin/default/edit/GeorgesTV/controllers/qrinv.py>,
>>>>>>>>> line 116, in update_inventory
>>>>>>>>> row = rows[0]
>>>>>>>>> File "/home/joel/Source/web2py/gluon/dal.py", line 7755, in
>>>>>>>>> __getitem__
>>>>>>>>> row = self.records[i]
>>>>>>>>> IndexError: list index out of range
>>>>>>>>>
>>>>>>>>> I thought maybe the request variable wasn't getting passed correctly
>>>>>>>>> when updated so I made the following change:
>>>>>>>>>
>>>>>>>>> session.partnum = request.vars.partnum
>>>>>>>>> rows = db(db.invent.partnum == session.partnum).select()
>>>>>>>>>
>>>>>>>>> which didn't help either, but I then noticed that if I tried to pull
>>>>>>>>> up the update_inventory page now without the query string
>>>>>>>>> it would come up since session varible was already set and it would
>>>>>>>>> update correctly!
>>>>>>>>>
>>>>>>>>> I was eventualy able to get it work more or less how I wanted by
>>>>>>>>> writing a page whose only function was to take the
>>>>>>>>> query string set a session variable and then redirect you to the
>>>>>>>>> update_inventory page. But I worry that using session varibles
>>>>>>>>> may cause unforseen problems eventualy.
>>>>>>>>>
>>>>>>>>> So what I would like to know, is this a bug I've found or am I going
>>>>>>>>> about doing this incorrectly? Could someone explains
>>>>>>>>> what happens when you use crud.update and why I get this error when I
>>>>>>>>> try to use query strings?
>>>>>>>>>
>>>>>>>>> Thanks in advance for your help.
>>>>>>>>>
>>>>>>>>> Joel Robinson
>>>>>>>>>
>>>>>>>>>