1

I am just getting to grips with jinja2 templating on GAE. What I am trying to do is to display a status msg in the rendered template without rendering the whole template again.

I have a webapp2 request handler which deals with sending a mail. In case of an error exception I would like to display a message on the page. Ditto if the mail has been sent successfully.

I render the template and pass the msg as a template value. I can see that this is not the way of doing things as the whole template renders again. What I want to do is simply pass the msg through.

class ContactoPage(webapp2.RequestHandler):

global template
template = jinja_environment.get_template('contacto.html')

def get(self):
    self.response.out.write(template.render({'mail_status':''}))

def post(self):
    userMail=self.request.get("emailFrom")
    if not mail.is_email_valid(userMail):
        self.response.out.write(template.render({'mail_status':'wrong mail address'}))
        return      
    subject="Test Mail"
    userMessage=self.request.get("emailBody")
    message=mail.EmailMessage(sender="dennisargeomatica@gmail.com",subject="Test")
    message.to=userMail
    message.body=("Thank you! \n"
                  "Your mail: %s \n"
                  "Subject: %s \n"
                  "Message: %s \n"
                  %(userMail,subject,userMessage))
    message.send()
    self.response.out.write(template.render({'mail_status':'rudy, a msg to you'}))

Thx, Dennis

Dennis Bauszus
  • 1,624
  • 2
  • 19
  • 44
  • 1
    what you are doing is fine. You can't pass the message through like you suggest without some javascript running on the client side, managing your POST of the data and then updating just that component of the page. hence you have to render it all to change it. http://en.wikipedia.org/wiki/Ajax_%28programming%29 – Paul Collingwood May 14 '15 at 19:45
  • thanks for letting me know. i ported this bit of code from an existing application which i wrote in asp.net using updatepanels. i thought something similar would have been available with jinja2 templates. i will look into doing something on the client side as i don't want the whole page to reload. – Dennis Bauszus May 14 '15 at 19:52
  • np. this might help: http://www.django-rest-framework.org/ – Paul Collingwood May 14 '15 at 20:01
  • i had a look at this before but decided but going through the hassle of setting up django on the eclipse IDE for use with Google App Engine plugin is not worth the hassle. Django might be overkill for most simple applications and I really like the speed of the jinja2 framework. I solved the issue with an AJAX call to a mail service. – Dennis Bauszus May 15 '15 at 14:50

1 Answers1

2

Paul Collingwood (not the Durham cricketer I assume) pointed me in the right direction by suggesting this should be solved with an AJAX call. Coming from an ASP.net background I got used to update panels and was looking for a similar technology within the jinja2 framework. This doesn't seem to exist. Thankfully, AJAX calls with jQuery are dead simple. I make the call from my javascript on the client side like so:

function mailService() {
mailFrom = $('#tbFrom').val();
mailMsg = $('#tbMail').val();
$.ajax({
    type : 'POST',
    url : 'mailService',
    data : {
        mailFrom:mailFrom,
        mailMsg:mailMsg
    },
    success: function(response) {
        $('#mailStatus').html(response);
    }
})}

This is the mailService webapp2 requesthandler on the serer which sends a response back after dealing with the mails:

class MailService(webapp2.RequestHandler):

def post(self):
    mailFrom = self.request.get('mailFrom')
    mailMsg = self.request.get('mailMsg')
    if not mail.is_email_valid(mailFrom):
        self.response.write('wrong mail address')
        return
    message=mail.EmailMessage(sender='dennisargeomatica@gmail.com',subject='Your mail to Argeomatica')
    message.to=mailFrom
    message.body=('Thank you, we have received your mail. \n'
                  'Message: %s \n'
                   %(mailMsg))
    message.send()
    self.response.write('mail sent')
Dennis Bauszus
  • 1,624
  • 2
  • 19
  • 44