Search code examples
google-app-enginepython-2.7jinja2app-engine-ndbwebapp2

Google App Engine + Jinja2: Using self.redirect() with params


Let's say I have a Book ndb.Model class.

class Book(ndb.Model):
    title = ndb.StringProperty(required = True)

Book entities are created by this handler:

     def get(self):
         self.render('new-book.html')

     def post(self):
          title = self.request.get('title')

         if title:
            b = Book(title = title)
            b.put()
            self.redirect('/book/%s' % str(b.key.id()))

When a Book entity is created, we are redirected to the /book/[book id]. On this page, I would like to include a message which says "Good job! You've created [book title]." How can I do this?


Solution

  • Why not include your message in the url and then place it where you want it using javascript?

    self.redirect('/book/%s?message=%s' % (str(b.key.id()), "Good job! You've created " + title)
    

    for the js do something like this...

    alert(document.location.substring(document.location.indexOf("?message=")+9, document.location.length));
    

    Hope this helps!