0

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?

Neli
  • 532
  • 1
  • 3
  • 15
  • Why do you redirect. Render a gesendet page in your post. – voscausa Oct 05 '16 at 22:22
  • That's a good idea, but since I want to return to the main page with only one additional sentence, i prefered to ask if someone has an idea to do it like that – Neli Oct 07 '16 at 07:49
  • You can render the main page again in your post but now with a template value for the gesendet message – voscausa Oct 07 '16 at 10:23

1 Answers1

0

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), or GAE's memcache/datastore/GCS.

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

Community
  • 1
  • 1
Dan Cornilescu
  • 39,470
  • 12
  • 57
  • 97