Reputation: 181
Good day!
I am using google app engine with python code and a problem occurred
the helloworld.py file:
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)
and when I tested it on local server, it gave this error message:
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'
I don't know what's wrong although the code looks fine. Any ideas how to fix it?
Upvotes: 1
Views: 7871
Reputation: 12986
Have a look at your Cat class.
The date_of_birth property is not defined correctly, no (). This explains the error as the unary operator won't work with the class, it needs an instance of the property.
So rather than date_of_birth = ndb.DateProperty
It should look like date_of_birth = ndb.DateProperty()
Upvotes: 4