python - Jinja2 html按钮: Catch POST on different pages

标签 python google-app-engine jinja2 google-api-python-client

我有一个 GAE 应用程序,它使用 Jinja2 模板来提供其 html 页面。

现在,在我的主 python 文件中,我有一个类 mainhandler,它具有 GET 和 POST 方法。这一切都适用于欢迎屏幕,其中有一个按钮可以执行某些操作。单击该按钮时,将调用 POST 方法,该方法将调用第二个页面。

我找不到任何有关如何捕获第二页 result.html 上的按钮事件的信息。并使其在主 python 文件中取得进展方法。

那么:“如何使用 result.html 上的 errorMail 和 toCalendar 按钮?

这是我的主文件:

# -*- 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)

这是页面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>

这是页面结果.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>

最佳答案

您不必接收按钮事件。您会在帖子中收到表单数据(包括按钮),例如 self.request.get('file')

您可以向一篇帖子添加多个按钮。 每个表单都可以通过添加操作来拥有自己的帖子处理程序:

index.html(结果发布到/result1):

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

result.html(结果发布到/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>

关于python - Jinja2 html按钮: Catch POST on different pages,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12960424/

相关文章:

java - 数据存储索引异常,尽管我已经为其定义了索引

java - 如何通过电子邮件发送谷歌应用程序引擎日志文件?

python - 如何在 jinja 模板中获取主机名的 IP 地址

python - 将 POST 和 GET 发送到同一函数时出现 flask 错误,

python - 根据某些条件删除表中的行

python - 使用 Mock python 断言对方法的调用

python - 如何在 os.system 中使用 python 变量?

python - 具有嵌入层的 PyTorch 朴素单标签分类随机失败

php - 从 GAE 向 GCP 日志条目添加标签

jinja2 - jinja2从模板加载模板文件