2015-10-14 120 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

相關問題