美好的一天!
我正在使用带有python代码的google app引擎并且出现了问题
helloworld.py文件:
import os
import urllib
import jinja2
import webapp2
import datetime
from google.appengine.api import users
from google.appengine.ext import ndb
JINJA_ENVIRONMENT = jinja2.Environment(
loader=jinja2.FileSystemLoader(os.path.dirname(__file__)),
extensions=['jinja2.ext.autoescape'])
class MainPage(webapp2.RequestHandler):
def get(self):
user = users.get_current_user()
if user:
template_values = {
'nickname' : user.nickname(),
}
template = JINJA_ENVIRONMENT.get_template('index.html')
self.response.write(template.render(template_values))
else:
self.redirect(users.create_login_url(self.request.uri))
class Tutorial6(webapp2.RequestHandler):
def get(self):
numbers = []
for i in range(10):
numbers.append(i)
template_values = {
'numbers': numbers
}
template = JINJA_ENVIRONMENT.get_template('/templates/tutorial6.html')
self.response.write(template.render(template_values))
class Tutorial7(webapp2.RequestHandler):
def get(self):
if users.get_current_user():
url = users.create_logout_url(self.request.uri)
url_linktext = 'Logout'
else:
url = users.create_login_url(self.request.uri)
url_linktext = 'Login'
cats_query = Cat.query().order(-Cat.date_of_birth)
cats = cats_query.fetch(10)
template_values = {
'cats': cats,
'url': url,
'url_linktext': url_linktext,
}
template = JINJA_ENVIRONMENT.get_template('/templates/tutorial7.html')
self.response.write(template.render(template_values))
def post(self):
user = users.get_current_user()
if user:
cat = Cat()
cat.owner = user
cat.name = self.request.get('name')
cat.description = self.request.get('description')
day = self.request.get('day')
month = self.request.get('month')
year = self.request.get('year')
cat.date_of_birth = datetime.date(year=int(year), month=int(month), day=int(day))
cat.put()
self.redirect('/tutorial7')
else:
self.redirect(users.create_login_url(self.request.uri))
class Cat(ndb.Model):
owner = ndb.UserProperty()
name = ndb.StringProperty()
date_of_birth = ndb.DateProperty
description = ndb.StringProperty(indexed=False)
application = webapp2.WSGIApplication([
('/', MainPage),
('/tutorial6', Tutorial6),
('/tutorial7', Tutorial7),
], debug=True)
当我在本地服务器上测试它时,它给出了以下错误消息:
File "/home/RMITVNNET/s3408675/Desktop/.HDrive/s3372661-s3408675/helloworld.py", line 58, in get
cats_query = Cat.query().order(-Cat.date_of_birth)
TypeError: bad operand type for unary -: 'type'
虽然代码看起来很好,但我不知道出了什么问题。任何想法如何解决它?
答案 0 :(得分:3)
看看你的Cat课程。
date_of_birth属性未正确定义,no()。这解释了错误,因为一元运算符不能与类一起使用,它需要属性的实例。
所以而不是date_of_birth = ndb.DateProperty
它应该看起来像date_of_birth = ndb.DateProperty()