2

我有一个GAE应用程序,它使用Jinja2模板来为其html页面提供服务。Jinja2 html按钮:在不同的页面上捕获POST

现在在我的主python文件中,我有一个类,主处理程序,一个GET和一个POST方法。这一切都适用于有一个按钮可以执行某些操作的欢迎屏幕。当按钮被点击时,调用POST方法调用第二页。

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

所以说:“我怎样才能与errorMail和toCalendar按钮上result.html工作

这是我的主要文件:

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

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> 

回答

2

您不必接收按钮事件。您可以像self.request.get('文件')一样接收表单数据(包括按钮)

您可以向帖子添加多个按钮。 每个表单可以有自己的处理程序后,通过添加一个动作:

的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> 
+0

我是否需要在应用程序中添加处理程序= webapp2.WSGIApplication([( '/',MainHandler)], 调试=真)? – Difusio

+1

您可以为每个帖子添加一个处理程序,但您也可以查找路径并找出页面已发布/提交的数据。 – voscausa

+0

你有什么机会可以在你的答案中详细阐述一下? – Difusio

相关问题