return javascript alert from contorler without redirection

104 views
Skip to first unread message

Yebach

unread,
Feb 19, 2015, 3:15:09 AM2/19/15
to web...@googlegroups.com
Hello

I have a button for user to export some data from db. If he does not have the right privileges (that is checked on server) I would like to return an alert (possible modal or smth) without redirection after he clicks ok

So far I have an alert but user is redirected to an empty page afterwards


This is my button in html

<div class="btn-group" style="margin-bottom: 20px; margin-top: 10px;">
   <!--      <button onclick="" class="btn btn-primary"><span class="glyphicon glyphicon-export"></span>&nbsp;&nbsp;{{ =T('Export EXCEL') }}</button> -->
       {{=A(T('iCal export'), _class='btn btn-primary', _href=URL('script','iCalDownload',  args = request.args[0]))}}
   </div>

and my controler

def iCalDownoload():
     if user has privilages:
         blah blah blah
     else:
         return SCRIPT("alert('You do not have the right privileges to download. Please contact administrator: in...@algit.si')")

Mirek Zvolský

unread,
Feb 19, 2015, 4:46:36 AM2/19/15
to web...@googlegroups.com
If you call controller function and return something from this function, it will allways create new page (if you return string, it will be returned directly, if dictionary, it will be returned through the templating engine).

You can

1) test privileges before (when you create the page at first) and
a) immediately hide the button
--or-- b) have a hidden element <div style="display: none;">no privileges</div> and show it using javascript when user clicks

--or-- (if you don't want test privileges before, but after the click)

2) use ajax call (see ajax chapter of the book), which can return
a) script
--or-- b) innerHTML for some element






Dne čtvrtek 19. února 2015 9:15:09 UTC+1 Yebach napsal(a):

Leonel Câmara

unread,
Feb 19, 2015, 6:47:49 AM2/19/15
to web...@googlegroups.com
This will always go to a new page because you're following a link. You need to make this an ajax call associated with the onclick of the button. Then you can simply use response.flash to use the regular alerts web2py already provides or response.js and you don't even need to return anything else.

Vid Ogris

unread,
Feb 19, 2015, 7:06:54 AM2/19/15
to web...@googlegroups.com
on button click i am returning a file. Should it be any different with it. Also my response.flash or session.flash message is not working.Any ideas why? 

2015-02-19 12:47 GMT+01:00 Leonel Câmara <leonel...@gmail.com>:
This will always go to a new page because you're following a link. You need to make this an ajax call associated with the onclick of the button. Then you can simply use response.flash to use the regular alerts web2py already provides or response.js and you don't even need to return anything else.

--
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/Q63J8bcFz80/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.



--
Lep pozdrav 

Vid Ogris


Vid Ogris

unread,
Feb 19, 2015, 8:28:28 AM2/19/15
to web...@googlegroups.com
Are you suggesting using  ajax

<button onclick= "ajax('{{=URL("script","iCalDownload",  args = request.args[0])}}',[], '')" class="btn btn-primary"><span class="glyphicon glyphicon-export"></span>&nbsp;&nbsp;{{ =T('Export iCal') }}</button>

because the return from my function is

return exported_chunks_zip.getvalue()

Leonel Câmara

unread,
Feb 19, 2015, 8:53:40 AM2/19/15
to web...@googlegroups.com
That won't work, the easiest way is to use some kind of jquery file download plugin.
Reply all
Reply to author
Forward
0 new messages