GAE python: success message or add HTML class after redirect
Question: I've a website with a contact form running on a google App Engine. After
submitting I'd like to redirect and show a message to the user to let him know
the message was sent, this can eighter be a alert message or adding a class to
a html tag. How can I do that?

my python file looks like this:

    
    
    import webapp2
    import jinja2
    import os
    from google.appengine.api import mail
    
    jinja_environment = jinja2.Environment(autoescape=True,loader=jinja2.FileSystemLoader(os.path.join(os.path.dirname(__file__), 'templates')))
    
    class index(webapp2.RequestHandler):
        def get(self):
            template = jinja_environment.get_template('index.html')
            self.response.write(template.render())
    
        def post(self):
            vorname=self.request.get("vorname")
            ...
            message=mail.EmailMessage(sender="...",subject="...")
    
            if not mail.is_email_valid(email):
                self.response.out.write("Wrong email! Check again!")
    
            message.to="..."
            message.body=""" Neue Nachricht erhalten:
            Vorname: %s
            ... %(vorname,...)
    
            self.redirect('/#Kontakt')
    
    app = webapp2.WSGIApplication([('/', index)], debug=True)
    

I already tried this in my html file:

    
    
    <script>
        function sentAlert() {
             alert("Nachricht wurde gesendet");
        }
    </script>
    
    <div class="submit">
        <input type="submit" value="Senden" onsubmit="return sentAlert()"   
         id="button-blue"/>
    </div>
    

but it does it before the redirect and therefore doesn't work. Does someone
have an idea how to do that?


Answer: After the redirect a request **different** than the POST one for which the
email was sent will be served.

So you need to **persist** the information about the email being sent across
requests, saving it in the POST request handler code and retrieving it in the
subsequent GET request handler code (be it the redirected one or any other one
for that matter).

To persist the info you can, for example, use the user's session (if you
already have one, see [Passing data between pages in a redirect() function in
Google App Engine](http://stackoverflow.com/questions/37134540/passing-data-
between-pages-in-a-redirect-function-in-google-app-engine?noredirect=1)), or
GAE's memcache/datastore/GCS.

Once the info is retrieved you can use it any way you wish.

