Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/341.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 查询数据存储:';非类型';对象没有属性_Python_Google App Engine_Google Cloud Datastore_Gqlquery - Fatal编程技术网

Python 查询数据存储:';非类型';对象没有属性

Python 查询数据存储:';非类型';对象没有属性,python,google-app-engine,google-cloud-datastore,gqlquery,Python,Google App Engine,Google Cloud Datastore,Gqlquery,当我运行此if块时: first_query = db.GqlQuery("SELECT * FROM Contract ORDER BY date DESC").get() if first_query == None: numBook = 42 numInitialPage = 42 numFinalPage = 42 first_record = Contract(book_number = 1, initial_pa

当我运行此if块时:

first_query = db.GqlQuery("SELECT * FROM Contract ORDER BY date DESC").get()
    if first_query == None:
        numBook = 42
        numInitialPage = 42
        numFinalPage = 42
        first_record = Contract(book_number = 1, initial_page = 0, final_page = 0)
        first_record.put()
        contract = db.GqlQuery("SELECT * FROM Contract ORDER BY date DESC").get()
        numBook = contract.book_number
        numInitialPage = contract.final_page +1
        numFinalPage = numInitialPage
    else:
        contract = first_query
        numBook = first_query
        numInitialPage = 51
        numFinalPage = 51
该过程跳入
if
块,但随后,我收到以下错误消息:

    File "C:\Users\CG\Documents\udacity\contract\main.py", line 88, in get
    numBook = contract.book_number
AttributeError: 'NoneType' object has no attribute 'book_number'
既然我使用
.put()
插入一条记录,而
.get()
确实访问了它,为什么合同的价值是

提前感谢您的帮助

下面是整个代码:

# -*- 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 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 Person(db.Model):
    firstName = db.StringProperty(required = True)
    nacionality = db.StringProperty(required = True)
    marital_status = db.StringProperty(required = True)
    profession = db.StringProperty(required = True)
    SSN = db.IntegerProperty(required = True)
    driver_license = db.IntegerProperty(required = True)
#    address = db.PostalAddress(required = False)

class Contract(db.Model):
  book_number = db.IntegerProperty(required = True)
  initial_page = db.IntegerProperty(required = True)
  final_page = db.IntegerProperty(required = True)
##  contrac_type = db.StringProperty(required = False, choices=set(["Purchase Agreement", "Rental House", "Rental Car"]))
##  contract_place = db.StringProperty(required = False)
##  contract_date = db.DateProperty (required = True, auto_now = True, auto_now_add = True)

class ContractingParty(db.Model):
  person = db.ReferenceProperty(Person, required=True, collection_name="party_to_contracts")
  condition = db.StringProperty(required = False, choices=set(["buyer", "seller", "renter", "owner", "witness"]))

class MainHandler(webapp2.RequestHandler):
    def get(self):
        first_query = db.GqlQuery("SELECT * FROM Contract ORDER BY date DESC").get()
        if first_query == None:
            numBook = 8
            numInitialPage = 8
            numFinalPage = 8
            first_record = Contract(book_number = 1, initial_page = 0, final_page = 0)
            first_record.put()
            contract = db.GqlQuery("SELECT * FROM Contract ORDER BY date DESC").get()
            numBook = contract.book_number
            numInitialPage = contract.final_page +1
            numFinalPage = numInitialPage
        else:
            contract = first_query
            numBook = first_query
            numInitialPage = 7
            numFinalPage = 7

##        numBook = contract.book_number
##        numInitialPage = contract.final_page +1
##        numFinalPage = numInitialPage
        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 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))):
            contract_record = Contract(book_number = numBook, initial_page = numInitialPage, final_page = numFinalPage)
            contract_record.put()
            contract_id = contract_record.key().id()
            person_record = Person(name = person_name,
                               nacionality = user_nacionality,
                               profession = user_profession,
                               marital_status = user_maritalStatus,
                               SSN = int(user_SSN),
                               driverLicense = int(user_driverLicense))
            person_record.put()
# HERE THE PROBLEM - Contracting party should be a table with ID refering to contract table
 #       contracting_party_record = ContractingParty(person = 1,  )
##            person = db.ReferenceProperty(People, required=True, collection_name="party_to_contracts")
##            condition = db.StringProperty(required = False, choices=set(["buyer", "seller", "renter", "owner", "witness"]))
##            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))
            self.redirect('/your_contract?person=%s' % contract_id)

        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):
        contract_id = self.request.get('contract_id')
        contract = db.GqlQuery("SELECT * FROM Contract WHERE __key__ = KEY('Model', contract_id").get()
