The problem is that web2py.js automatically disables the submit buttons on
forms after clicking, but it does not re-enable them unless the form is
submitted via Ajax (under the assumption that a non-Ajax form submission
will result in a new page being loaded in the browser, making it
unnecessary to re-enable the button on the original page). This breaks down
in your case because the form submission results in a file attachment being
returned to the browser rather than a new page loading -- so the original
page remains in the browser window.
We should probably come up with a way to accommodate this case, but for
now, you can add some JS code to immediately re-enable the button yourself.
In the view, add something like:
<script>
jQuery(function() {
jQuery(document).on('submit', 'form', function() {
var submit_button = $(this).find(jQuery.web2py.formInputClickSelector);
jQuery.web2py.enableElement(submit_button);
});
})
</script>
Alternatively, you can put the form in a web2py component (i.e., LOAD(...,
ajax=True)), which will cause the form to be submitted via Ajax. You can
then return the file via a separate action, which can be called via
Javascript after the form is submitted:
def print_countries():
...
if form.process().accepted:
response.js = 'window.location = "%s";' % URL('default', 'get_pdf')
return dict(form=form)
Then move all the PDF creation code to the get_pdf function. In this case,
print_countries would be used as an Ajax component within a containing
page. When the form is submitted, response.js will be returned to the
browser where it will be executed, resulting in the file being downloaded.
Anthony
On Saturday, November 7, 2015 at 3:45:07 AM UTC-5, Karl Florian wrote:
>
> Hi,
> maybe some Pictures can demonstrate what i mean.The source code is also
> attached:
> This excample is of my Countries Report.
>
>
>
> <https://lh3.googleusercontent.com/-3qSbBrXf50Y/Vj22n9l93iI/AAAAAAAAAAw/siaI3TBfrNA/s1600/CountriesReport1.jpg>
>
>
> <https://lh3.googleusercontent.com/--0nkparVSI0/Vj22ttxTSPI/AAAAAAAAAA4/oyZwenqUf2Y/s1600/CountriesReport2.jpg>
>
>
> <https://lh3.googleusercontent.com/-Ulyv9Uo-U_Y/Vj22zDAUz4I/AAAAAAAAABA/XhiEfVLB8zs/s1600/CountriesReport3.jpg>
>
> <https://lh3.googleusercontent.com/-QigPM6MGLS8/Vj223l3AiwI/AAAAAAAAABI/oynrIlh7lcM/s1600/CountriesReport4.jpg>
> First Picture is start of app
> Second Picture is after Print Report Button was pressed
> Third Picture is my pdf-file
> Fourth Picture is after closing the pdf viewer
>
> Any suggestions will be appreciated.
>
> Karl:-) :) :] =)
>
> Am Freitag, 6. November 2015 20:36:33 UTC+1 schrieb Anthony:
>
>> Also, dict() is for creating a dictionary -- there is no reason to do
>> dict(redirect(...)). Also, you do not return a redirect() -- you simply
>> call it, and it immediately raises an HTTP exception.
>>
>> Anthony
>>
>> On Friday, November 6, 2015 at 1:50:15 PM UTC-5, Dave S wrote:
>>>
>>>
>>>
>>> On Friday, November 6, 2015 at 9:33:47 AM UTC-8, Karl Florian wrote:
>>>>
>>>> *Sorry everybody but i need your help again.*
>>>>
>>>> Here is my code:
>>>>
>>>> def languages_pedit():
>>>>
>>>
>>> [...]
>>>
>>>
>>>>
>>>>
>>>> * #I tried this mk_pdffile(pdffile,'languages.pdf') but it did not
>>>> work*
>>>>
>>>> def mk_pdffile(pdffile,pdfname):
>>>>
>>>> import cStringIO
>>>>
>>>> pdfdata = open(pdffile,"rb").read()
>>>>
>>>> response.headers['Content-Type']='application/pdf'
>>>>
>>>> return response.stream(cStringIO.StringIO(pdfdata), attachment=True,
>>>> filename=pdfname)
>>>>
>>>>
>>> Where does the "does not work" part stop and the "this works fine part
>>> start?
>>>
>>>>
>>>>
>>>> @auth.requires_login()
>>>>
>>>> def print_languages():
>>>>
>>> [...] .......................................
>>>>
>>>> #Code to make my texfile....................
>>>>
>>>> .......................................
>>>>
>>>> texfile.write('\\end{document}\n')
>>>>
>>>> [...]
>>>
>>>> return response.stream(cStringIO.StringIO(pdfdata), attachment=True,
>>>> filename='languages.pdf')
>>>>
>>>>
>>>>
>>>> *This works fine but the code below does not get excecuted and my
>>>> SUBMIT Button stays pressed?*
>>>>
>>>>
>>>>
>>>> return dict(redirect(URL('languages_pedit')))
>>>>
>>>>
>>>
>>> Where does this go? If right after the "return response.stream()", then
>>> it will never get executed.
>>>
>>>
>>>> def example():
>>> return 1
>>> return 2
>>>
>>>
>>> will always return 1.
>>>
>>> /dps
>>>
>>>
>>
--
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 the Google Groups
"web2py-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
For more options, visit https://groups.google.com/d/optout.