2012-06-07 130 views
1

我試圖用Python在Google App Engine中編寫我的第一個應用程序。這裏的應用程序的鏈接:http://contractpy.appspot.com/ - 這只是一個實驗性的應用程序。整個代碼是波紋管。GAE TypeError:格式字符串沒有足夠的參數

但我發現了這個錯誤(顯示在日誌):

(...) line 229, in post "owner":owner}) 
TypeError: not enough arguments for format string 

229線在此如果塊:

if (resident_name and valid_resident(resident_name)) \ 
     and (user_SSN and valid_SSN(user_SSN)) \ 
     and ((not user_email) or (user_email and valid_email(user_email))): 
      self.redirect('/welcome?resident=%s&nacionality=%s?SSN=%s?driverLicense=%s?email=%s?witness =%s?owner=%s' % {"resident":resident_name, 
             "nacionality":user_nacionality, 
             "SSN":user_SSN, 
             "driverLicense":user_driverLicense, 
             "email":user_email, 
             "witness ":witness , 
             "owner":owner}) 

更specificaly,這是這一行:

"owner":owner}) 

我試圖做出改變,閱讀類似awnswers,但沒有奏效。 有人可以找出是什麼原因導致這個錯誤? 在此先感謝您的幫助!

# -*- coding: utf-8 -*- 
# -*- coding: utf-8 -*- 
#!/usr/bin/env python 
# 
# Copyright 2007 Google Inc. 
# 
# Licensed under the Apache License, Version 2.0 (the "License"); 
# you may not use this file except in compliance with the License. 
# You may obtain a copy of the License at 
# 
#  http://www.apache.org/licenses/LICENSE-2.0 
# 
# Unless required by applicable law or agreed to in writing, software 
# distributed under the License is distributed on an "AS IS" BASIS, 
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
# See the License for the specific language governing permissions and 
# limitations under the License. 
# 

import webapp2 

form = """ 

<html> 
    <head> 
    <title>Sign Up</title> 
    <style type="text/css"> 
     .label {text-align: right} 
     .error {color: red} 
    </style> 

    </head> 

    <body> 
    <h2>Signup</h2> 
    <form method="post"> 
     <table> 
     <tr> 
      <td class="label"> 
      resident 
      </td> 
      <td> 
      <input type="text" name="resident" value= "%(resident)s"> 
      </td> 
      <td class="error"> %(resident_error)s 

      </td> 
     </tr> 

     <tr> 
      <td class="label"> 
      nacionality 
      </td> 
      <td> 
      <input type="text" name="nacionality" value= " "> 
      </td> 
     </tr> 

     <tr> 
      <td class="label"> 
      License 
      </td> 
      <td> 
      <input type="text" name="driverLicense" value=""> 
      </td> 
     </tr> 

     <tr> 
      <td class="label"> 
      SSN 
      </td> 
      <td> 
      <input type="text" name="SSN" value="%(SSN)s"> 
      </td> 
      <td class="error"> %(SSN_error)s 
      </td> 
     </tr> 


     <tr> 
      <td class="label"> 
      Email (optional) 
      </td> 
      <td> 
      <input type="text" name="email" value="%(email)s"> 
      </td> 
      <td class="error"> %(email_error)s 

      </td> 
     </tr> 
     </table> 

<br> 
     <td class="label"> 
      Contract Type 
      <select name = "contractType"> 
       <option>House Rental Contract</option> 
       <option>Car Rental Contract</option> 
       <option>Other</option> 
      </select> 
      </td> 
<br> 
<br> 

<br> 
      owner 
      <select name = "owner"> 
       <option>House owner</option> 
       <option>Car owner</option> 
       <option>Other owner</option> 
      </select> 
<br> 
<br> 

<br> 
      Witness 
      <select name = "witness"> 
       <option>Carl Sagan</option> 
       <option>Mahatma Gandhi</option> 
      </select> 
<br> 
<br> 

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

</html> 
""" 

welcome = """ 
<html> 
    <head> 
    <title>Unit 2 Signup</title> 
    </head> 

    <body> 

%(contractType)s 

In consideration of the agreements of the Resident(s), known as: %(resident)s The owner hereby rents them the dwelling located at %(residentAdress)s, for the period commencing on the %(dateStarts)s, and monthly thereafter until the last day of %(dateEnds)s, at which time this Agreement is terminated. Resident(s), in consideration of owners permitting them to occupy the above property, hereby agrees to the following terms: 

RENT: To pay as rental the sum of $ (rentalSum) per month, due and payable in advance from the first day of every month. Failure to pay rent when due will result in the owner taking immediate legal action to evict the Resident from the premises and seize the security deposit. 
LATE FEE: Rent received after the first of the month will be subject to a late fee of 10% plus (3.00) dollars per day. 

ACCEPTED THIS (dateContract), at (localContract). 

___________________________________________________ 
%(resident)s - Resident 

___________________________________________________ 
%(owner)s – owner 

___________________________________________________ 
%(witness)s – Witness 

    </body> 
</html> 

""" 

import re 

USER_RE = re.compile(r"^[a-zA-Z0-9_-]{3,20}$") 
def valid_resident(resident): 
    return USER_RE.match(resident) 

PASS_RE = re.compile(r"^.{3,20}$") 
def valid_SSN(SSN): 
    return PASS_RE.match(SSN) 

