2015-10-14 115 views
0

嗨我有一个Web应用程序的问题。它总是给一个形式。我的代码是这样的:验证的Web应用程序代码

import webapp2 

form=""" 
<form method="post"> 
what is your birthday 
<br> 

<label>Month 
<input name="text" name="month"> 
</label> 

<label>Day 
<input name="text" name="day"> 
</label> 

<label>Year 
<input name="text" name="year"> 
</label> 

<br> 
<br> 
<input type="submit"> 
</form> 
""" 

months = ['January', 
      'February', 
      'March', 
      'April', 
      'May', 
      'June', 
      'July', 
      'August', 
      'September', 
      'October', 
      'November', 
      'December'] 

def valid_day(day): 
    if day and day.isdigit(): 
     day = int(day) 
     if day < 32 and day > 0: 
         return day 

def valid_month(month): 
    if(month): 
     month = month.capitalize() 
     if month in months: 
         return month 

def valid_year(year): 
    if year and year.isdigit():  
     year = int(year) 
     if year < 2020 and year > 1880: 
         return year 

class MainPage(webapp2.RequestHandler): 

    def get(self): 
     self.response.out.write(form) 


    def post(self): 
    user_month = valid_month(self.request.get('month')) 
    user_day = valid_day(self.request.get('day')) 
    user_year = valid_year(self.request.get('year')) 

    if not (user_month and user_day and user_year): 
     self.response.out.write(form) 
    else: 
     self.response.out.write("Thanks! That's a totally valid day!") 

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

但即使我给了一个正确的输入,它仍然会给出表格。任何人都可以帮助我吗?我无法检查这里有什么问题。

回答

0

看着你发布的代码,你不需要指定表单的action属性,所以当你点击submit时,表单不会被发送到任何地方。为了给你一个完整的答案,我需要看到你的服务器端代码(如果存在的话)来处理表单提交 例如

<form method="POST" action="/path_to_server_code"> 

那么你的服务器监听在指定的URL到POST请求

@app.route('/path_to_server_code') 
def process_form(): 
    # do something with the form 
+0

嗨,你的帮助真的坦克:动作,你应该在那个位置

例如处理表单。我通过谷歌appengine在我的localhost:8080上运行我的Web应用程序。这是我的整个代码。 –

+0

我是如此回答你的问题,还是你问我另一个问题? 很高兴我能帮到你 – shifloni

相关问题