Frage

I've got a GAE-app that uses Jinja2 templates to serve its html pages.

Now in my main python file I've got one class, mainhandler, with a GET and a POST method. This all works for the welcome screen where there is a button to do something. When the button is clicked, the POST method is invoked which calls a second page.

I can't find anything about how to catch the button events on the second page, result.html. And make it progress methods in the main python file.

So: "How can I work with errorMail and toCalendar buttons on result.html?

This is my main file:

# -*- coding: utf8 -*- 

import webapp2
from apiclient.discovery import build
from oauth2client.appengine import OAuth2Decorator

from format import formatFile

import jinja2
import os

jinja_environment = jinja2.Environment(
    loader=jinja2.FileSystemLoader(os.path.dirname(__file__)))

decorator = OAuth2Decorator(secret)

class MainHandler(webapp2.RequestHandler):
    @decorator.oauth_required
    def get(self):
        template = jinja_environment.get_template('index.html')
        self.response.out.write(template.render())

    #processes the file and shows the results
    def post(self):
        # Get the authorized Http object created by the decorator.
        http = decorator.http()

        service = build('calendar', 'v3', http=http,
           developerKey='secret')

        # Make a list of calendars
        calendar_list = service.calendarList().list().execute()

        totalList = formatFile(self.request.get('file'))

        template_values = {"totalList": totalList, "calendar_list": calendar_list}

        template = jinja_environment.get_template('result.html')
        self.response.out.write(template.render(template_values))


app = webapp2.WSGIApplication([('/', MainHandler)],
                              debug=True)

This is page index.html:

<!DOCTYPE html>

<html>
  <head><title></title></head>
  <body>
    <form method="post">
    <div><label>Select file:</label</div>
    <input type="file" name="file">
    <br>
    <input type="submit" name="upload" value="Upload">
    </form>
  </body>
</html>

This is page result.html:

<html>
    <head>

    </head> 
    <body>
        <h3>De volgende data staat klaar voor je agenda:</h3>
        <table border="1" cellpadding="3">
            <tr>
                <th>Dag</th>
                <th>Datum</th>
                <th>Tijd</th>
                <th>Omschrijving</th>
            </tr>
                 {% for line in totalList %}
                <tr>
                    {% for item in line %}
                    <td>{{ item }}</td>
                    {% endfor %}
            </tr>        
            {% endfor %}
        </table>

        <br>
        <b>Selecteer de agende waar de diensten in geplaatst worden:</b>
        <br>
        <select>
            {% for calendar_list_entry in calendar_list['items'] %}
            <option value=>{{ calendar_list_entry['summary'] }}</option>
            {% endfor %}
        </select>
        <br>

        <form method="post">
            <input type="submit" name="toCalendar" value="In kalender plaatsen">
        </form>
        <br>
        <b>Uitvoer incorrect? Klik dan op onderstaande knop om foutmeldings-email te sturen.</b>
        <form method="post">
            <input type="submit" name="errorMail" value="Uitvoer incorrect!">
        </form>

    </body>
</html>
War es hilfreich?

Lösung

You do not have to receive buttons events. You receive the form data (including the buttons) in the post like the self.request.get('file')

You can add more than one button to a post. Every form can have its own post handler, by adding an action:

index.html (results in post to /result1):

<form action="/result1" method="post">

result.html (results in post to /result2):

<form action="/result2" method="post">
    <input id="toCalender " type="submit" name="toCalendar" value="In kalender plaatsen">
    <br>
    <b>Uitvoer incorrect? Klik dan op onderstaande knop om foutmeldings-email te sturen.</b>
    <input id="errorMail" type="submit" name="errorMail" value="Uitvoer incorrect!">
</form>
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top