- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
这行代码:
geted_nickname = user.nickname()
这个处理程序的:
class MainHandler(webapp2.RequestHandler):
def get(self):
user = users.get_current_user()
if user == None:
self.redirect(users.create_login_url(self.request.uri))
geted_nickname = user.nickname()
正在产生此错误:
File "C:\Users\Py\Desktop\Apps\contract\main.py", line 99, in get
geted_nickname = user.nickname()
AttributeError: 'NoneType' object has no attribute 'nickname'
因为 user
是一个 NoneType 对象,程序应该执行 if block self.redirect(users.create_login_url(self.request.uri))
,但这是正在发生。为什么?如何解决?
感谢您的帮助!
这里是完整的代码:
# -*- 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.
#
from google.appengine.api import users
from google.appengine.ext.webapp.util import run_wsgi_app
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)
clauses = {u'Software development agreement':"Don't be evil", 'Car Rental Contract':"non-skid the tires", 'House Rental Contract':"Don't break stuff"}
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):
person_name = 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.address = db.PostalAddressProperty(required = True)
class Contract(db.Model):
book_number = db.IntegerProperty(required = True)
initial_page = db.IntegerProperty(required = True)
final_page = db.IntegerProperty(required = True)
contract_type = db.StringProperty(required = True)
date = db.DateProperty (required = True, auto_now = True, auto_now_add = True)
class RegisteredUser(db.Model):
user_name = db.UserProperty()
user_nickname = db.StringProperty()
name = db.StringProperty()
user_position = db.StringProperty()
org_name = db.StringProperty()
org_address = db.StringProperty()
org_city = db.StringProperty()
org_state = db.StringProperty()
class ContractingParty(db.Model):
person = db.ReferenceProperty(Person, required=True, collection_name="party_to_contracts")
contract = db.ReferenceProperty(Contract, required=True)
condition = db.StringProperty(required = True, choices=set(["active position", "passive position", "witness"]))
class MainHandler(webapp2.RequestHandler):
def get(self):
user = users.get_current_user()
if user == None:
self.redirect(users.create_login_url(self.request.uri))
geted_nickname = user.nickname()
registeredUser = db.GqlQuery("SELECT * FROM RegisteredUser WHERE user_nickname =:nickname", nickname = geted_nickname).get()
if registeredUser is None:
self.redirect('/register?user_nickname=%s' % (geted_nickname))
contract = db.GqlQuery("SELECT * FROM Contract ORDER BY date DESC").get()
if contract == None:
numBook = 1
numInitialPage = 1
numFinalPage = 1
else:
numBook = contract.book_number
numInitialPage = contract.final_page +1
numFinalPage = numInitialPage
template_values = {"numBook": numBook,
"numInitialPage": numInitialPage,
"numFinalPage": numFinalPage}
template = jinja_environment.get_template('index.html')
self.response.out.write(template.render(template_values))
def post(self):
choosed_contract_type = self.request.get("contract_type")
numBook = self.request.get("numBook")
numInitialPage = self.request.get("numInitialPage")
numFinalPage = self.request.get("numFinalPage")
person_name = self.request.get("person_name")
user_nacionality = self.request.get('nacionality')
user_profession = self.request.get('profession')
user_marital_status = self.request.get('marital_status')
user_driver_license = self.request.get('driver_license')
driver_license_error = ''
user_SSN = self.request.get('SSN')
SSN_error = ""
address = self.request.get('address')
person_name2 = self.request.get("person_name2")
user_nacionality2 = self.request.get('nacionality2')
user_profession2 = self.request.get('profession2')
user_marital_status2 = self.request.get('marital_status2')
user_driver_license2 = self.request.get('driver_license2')
user_SSN2 = self.request.get('SSN2')
SSN_error2 = ""
address2 = self.request.get('address2')
bsubmit = self.request.get('bsubmit')
if (bsubmit == 'Submit Contract') and (person_name and valid_person(person_name)) and (user_SSN and valid_SSN(user_SSN)):
contract_record = Contract(book_number = int(numBook),
initial_page = int(numInitialPage),
final_page = int(numFinalPage),
contract_type = choosed_contract_type)
contract_record.put()
contract_id = contract_record.key().id()
person_record = Person(person_name = person_name,
nacionality = user_nacionality,
profession = user_profession,
marital_status = user_marital_status,
driver_license = int(user_driver_license),
SSN = int(user_SSN),
address = address)
person_record.put()
person_id = person_record.key().id()
person_record2 = Person(person_name = person_name2,
nacionality = user_nacionality2,
profession = user_profession2,
marital_status = user_marital_status2,
driver_license = int(user_driver_license2),
SSN = int(user_SSN2),
address = address2)
person_record2.put()
person_id2 = person_record2.key().id()
self.redirect('/your_contract?contract_id=%s&person_id=%s&person_id2=%s' % (contract_id, person_id, person_id2))
else:
if not person_name or not valid_person(person_name):
person_name_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!"
template_values = {"person_name": person_name,
"nacionality": user_nacionality,
"marital_status": user_marital_status,
"profession": user_profession,
"SSN": user_SSN,
"driver_license": user_driver_license,
## "email": user_email,
"person_name_error": person_name_error,
"SSN_error": SSN_error,
"driver_license_error": user_driver_license,
"address": address,
"person_name2":person_name2,
"nacionality2":user_nacionality2,
"marital_status2":user_marital_status2,
"profession2":user_profession2,
"driver_license2":user_driver_license2,
"SSN2":user_SSN2,
"address2":user_address2,
"contract_type":choosed_contract_type,
"numBook":geted_numBook,
"numInitialPage":geted_numInitialPage,
"numFinalPage":geted_numInitialPage,
}
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 = Contract.get_by_id(int(contract_id))
geted_contract_type = contract.contract_type
geted_clauses = clauses[geted_contract_type]
geted_numBook = contract.book_number
geted_numInitialPage = contract.initial_page
geted_numFinalPage = contract.final_page
geted_dateToday = dateToday()
user = users.get_current_user()
geted_nickname = user.nickname()
registered_user = db.GqlQuery("SELECT * FROM RegisteredUser WHERE user_nickname =:nickname", nickname = geted_nickname).get()
geted_autor_ato = registered_user.user_name
user_position = registered_user.user_position
org_name = registered_user.org_name
org_city = registered_user.org_city
org_state = registered_user.org_state
person_id = self.request.get('person_id')
person = Person.get_by_id(int(person_id))
geted_person_name = person.person_name
geted_user_nacionality = person.nacionality
geted_user_profession = person.profession
geted_user_marital_status = person.marital_status
geted_user_driver_license = person.driver_license
geted_user_SSN = person.SSN
geted_user_address = person.address
person_id2 = self.request.get('person_id2')
person2 = Person.get_by_id(int(person_id2))
geted_person_name2 = person2.person_name
geted_user_nacionality2 = person2.nacionality
geted_user_profession2 = person2.profession
geted_user_marital_status2 = person2.marital_status
geted_user_driver_license2 = person2.driver_license
geted_user_SSN2 = person2.SSN
geted_user_address2 = person2.address
your_contract = jinja_environment.get_template('your_contract.html')
your_contract_values = {"autor_ato":geted_autor_ato,
"user_position":user_position,
"org_name":org_name,
"org_city":org_city,
"org_state":org_state,
"contract_type":geted_contract_type,
"clauses":geted_clauses,
"dateContract":geted_dateToday,
"numBook":geted_numBook,
"numInitialPage":geted_numInitialPage,
"numFinalPage":geted_numInitialPage,
"person_name":geted_person_name,
"nacionality":geted_user_nacionality,
"marital_status":geted_user_marital_status,
"profession": geted_user_profession,
"driver_license":geted_user_driver_license,
"SSN":geted_user_SSN,
"address":geted_user_address,
"person_name2":geted_person_name2,
"nacionality2":geted_user_nacionality2,
"marital_status2":geted_user_marital_status2,
"profession2": geted_user_profession2,
"driver_license2":geted_user_driver_license2,
"SSN2":geted_user_SSN2,
"address2":geted_user_address2,
}
template = jinja_environment.get_template('index.html')
self.response.out.write(your_contract.render(your_contract_values))
class RegisterHandler(webapp2.RequestHandler):
def get(self):
user = users.get_current_user()
if not user:
self.redirect(users.create_login_url(self.request.uri))
nickname = user.nickname()
template_values = {"user_nickname": nickname}
template = jinja_environment.get_template('register.html')
self.response.out.write(template.render(template_values))
def post(self):
user = users.get_current_user()
geted_user_nickname = user.nickname()
geted_user_name = self.request.get("user_name")
geted_user_position = self.request.get('position')
geted_org_name = self.request.get('org_name')
geted_org_address = self.request.get('org_adress')
geted_address = self.request.get('address')
geted_city = self.request.get('city')
registered_user = RegisteredUser(user = user,
user_name = geted_user_name,
user_nickname = geted_user_nickname,
position = geted_user_position,
org_name = geted_org_name,
org_address = geted_org_address,
city = geted_city,
)
registered_user.put()
## registered_user_id = registered_user.key().id()
self.redirect('/')
app = webapp2.WSGIApplication([('/', MainHandler), ('/your_contract', your_contractHandler), ('/register', RegisterHandler)],
debug=True)
最佳答案
根据docs调用 redirect
不会停止代码执行,除非 abort
设置为 True
。它建议返回重定向的结果,例如:
return redirect('/some-path')
在您的例子中,您的方法一直在执行,user
(即 None
)正在查找一个不存在的属性。
关于python - 使用 get_current_user() 方法时产生此 python AttributeError 的原因是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11837693/
SQLite、Content provider 和 Shared Preference 之间的所有已知区别。 但我想知道什么时候需要根据情况使用 SQLite 或 Content Provider 或
警告:我正在使用一个我无法完全控制的后端,所以我正在努力解决 Backbone 中的一些注意事项,这些注意事项可能在其他地方更好地解决......不幸的是,我别无选择,只能在这里处理它们! 所以,我的
我一整天都在挣扎。我的预输入搜索表达式与远程 json 数据完美配合。但是当我尝试使用相同的 json 数据作为预取数据时,建议为空。点击第一个标志后,我收到预定义消息“无法找到任何内容...”,结果
我正在制作一个模拟 NHL 选秀彩票的程序,其中屏幕右侧应该有一个 JTextField,并且在左侧绘制弹跳的选秀球。我创建了一个名为 Ball 的类,它实现了 Runnable,并在我的主 Draf
这个问题已经有答案了: How can I calculate a time span in Java and format the output? (18 个回答) 已关闭 9 年前。 这是我的代码
我有一个 ASP.NET Web API 应用程序在我的本地 IIS 实例上运行。 Web 应用程序配置有 CORS。我调用的 Web API 方法类似于: [POST("/API/{foo}/{ba
我将用户输入的时间和日期作为: DatePicker dp = (DatePicker) findViewById(R.id.datePicker); TimePicker tp = (TimePic
放宽“邻居”的标准是否足够,或者是否有其他标准行动可以采取? 最佳答案 如果所有相邻解决方案都是 Tabu,则听起来您的 Tabu 列表的大小太长或您的释放策略太严格。一个好的 Tabu 列表长度是
我正在阅读来自 cppreference 的代码示例: #include #include #include #include template void print_queue(T& q)
我快疯了,我试图理解工具提示的行为,但没有成功。 1. 第一个问题是当我尝试通过插件(按钮 1)在点击事件中使用它时 -> 如果您转到 Fiddle,您会在“内容”内看到该函数' 每次点击都会调用该属
我在功能组件中有以下代码: const [ folder, setFolder ] = useState([]); const folderData = useContext(FolderContex
我在使用预签名网址和 AFNetworking 3.0 从 S3 获取图像时遇到问题。我可以使用 NSMutableURLRequest 和 NSURLSession 获取图像,但是当我使用 AFHT
我正在使用 Oracle ojdbc 12 和 Java 8 处理 Oracle UCP 管理器的问题。当 UCP 池启动失败时,我希望关闭它创建的连接。 当池初始化期间遇到 ORA-02391:超过
关闭。此题需要details or clarity 。目前不接受答案。 想要改进这个问题吗?通过 editing this post 添加详细信息并澄清问题. 已关闭 9 年前。 Improve
引用这个plunker: https://plnkr.co/edit/GWsbdDWVvBYNMqyxzlLY?p=preview 我在 styles.css 文件和 src/app.ts 文件中指定
为什么我的条形这么细?我尝试将宽度设置为 1,它们变得非常厚。我不知道还能尝试什么。默认厚度为 0.8,这是应该的样子吗? import matplotlib.pyplot as plt import
当我编写时,查询按预期执行: SELECT id, day2.count - day1.count AS diff FROM day1 NATURAL JOIN day2; 但我真正想要的是右连接。当
我有以下时间数据: 0 08/01/16 13:07:46,335437 1 18/02/16 08:40:40,565575 2 14/01/16 22:2
一些背景知识 -我的 NodeJS 服务器在端口 3001 上运行,我的 React 应用程序在端口 3000 上运行。我在 React 应用程序 package.json 中设置了一个代理来代理对端
我面临着一个愚蠢的问题。我试图在我的 Angular 应用程序中延迟加载我的图像,我已经尝试过这个2: 但是他们都设置了 src attr 而不是 data-src,我在这里遗漏了什么吗?保留 d
我是一名优秀的程序员,十分优秀!