1

再会!

我正在使用带有 python 代码的谷歌应用程序引擎并且出现问题

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'

尽管代码看起来不错,但我不知道出了什么问题。任何想法如何解决它?

4

1 回答 1

4

看看你的 Cat 课程。

date_of_birth 属性定义不正确,没有 ()。这解释了错误,因为一元运算符不适用于该类,它需要该属性的实例。

所以而不是date_of_birth = ndb.DateProperty 它应该看起来像date_of_birth = ndb.DateProperty()

于 2013-08-09T12:48:11.453 回答