##        SELECT * FROM Model where __key__ = KEY('Model', <numeric_id>)
        geted_numBook = contract.book_number
        geted_numInitialPage = contract.initial_page
        geted_numFinalPage = contract.final_page
        geted_dateToday = dateToday()
        geted_contractType = contract.contract_type

        person = db.GqlQuery("SELECT * FROM Person ORDER BY date DESC").get()
        geted_person_name = person.name
        geted_user_nacionality = person.nacionality
        geted_user_profession = person.profession
        geted_user_maritalStatus = person.marital_status
        geted_user_SSN = person.SSN
        geted_user_driver_license = person.driver_license


        your_contract = jinja_environment.get_template('your_contract.html')

        your_contract_values = {"person":geted_person_name,
                                "nacionality":geted_user_nacionality,
                                "maritalStatus": geted_user_marital_status,
                                "profession": geted_user_profession,
                                "SSN":geted_user_SSN,
                                "driverLicense":geted_user_driver_license,
                                "address":geted_address,
                                "email":geted_user_email,
                                "contractType":geted_contract_type,
                                "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))


# ContractingParty.all().ancestor(thecontract).run()
# contract_id = contract.key().id()
# contract_parties = Contract.get_by_id(contract_id)
#http://stackoverflow.com/questions/11294526/how-to-model-a-contract-database-with-several-buyers-or-sellers-using-gae-data
#http://stackoverflow.com/questions/11311461/how-to-query-gae-datastore-to-render-a-template-newbie-level
app = webapp2.WSGIApplication([('/', MainHandler), ('/your_contract', your_contractHandler)],
                              debug=True)
#-*-编码:utf-8-*-
#-*-编码:utf-8-*-
#!/usr/bin/env python
#
#版权所有2007谷歌公司。
#
#根据Apache许可证2.0版(以下简称“许可证”)获得许可;
#除非遵守许可证,否则不得使用此文件。
#您可以通过以下方式获得许可证副本:
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
#除非适用法律要求或书面同意,软件
#根据许可证进行的分发是按“原样”进行分发的,
#无任何明示或暗示的保证或条件。
#请参阅许可证以了解管理权限和权限的特定语言
#许可证下的限制。
#
导入操作系统
导入webapp2
进口金玉2
jinja_环境=jinja2.environment(自动转义=True,
loader=jinja2.FileSystemLoader(os.path.join(os.path.dirname(_文件,'templates'))
进口稀土
从google.appengine.ext导入数据库
USER_RE=RE.compile(r“^[a-zA-Z0-9_u-]{3,20}$”)
def有效_人员(人员):
返回用户匹配(个人)
PASS_RE=RE.compile(r“^.{3,20}$”)
def有效\u SSN(SSN):
回程通行证重新匹配(SSN)
电子邮件\u RE=RE.compile(r“^[\S]+@[\S]+\.[\S]+$”)
def有效电子邮件(电子邮件):
返回匹配的电子邮件(电子邮件)
导入时间
导入日期时间
def dateToday():
今天=datetime.datetime.today()
todayDay=str(今天.day)
todayMonth=str(今天.month)
Montexht={'1':'1','2':'2','3','4':'4','5':'5','6':'6','7':'7','8':'8','9':'9','9','10':'10','11':'11','12':'12'}
todayYear=str(今天.年)
报税表(今日+月日+月日+月日+年日)
班级人员(db.Model):
firstName=db.StringProperty(必需=True)
nacionality=db.StringProperty(必需=True)
婚姻状况=db.StringProperty(必需=True)
profession=db.StringProperty(必需=True)
SSN=db.IntegerProperty(必需=True)
驾驶执照=db.IntegerProperty(必需=True)
#地址=db.PostalAddress(必需=False)
类别合同(db.Model):
图书编号=db.IntegerProperty(必需=真)
初始页面=db.IntegerProperty(必需=True)
最终页面=db.IntegerProperty(必需=True)
##contrac_type=db.StringProperty(必需=False,选项=set([“购买协议”、“出租房屋”、“出租汽车”]))
##合同地点=db.StringProperty(必需=False)
##合同日期=db.DateProperty(必需=True,自动现在=True,自动现在添加=True)
合同方类别(db.型号):
person=db.ReferenceProperty(person,required=True,collection\u name=“合同方”)
条件=db.StringProperty(必需=False,选项=set([“买方”、“卖方”、“承租人”、“所有者”、“见证人”]))
类MainHandler(webapp2.RequestHandler):
def get(自我):
first_query=db.GqlQuery(“按日期描述从合同订单中选择*).get()
如果第一个查询==无:
numBook=8
numInitialPage=8
numFinalPage=8
第一个记录=合同(书号=1,初始页=0,最终页=0)
第一个记录。放置()
contract=db.GqlQuery(“按日期描述从合同订单中选择*).get()
numBook=contract.book\u编号
numInitialPage=contract.final_第+1页
numFinalPage=numInitialPage
其他:
合同=第一次查询
numBook=第一次查询
numInitialPage=7
numFinalPage=7
##numBook=contract.book\u编号
##numInitialPage=contract.final_第+1页
##numFinalPage=numInitialPage
模板_值={“人”:“”,
“民族性”:“民族性”,
“SSN”:“,
“驾驶员执照”:“,
“电子邮件”:“,
“人员错误”:“,
“SSN_错误”:“”,
“driverLicense_错误”:“”,
“地址”:“地址”,
“电子邮件错误”:“”,
“麻木书”:麻木书,
“numInitialPage”:numInitialPage,
“numFinalPage”:numFinalPage,
}
template=jinja_环境。获取_模板('index.html'))
self.response.out.write(template.render(template_值))
def post(自我):
person\u name=self.request.get(“person”)
user\u nacionality=self.request.get('nacionality'))
user\u profession=self.request.get('profession')
user\u maritalStatus=self.request.get('maritalStatus')
user\u SSN=self.request.get('SSN')
user\u email=self.request.get('email'))
user\u driverLicense=self.request.get('driverLicense'))
person_error=“”
SSN_错误=“”
driverLicense_error=“”
geted_email_error=“”
address=self.request.get('address')
contractType=self.request.get(“contractType”)
所有者=self.request.get(“所有者”)
witness=self.request.get(“witness”)
numBook=self.request.get(“numBook”)
numInitialPage=self.request.get(“numInitialPage”)
numFinalPage=self.request.get(“numFinalPage”)
如果(人名和有效人名)和(用户编号和有效用户编号(用户编号))和((非用户电子邮件)或(用户电子邮件和有效电子邮件(用户电子邮件)):
合同\记录=合同(账簿编号)