Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Tornado redirecting to page with parameters

Tags:

python

tornado

I'm using self.render to render a html template, which is dependent on the information received from the client via ajax in the def post() method like this:

class aHandler(BaseHandler):
    @tornado.web.authenticated
    def post(self):
        taskComp = json.loads(self.request.body)   
        if taskComp['type'] == 'edit':
            if taskComp['taskType'] == 'task':
                self.render(
                    "tasks.html",         
                    user=self.current_user,
                    timestamp='',
                    projects='',
                    type='',
                    taskCount='',
                    resName='')

However this does not redirect the user to the html page 'tasks.html'.

However I see in my console a status:

[I 141215 16:00:55 web:1811] 200 GET /tasks (127.0.0.1)

Where '/tasks' is an alias for tasks.html

Why wouldn't this be redirected?

Or how can data received from ajax, then be used to redirect to the tasks.html page along with all the parameters supplied in the above self.render request?

like image 515
user94628 Avatar asked Dec 15 '14 16:12

user94628


1 Answers

"render" never redirects a visitor's browser to a different URL. It shows the browser the contents of the page you render, in this case the "tasks.html" template.

To redirect the browser:

@tornado.web.authenticated
    def post(self):
        self.redirect('/tasks')
        return

More info in the redirect documentation.

To redirect using an AJAX response, try sending the target location from Python to Javascript:

class aHandler(BaseHandler):
    @tornado.web.authenticated
    def post(self):
        self.write(json.dumps(dict(
            location='/tasks',
            user=self.current_user,
            timestamp='',
            projects='',
            type='',
            taskCount='',
            resName='')))

Then in your AJAX response handler in Javascript:

$.ajax({
  url: "url",
}).done(function(data) {
  var url = data.location + '?user=' + data.user + '&timestamp=' + data.timestamp; // etc.
  window.location.replace("http://stackoverflow.com");
});

More about URL encoding is at this answer.

like image 164
A. Jesse Jiryu Davis Avatar answered Oct 29 '22 07:10

A. Jesse Jiryu Davis