what are the ways I can reload or refresh the current page where redirect doesn't work

374 views
Skip to first unread message

Ron Chatterjee

unread,
Jan 13, 2016, 1:37:16 PM1/13/16
to web2py-users
How do I update a current page in its view after a file submission if the redirect doesn't work?

{{redirect(URL('show_project_summary', args = Selected_project.id), client_side = True)}}

Any response.js that will update or reload/refresh the current page?

Anthony

unread,
Jan 13, 2016, 1:43:10 PM1/13/16
to web2py-users
Please show some code.

Ron Chatterjee

unread,
Jan 13, 2016, 1:49:42 PM1/13/16
to web2py-users
I have this in my view (show_project_summary.html).


    
{{ if (submission.created_by == auth.user_id):}}
<div id="file-uploader"></div>
{{pass}}
    




Script in the same view at the bottom:

<script>
jQuery(document).ready(function() {
  var uploader = new qq.FileUploader({
        // pass the dom node (ex. jQuery(selector)[0] for jQuery users)
        element: document.getElementById('file-uploader'),
        // path to server-side upload script
        action: '{{=URL("upload_callback_submission")}}/{{=request.args(0)}}',
        sizeLimit: 150000000,
        minSizeLimit: 0,
        allowedExtensions: ['xls','jpg', 'jpeg', 'pdf', 'txt','doc','docx','ppt', 'pptx', 'htm','html','xml','xmls', 'txt','ppt','png', 'gif'],
        // set to true to output server response to console
        debug: true,

        // events
        // you can return false to abort submit
        onSubmit: function(id, fileName){},
        onProgress: function(id, fileName, loaded, total){},
        onComplete: function(id, fileName, responseJSON){},
        onCancel: function(id, fileName){},

        messages: {
            // error messages, see qq.FileUploaderBasic for content
            typeError: "{file} {{=T('has invalid extension.')}} {{=T('Only')}} {extensions} {{=T('are allowed.')}}",
            sizeError: "{file} {{=T('is too large, maximum file size is')}} {sizeLimit}.",
            minSizeError: "{file} {{=T('is too small, minimum file size is')}} {minSizeLimit}.",
            emptyError: "{file} {{=T('is empty, please select files again without it.')}}",
            onLeave: "{{=T('The files are being uploaded, if you leave now the upload will be cancelled.')}}"
        },
        showMessage: function(message){ alert(message); }
    });
    });
</script>



In my default controller:

def upload_callback():
    Selected_project = []
    #Rendering rule for Post
    Selected_project = db.Project(request.args(0, cast=int))
    db.Project_Files.Project_id.default = Selected_project.id;
    project = db.Project[request.args(0)]
    if Selected_project is None:
        raise HTTP(404)
    if 'qqfile' in request.vars:
        filename = request.vars.qqfile
        newfilename = db.Project_Files.filename.store(request.body, filename)
        db.Project_Files.insert(Project_id=Selected_project.id, filename=newfilename)
    return response.json({'success': 'true'})

Ron Chatterjee

unread,
Jan 13, 2016, 1:51:51 PM1/13/16
to web2py-users
Basically after the file upload, I would like to refresh/reload "show_project_summary.html" to show the list of the file that just been downloaded. 

Anthony

unread,
Jan 13, 2016, 2:06:34 PM1/13/16
to web2py-users
Where is:


{{redirect(URL('show_project_summary', args = Selected_project.id), client_side = True)}}

If that's in a view file for upload_callback, it will never get executed, because upload_callback doesn't return a dictionary.

You can call redirect(..., client_side=True) within the upload_callback function, though that will skip the returning of the {"success": "true"} JSON response (though perhaps you don't need that, as returning a 200 response may be sufficient to imply success). Alternatively, you can set response.js:

    response.js = 'window.location = "%s";' % URL(...)

That's really all the client side redirect does.

Anthony

Ron Chatterjee

unread,
Jan 13, 2016, 2:17:22 PM1/13/16
to web2py-users
Yes. That's what I thought too. But what's the correct syntax for response.js Anthony?

Is it:

response.js = 'window.location = "%s";' % URL('default',show_project_summary')

?
And I believe I need to invoke that in my controller. Right?

Anthony

unread,
Jan 13, 2016, 2:20:14 PM1/13/16
to web2py-users
Yes, in the controller, and just us the URL function as usual.

Anthony

Ron Chatterjee

unread,
Jan 13, 2016, 2:27:36 PM1/13/16
to web2py-users
It didn't work. didn't reload it. 

Anthony

unread,
Jan 13, 2016, 2:44:33 PM1/13/16
to web2py-users
Check the returned response to ensure the JS is in the header, and check the browser JS console to see if there are any errors.

Looks like the JS library you are using also has an onComplete callback, so you could put the reload code in there.

Anthony
Reply all
Reply to author
Forward
0 new messages