Pregunta

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?

¿Fue útil?

Solución

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!

Otros consejos

I would just simply do it like

main.py or whatever else you use for your app something like:

app = webapp2.WSGIApplication([

    ('/book/([^/]+)?', server.BookHandler)

], debug=True)

Handlers something like:

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()), abort=True)

#...more stuff

#on handler for book
def get(self, message):
    #use message

    self.response.out.write(message)
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top