EMAIL_RE = re.compile(r"^[\S][email protected][\S]+\.[\S]+$") 
def valid_email(email): 
    return EMAIL_RE.match(email) 

def escape_html(s): 
    for (i,o) in (("&","&"), (">",">"), ("<","<"), ('"','"')): 
     s = s.replace(i,o) 
     return s 

import time 

import datetime 

def dateToday(): 
    today = datetime.datetime.today() 
    todayDay = str(today.day) 
    todayMonth = str(today.month) 
    monthExt = {'1':' January ', '2':'February', '3':' March ', '4':'April', '5':'May', '6':'June', '7 ':' July ', '8':'August', '9':'September', '10':'October', '11':'November ', '12':'December'} 
    todayYear = str(today.year) 
    return(todayDay + ' of ' + monthExtenso[todaymonth] + ' of ' + todayYear) 

class MainHandler(webapp2.RequestHandler): 
    def get(self): 
     self.response.out.write(form %{"resident": "", 
             "SSN": "", 
             "driverLicense": "", 
             "email":"", 
             "resident_error": "", 
             "SSN_error": "", 
             "driverLicense_error": "", 
             "email_error": ""}) 

    def write_form(self, text_write): 
     self.response.out.write(text_write) 

    def post(self): 
     resident_name = self.request.get(escape_html("resident")) 
     user_nacionality = self.request.get("nacionality") 
     user_SSN = self.request.get(escape_html('SSN')) 
     user_email = self.request.get(escape_html('email')) 
     user_driverLicense = self.request.get(escape_html('driverLicense')) 
     resident_error = "" 
     SSN_error = "" 
     driverLicense_error = "" 
     contract_type = self.request.get("contractType") 
     owner = self.request.get("owner") 
     witness = self.request.get("witness ") 

     if (resident_name and valid_resident(resident_name)) \ 
     and (user_SSN and valid_SSN(user_SSN)) \ 
     and ((not user_email) or (user_email and valid_email(user_email))): 
      self.redirect('/welcome?resident=%s&nacionality=%s?SSN=%s?driverLicense=%s?email=%s?witness =%s?owner=%s' % {"resident":resident_name, 
             "nacionality":user_nacionality, 
             "SSN":user_SSN, 
             "driverLicense":user_driverLicense, 
             "email":user_email, 
             "witness":witness, 
             "owner":owner}) 
     else: 
      if not valid_resident(resident_name): 
       resident_error = "Oh no!!! this resident name isn't valid!" 
      if not valid_SSN(user_SSN): 
       SSN_error = "Oh no!!! SSN isn't valid!" 
      if user_email and not valid_email(user_email): 
       email_error = "Oh no!!! e-mail isn't valid!" 
      self.write_form(form % {"resident":resident_name, 
             "resident_error":resident_error, 
             "SSN":user_SSN, 
             "SSN_error":SSN_error, 
             "driverLicense":user_driverLicense, 
             "email":user_email, 
             }) 

class WelcomeHandler(webapp2.RequestHandler): 
    def get(self): 
     geted_resident_name = self.request.get('resident') 
     geted_user_nacionality = self.request.get("nacionality") 
     geted_user_SSN = self.request.get('SSN') 
     geted_user_email = self.request.get('email') 
     geted_user_driverLicense = self.request.get('driverLicense') 
     geted_resident_error = "" 
     geted_SSN_error = "" 
     geted_driverLicense_error = "" 
     #geted_contract_type = self.request.get("contractType") 
     geted_owner = self.request.get("owner") 
     geted_witness = self.request.get("witness ") 



     self.response.out.write(welcome % {"resident":geted_resident_name, 
             "nacionality":geted_user_nacionality, 
             "SSN":geted_user_SSN, 
             "SSN_error":geted_SSN_error, 
             "driverLicense":geted_user_driverLicense, 
             "email":geted_user_email, 
             "witness ":geted_witness , 
             "owner":geted_owner, 
             }) 


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

回答

3

右手參數一起使用時,格式字符串必須是一個元組,而不是一個字典的%操作。在你的情況下,該字典將替換格式字符串中第一次出現的%s,而沒有足夠的其他對象替換其他對象。您需要手動構建要用於替換的項目元組,如:

('/welcome?resident=%s&nacionality=%s?SSN=%s?driverLicense=%s?email=%s?witness =%s? 
owner=%s' % (resident_name, user_nacionality, user_SSN, user_driverLicense, user_email, 
witness, owner) 
+0

有趣的...我用另一個工作模式的應用程序,我認爲這是使用字典。但你的消化固定了錯誤。謝謝,Wooble! – craftApprentice

1

當字符串中有x格式參數,但只傳遞了y個參數以匹配then(其中x> y)時,沒有足夠的參數。

在你的情況下,你有一個錯過%,只是將s%更改爲%s。

self.redirect('/welcome?resident=%s&nacionality=%s?SSN=%s?driverLicense=%s?email=s%?witness =%s?owner=%s' % {"resident":resident_name, 
             "nacionality":user_nacionality, 
             "SSN":user_SSN, 
             "driverLicense":user_driverLicense, 
             "email":user_email, 
             "witness ":witness , 
             "owner":owner}) 
+0

非常感謝Shay。確實有一個打字錯誤(「email = s%」)。但我修正了(%s)並且消息錯誤仍然存​​在。任何提示有什麼問題? – craftApprentice

相關問題