Warum diese HTML-Vorlage auf jinja2 / python produziert 'u ()' Tupel-Fehler in String-Substitution?

Ich schrieb den Code unten, aber etwas Seltsames passiert. Einige Teile der Streichersubstitution mit jinga2 setzen den substituierten String in 'u( )' , so:

Bildbeschreibung hier eingeben

Aber das geschieht nur mit 3 Variablen. Hier die HTML-Vorlage:

 <!DOCTYPE html> <html> <head> <title>Contract with Python</title> <style type="text/css"> .label {text-align: right} .error {color: red} </style> </head> <body> <h2>Contract with Python</h2> <form method="post"> <hr> <table> <tr> <td class="label"> Name </td> <td> <input type="text" name="person" value= "{{person}}"> </td> <td class="error"> {{person_error}} </td> </tr> <tr> <td class="label"> Nacionality </td> <td> <input type="text" name="nacionality" value= "{{nacionality}}"> </td> </tr> <tr> <td class="label"> Profession </td> <td> <input type="text" name="profession" value= "{{profession}}"> </td> </tr> <tr> <td class="label"> Marital Status </td> <td> <select name = "maritalStatus"> <option>unmarried</option> <option>married</option> <option>divorced</option> <option>widower</option> </select> </td> </tr> <tr> <td class="label"> Driver's License </td> <td> <input type="text" name="driverLicense" value=" {{driverLicense}}"> </td> </tr> <tr> <td class="label"> SSN </td> <td> <input type="text" name="SSN" value="{{SSN}}"> </td> <td class="error"> {{SSN_error}} </td> </tr> <tr> <td class="label"> Address </td> <td> <input type="text" name="address" value= "{{address}}"> </td> </tr> <tr> <td class="label"> Email (optional) </td> <td> <input type="text" name="email" value="{{email}}"> </td> <td class="error"> {{email_error}} </td> </tr> </table> <br> <hr> <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> <tr> <td class="label"> Contract number </td> <td> <input type="text" name="numBook" value= "{{numBook}}"> </td> </tr> <tr> <td class="label"> Initial Page </td> <td> <input type="text" name="numInitialPage" value= "{{numInitialPage}}"> </td> </tr> <tr> <td class="label"> Final Page </td> <td> <input type="text" name="numFinalPage" value= "{{numFinalPage}}"> </td> </tr> <hr> <input type="submit"> </form> </body> </html> 

Wie kann man das beheben? Warum ist es nur passend zu "numBook", "numInitialPage" und "numFinalPage" (aber nicht zu den anderen String-Substitutionen) ?

