URL parsing in controller

54 views
Skip to first unread message

Jeff Riley

unread,
Mar 18, 2016, 4:01:51 PM3/18/16
to web2py-users
Hello All,

I am pulling my hair out.  I am trying to redirect the view button on a smartgrid.  I can get the button to redirect to the view I want, but I cannot parse the URL correctly to get the correct sheet id.

Here is my controller

@auth.requires_membership('manager')
def manage_customers():
    if 'new' in request.args:
        if request.args(2) == 'customer':
            redirect(URL('new_customer'))
        elif request.args(4) == 'sheet':
            redirect(URL('new_sheet', args=request.args(2)))
    if 'edit' in request.args:
        if request.args(4) == 'sheet':
            redirect(URL('edit_sheet', args=request.args(2)))
    if 'view' in request.args:
        if request.args(4) == 'sheet':
            redirect(URL('view_sheet', args=request.args(2,5)))
        elif request.args(6) == 'sheet_archive':
            redirect(URL('view_sheet', args=request.args(7)))
    form = SQLFORM.smartgrid(db.customer, linked_tables=['address', 'sheet', 'sheet_archive'],
                             searchable= dict(customer=True, address=False, sheet=False),
                             editable= dict(customer=True, address=True, sheet=True, sheet_archive=False),
                             deletable= dict(customer=True, address=True, sheet=True, sheet_archive=False),
                             create=dict(customer=True, address=True, sheet=True, sheet_archive=False),
                             paginate=20, maxtextlength=60, fields=[db.customer.first_name,
                             db.customer.last_name, db.customer.phone, db.customer.email, db.customer.handed,
                             db.address.address1, db.address.address2, db.address.city, db.address.state_province,
                             db.address.postal_code, db.sheet.title, db.sheet.created_by, db.sheet.created_on,
                             db.sheet_archive.id, db.sheet_archive.title, db.sheet_archive.created_by,
                             db.sheet_archive.created_on],)
    return dict(form=form, request=request)

Here is my URL:

http://127.0.0.1:8000/manage_customers/customer/sheet.customer_id/1/edit/sheet/2?_signature=3b4a45778f06aec42421ef503cba968cd633269a


I can only get the customer_id and not the sheet #2.  So I am always displaying the first customer record and the first sheet record.  SO LOST.


webm...@trytha.com

unread,
Mar 18, 2016, 4:18:23 PM3/18/16
to web2py-users
Maybe something about "args=request.args(2,5)"?

You could try to test this:

args=[request.args(2),request.args(5)]

Dave S

unread,
Mar 18, 2016, 4:58:21 PM3/18/16
to web2py-users


On Friday, March 18, 2016 at 1:18:23 PM UTC-7, webm...@trytha.com wrote:
Maybe something about "args=request.args(2,5)"?


He's trying to do a slice, but that would be [2:5], innit?

 
You could try to test this:

args=[request.args(2),request.args(5)]



shouldn't those '(' and ')' be '[' and ']'  (list indexes)?

Also, (Jeff) please verify which part of the URL is request.args[0] .  It will be after the app/contoller/function code (and you're using routes.py to set default for the c/f part?);
I see "sheet" where I count 4, but it should be double-checked.

/dps

Jeff Riley

unread,
Mar 18, 2016, 5:14:31 PM3/18/16
to web...@googlegroups.com
DPS and all,

I have my routes.py set to the application (a).  As far as what is requestmaths(0), that is what is confusing.  That URL is generated by smartgrid.  Manage_customers is the controller, so customer should be the function but it is not. View is the function.  This is why I am having trouble understanding how to grab the customer id and the sheet id.

Thank you,
Jeff Riley
--
Resources:
- http://web2py.com
- http://web2py.com/book (Documentation)
- http://github.com/web2py/web2py (Source code)
- https://code.google.com/p/web2py/issues/list (Report Issues)
---
You received this message because you are subscribed to a topic in the Google Groups "web2py-users" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/web2py/VW9k-9LIEAs/unsubscribe.
To unsubscribe from this group and all its topics, send an email to web2py+un...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Message has been deleted

Jeff Riley

unread,
Mar 18, 2016, 5:18:40 PM3/18/16
to web2py-users
DPS and all,

I have my routes.py set to the application (a). As far as what is request.args(0), that is what is confusing. That URL is generated by smartgrid. Manage_customers is the controller, so customer should be the function but it is not. View is the function. This is why I am having trouble understanding how to grab the customer id and the sheet id.

Thank you,
Jeff Riley

Jeff Riley

unread,
Mar 18, 2016, 5:21:24 PM3/18/16
to web2py-users
Sorry, edit is the function. This URL does not call view.

Dave S

unread,
Mar 18, 2016, 5:55:42 PM3/18/16
to web2py-users


On Friday, March 18, 2016 at 2:21:24 PM UTC-7, Jeff Riley wrote:
Sorry, edit is the function.  This URL does not call view.

>> I have my routes.py set to the application (a).

and the default controller ....

>   As far as what is request.args(0), that is what is confusing.  
>> That URL is generated by smartgrid.  Manage_customers is the controller, so customer should be the function but it is not. View is the function.  
>> This is why I am having trouble understanding how to grab the customer id and the sheet id 

If your original code paste is accurate, the controller is the file you have that in, and manage_customers is the function
(to use the Web2Py terminology).

It appears that request.arg[0] is "customer", request.arg[1] is "sheet.customer_id", etc.

But if your paste is accurate, you have errors in python in accessing elements of a list.

/dps


Jeff Riley

unread,
Mar 18, 2016, 6:08:15 PM3/18/16
to web2py-users
Ah you are totally correct. The controller is default and manage_customers is the function. I am still getting used to these constructs.

Jeff Riley

unread,
Mar 18, 2016, 8:16:13 PM3/18/16
to web2py-users
Thank you all very much.  This worked. 

args=[request.args(2),request.args(5)]

Anthony

unread,
Mar 18, 2016, 8:27:14 PM3/18/16
to web2py-users
Rather than intercept the built-in view/edit links (which results in an unnecessary request due to the redirect), you might also consider removing them and instead use the "links" argument to create your own links/buttons for those functions.

Anthony

webm...@trytha.com

unread,
Mar 18, 2016, 8:28:44 PM3/18/16
to web2py-users
Booya!  I'm still learning web2py/python myself and that felt a lot like the type of thing I might have had trouble with.  ;)

Hit the "answered" button so anyone who searches for this in the future will see the solution.
Reply all
Reply to author
Forward
0 new messages