Assuming your ajax callback function is correctly called you just need to
do:
redirect(location, client_side=True)
instead of
redirect(location)
With client_side = True you are handling the redirect client-side and not
server side. This is designed to work over ajax requests.
On Monday, 21 January 2013 12:58:03 UTC-6, c0nstin3 wrote:
>
> Hi everyone,
>
>
> Could someone please help me with a problem I'm having with the
> *amazing*web2py framework?
>
>
> This is hard to explain, so please bear with me, I'm still new to web
> development :)
>
>
> I seem to have a problem with using redirect in a function that is
> called via ajax. I thought I could use web2py_component method to call a
> method that returns a form (using SQLFORM or CRUD) and when the form is
> processed OK I could redirect to another method and have the content of
> that method returned instead.
>
>
> Everything is OK if I just add records, or edit records or delete
> records BUT when I edit or delete a record and then try to add a record
> something odd happens. Basically, when I delete a record I redirect back to
> the index page (to reload the grid). The grid is rendered OK BUT when a add
> a new record (using the form included in the index page) the WRONG METHOD
> is called. I can see from the debug that the delete method is called. I
> presume its all to do with me doing a redirect from within a .load file
> that has been rendered via web2py_component.
>
>
> I have page that I load a tabbed display into using ajax
> (web2py_component). The tabs are labelled Contacts, Documents, Widgets etc.
> When I click on the Contacts tab I use ajax to call contacts/index and
> display the what is returned into the tab-content div. Everything is OK at
> this point.
>
>
> @auth.requires_login()
>
> def index():
>
> company_id = request.args[0]
>
> crud.settings.hideerror=True
>
>
> db.contact.company.default = company_id
>
>
> grid = create_grid(db.contact.company == company_id, company_id)
>
> form = crud.create(db.contact, next=URL('contacts', 'index',
> args=company_id, user_signature=True))
>
> submit_btn = form.element(_type='submit')
>
> submit_btn['_class']='btn btn-small btn-success'
>
> submit_btn['_value']='Save'
>
> return locals()
>
>
> The create_grid function returns a grid and the links are like this:
>
>
> links = [
>
> lambda row: A('', _onclick="web2py_component('" + URL("contacts", "edit",
> args=[row.id, company_id], user_signature=True) + "', 'tabContent');",
> _class='icon-pencil'),
>
> lambda row: A('', _onclick="web2py_component('" + URL("contacts",
> "delete", args=[row.id, company_id], user_signature=True) + "',
> 'tabContent');", _class='icon-trash')
>
> ]
>
> If a click on a Delete link within the grid, this is what is called:
>
>
> @auth.requires_login()
>
> def delete():
>
> contact_id = request.args[0]
>
> company_id = request.args[1]
>
> crud.settings.hideerror=True
>
>
> contact = db.contact[contact_id] or redirect(error_page)
>
> crud.delete(db.contact, contact.id, next=URL('contacts', 'index',
> args=company_id, user_signature=True))
>
>
> The record is deleted OK and the index method is called OK and the grid
> is re-appears. When I try to add a new record then the delete method is
> called AGAIN rather than the index method. There is no problem adding
> records if I don't do a delete first!
>
>
> I don't understand what where I'm going wrong :( I suspect its to with
> using web2py_component method within a partial (.load) file that was also
> loaded via ajax and then trying to do a redirect from a method that was
> invoked via ajax.
>
>
> Kind Regards,
>
> Chris
>
>
>
--