Der ganze Code ist hier:

 # -*- 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 os import webapp2 import jinja2 jinja_environment = jinja2.Environment(autoescape=True, loader=jinja2.FileSystemLoader(os.path.join(os.path.dirname(__file__), 'templates'))) import re from google.appengine.ext import db USER_RE = re.compile(r"^[a-zA-Z0-9_ -]{3,20}$") def valid_person(person): return USER_RE.match(person) PASS_RE = re.compile(r"^.{3,20}$") def valid_SSN(SSN): return PASS_RE.match(SSN) EMAIL_RE = re.compile(r"^[\S]+@[\S]+\.[\S]+$") def valid_email(email): return EMAIL_RE.match(email) import time import datetime def getNumBookPg(): doc = open("numbookpg.txt") numBookPg = [] numbers = doc.read().split() for n in numbers: numBookPg.append(n) return numBookPg numBookPage = getNumBookPg() ##def updateNumBookPg(b, ip, fp): ## newNumBookPg = ' ' + b + ' ' + str(int(fp) + 1) + ' ' + str(int(fp) + 1) ## doc = open("numbookpg.txt", "w") ## doc.write(newNumBookPg) 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 ' + monthExt[todayMonth] + ' of ' + todayYear) class MainHandler(webapp2.RequestHandler): def get(self): numBook = numBookPage[-3] ## numInitialPage = numBookPage[-2] ## numFinalPage = numBookPage[-1] template_values = {"person": "", "nacionality": "", "SSN": "", "driverLicense": "", "email":"", "person_error": "", "SSN_error": "", "driverLicense_error": "", "address": "", "email_error": "", "numBook": numBook, ## "numInitialPage": numInitialPage, ## "numFinalPage": numFinalPage, } template = jinja_environment.get_template('index.html') self.response.out.write(template.render(template_values)) ## def write_form(self, text_write): ## self.response.out.write(text_write) def post(self): person_name = self.request.get("person") user_nacionality = self.request.get('nacionality') user_profession = self.request.get('profession') user_maritalStatus = self.request.get('maritalStatus') user_SSN = self.request.get('SSN') user_email = self.request.get('email') user_driverLicense = self.request.get('driverLicense') person_error = "" SSN_error = "" driverLicense_error = "" geted_email_error = "" address = self.request.get('address') contractType = self.request.get("contractType") owner = self.request.get("owner") witness = self.request.get("witness") numBook = self.request.get("numBook"), numInitialPage = self.request.get("numInitialPage"), numFinalPage = self.request.get("numFinalPage"), if (person_name and valid_person(person_name)) and (user_SSN and valid_SSN(user_SSN)) and ((not user_email) or (user_email and valid_email(user_email))): a = Person(firstName = person_name, nacionality = user_nacionality, maritalStatus = user_maritalStatus, profession = user_profession, SSN = int(user_SSN), driverLicense = int(user_driverLicense) #address = user_address ) a.put() self.redirect('/your_contract?person=%s&nacionality=%s&profession=%s&maritalStatus=%s&SSN=%s&driverLicense=%s&email=%s&witness=%s&owner=%s&contractType=%s&address=%s&numBook=%s&numInitialPage=%s&numFinalPage=%s' % (person_name, user_nacionality, user_profession, user_maritalStatus, user_SSN, user_driverLicense, user_email, witness, owner, contractType, address, numBook, numInitialPage, numFinalPage)) else: if not person_name or not valid_person(person_name): person_error = "Oh no!!! this person name isn't valid!" if not user_SSN or not valid_SSN(user_SSN): SSN_error = "Oh no!!! SSN isn't valid!" if user_email and not valid_email(user_email): geted_email_error = "Oh no!!! e-mail isn't valid!" template_values = {"person": person_name, "nacionality": user_nacionality, "maritalStatus": user_maritalStatus, "profession": user_profession, "SSN": user_SSN, "driverLicense": user_driverLicense, "email": user_email, "person_error": person_error, "SSN_error": SSN_error, "driverLicense_error": user_driverLicense, "address": address, "email_error": geted_email_error} template = jinja_environment.get_template('index.html') self.response.out.write(template.render(template_values)) class your_contractHandler(webapp2.RequestHandler): def get(self): geted_person_name = self.request.get('person') geted_user_nacionality = self.request.get("nacionality") geted_user_profession = self.request.get("profession") geted_user_maritalStatus = self.request.get("maritalStatus") geted_user_SSN = self.request.get('SSN') geted_user_email = self.request.get('email') geted_user_driverLicense = self.request.get('driverLicense') geted_person_error = "" geted_SSN_error = "" geted_driverLicense_error = "" geted_address = self.request.get('address') geted_owner = self.request.get("owner") geted_witness = self.request.get("witness") geted_contractType = self.request.get("contractType") geted_dateToday = dateToday() your_contract = jinja_environment.get_template('your_contract.html') geted_numBook = self.request.get('numBook') geted_numInitialPage = self.request.get('numInitialPage') geted_numFinalPage = self.request.get('numFinalPage') ## updateNumBookPg(geted_numBook, geted_numInitialPage, geted_numFinalPage) your_contract_values = {"person":geted_person_name, "nacionality":geted_user_nacionality, "maritalStatus": geted_user_maritalStatus, "profession": geted_user_profession, "SSN":geted_user_SSN, "driverLicense":geted_user_driverLicense, "address":geted_address, "email":geted_user_email, "contractType":geted_contractType, "dateContract":geted_dateToday, "numBook":geted_numBook, "numInitialPage":geted_numInitialPage, "numFinalPage":geted_numInitialPage, } template = jinja_environment.get_template('index.html') self.response.out.write(your_contract.render(your_contract_values)) class Person(db.Model): firstName = db.StringProperty(required = True) # lastName = db.StringProperty(required = True) nacionality = db.StringProperty(required = True) maritalStatus = db.StringProperty(required = True) profession = db.StringProperty(required = True) SSN = db.IntegerProperty(required = True) driverLicense = db.IntegerProperty(required = True) # address = db.PostalAdressProperty(required = True) # condition = db.StringProperty(required = False, choices=set(["buyer", "seller", "renter", "owner", "witness"])) ##class Acts(db.Model): ## firstPart = db.StringProperty(required = True) ## secondPart = db.StringProperty(required = True) ## contractNumber = db.IntegerProperty(required = False) ## contractDate = db.DateProperty(auto_now_add=True) ## contractDraft = db.TextProperty(required = True) app = webapp2.WSGIApplication([('/', MainHandler), ('/your_contract', your_contractHandler)], debug=True) 

2 Solutions collect form web for “Warum diese HTML-Vorlage auf jinja2 / python produziert 'u ()' Tupel-Fehler in String-Substitution?”

 numBook = self.request.get("numBook"), numInitialPage = self.request.get("numInitialPage"), numFinalPage = self.request.get("numFinalPage"), 

Wie Woobles Kommentar sagt, sollten diese Zeilen nicht mit Kommas enden.

Es sieht so aus, als hättest du ein Tupel. Sie können einfach den 0. Index verwenden.

Mögen

 <td class="label"> Contract number </td> <td> <input type="text" name="numBook" value="{{numBook.0}}"> Page <input type="text" name="numInitialPage" value="{{numInitialPage.0}}"> to <input type="text" name="numFinalPage" value="{{numFinalPage.0}}"> </td> 
  • Python Rückgängig Unicode Frage
  • UTF-8 Kodierung in Python
  • Wie gibt man eine utf-8 String-Liste wie es in Python ist?
  • Arbeiten mit UTF-8 in Python
  • Python 2.7, Appengine Data Store und Unicode
  • SQLAlchemy Text Zusammenpassende Daten im JSON-Feld mit UTF-8
  • Warum Zeichen ID 160 wird nicht als Unicode in PDFMiner erkannt?
  • Decodierungsprobleme in Django und lxml
  • Ist Django doppelkodiert eine Unicode (utf-8?) String?
  • Scrapy xpath utf-8 Literale
  • UTF-8 und os.listdir ()
  • Python ist die beste Programmiersprache der Welt.