0

我目前正在使用 Google App Engine 数据存储,并尝试使用 GQL 从我的应用程序数据库中提取一些数据。

import os
import re
import random
import hashlib
import hmac
from string import letters

import webapp2
import jinja2

from google.appengine.ext import db

template_dir = os.path.join(os.path.dirname(__file__), 'templates')
jinja_env = jinja2.Environment(loader = jinja2.FileSystemLoader(template_dir), autoescape=True)

class Handler(webapp2.RequestHandler):
    def write(self, *a, **kw):
        self.response.out.write(*a, **kw)
    def render_str(self, template, **params):
        t = jinja_env.get_template(template)
        return t.render(params)
    def render(self, template, **kw):
        self.write(self.render_str(template, **kw))

class Users(db.Model):
    name = db.StringProperty(required = True)
    password = db.StringProperty(required = True)
    email = db.EmailProperty(required = True)
    address = db.PostalAddressProperty(required = True)
    join_date = db.DateTimeProperty(auto_now_add = True)
    dob = db.DateTimeProperty()
    phone = db.PhoneNumberProperty()

class MainPage(Handler):
    def get(self):
        self.render("signup-form.html")
    def post(self):
        name = self.request.get("username")
        password = self.request.get("password")
        verify = self.request.get("verify")
        email = self.request.get("email")
        address = self.request.get("address")
        phone = self.request.get("phone")

        a = Users(name = name, password = password, email = email, address = address, phone = phone)
        a.put()
        p = Users.all()
        ans = p.filter("name = :1", "Manas")
        if(ans.email == "manas.oid@gmail.com"):
            self.response.write("Email found")
        else:
            self.response.write("Not found")


app = webapp2.WSGIApplication([('/', MainPage)], debug=True)

现在,我的 Users 表中已经存储了一个“Manas”属性,其中“manas.oid@gmail.com”作为其对应的电子邮件 ID。因此,相应地,我应该会显示“找到电子邮件”消息。但是,我收到此错误:

Traceback (most recent call last):
  File "C:\Program Files\Google\google_appengine\lib\webapp2-2.5.2\webapp2.py", line 1535, in __call__
    rv = self.handle_exception(request, response, e)
  File "C:\Program Files\Google\google_appengine\lib\webapp2-2.5.2\webapp2.py", line 1529, in __call__
    rv = self.router.dispatch(request, response)
  File "C:\Program Files\Google\google_appengine\lib\webapp2-2.5.2\webapp2.py", line 1278, in default_dispatcher
    return route.handler_adapter(request, response)
  File "C:\Program Files\Google\google_appengine\lib\webapp2-2.5.2\webapp2.py", line 1102, in __call__
    return handler.dispatch()
  File "C:\Program Files\Google\google_appengine\lib\webapp2-2.5.2\webapp2.py", line 572, in dispatch
    return self.handle_exception(e, self.app.debug)
  File "C:\Program Files\Google\google_appengine\lib\webapp2-2.5.2\webapp2.py", line 570, in dispatch
    return method(*args, **kwargs)
  File "F:\Python 2.7\engineapp1\HelloApp\appapp\main.py", line 48, in post
    ans = p.filter("name = :1", "Manas")
  File "C:\Program Files\Google\google_appengine\google\appengine\ext\db\__init__.py", line 2560, in filter
    prop = match.group(1)
AttributeError: 'NoneType' object has no attribute 'group'

我的查询似乎有什么问题?

4

1 回答 1

1

Query.filter不接受占位符。

替换以下行:

ans = p.filter("name = :1", "Manas")

和:

ans = p.filter("name =", "Manas")

更新

filter返回一个可迭代的,而不是一条记录。您需要对其进行迭代以检查给定的电子邮件是否存在:

for x in ans:
    if x.email == "manas.oid@gmail.com":
        self.response.write("Email found")
else:
    self.response.write("Not found")

更优选地,使用链式Query.filterand Query.count,您不需要迭代它。

ans = p.filter("name =", "Manas").filter("email =", "manas.oid@gmail.com")
if ans.count(limit=1):
    self.response.write("Email found")
else:
    self.response.write("Not found")
于 2014-02-05T15:18:58.873 回答