我正在尝试在留言簿上实现一个删除按钮,用户可以根据需要删除他们的评论并收到以下错误。我是GAE / python的新手,我们将非常感谢您提供的任何帮助。我的环境是在Windows 8上运行的python 2.7 api版本1。
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 "D:\Projects\python\guestideabook\guestideabook.py", line 87, in post
item = ndb.get(key)
AttributeError: 'module' object has no attribute 'get'
源代码: guestideabook.py
import jinja2
import os
import webapp2
import datetime
from google.appengine.api import users
from google.appengine.ext import ndb
# We set a parent key on the 'Greetings' to ensure that they are all in the same
# entity group. Queries across the single entity group will be consistent.
# However, the write rate should be limited to ~1/second.
def Guestideabook_key(Guestideabook_name='default_Guestideabook'):
return ndb.Key('Guestideabook', Guestideabook_name)
jinja_environment = jinja2.Environment(
loader=jinja2.FileSystemLoader(os.path.dirname(__file__)),
extensions=['jinja2.ext.autoescape'],
autoescape=True)
class Greeting(ndb.Model):
author = ndb.UserProperty()
content = ndb.StringProperty(indexed=False)
date = ndb.DateTimeProperty(auto_now_add=True)
class MainPage(webapp2.RequestHandler):
def get(self):
greetings_query = Greeting.query(ancestor=Guestideabook_key()).order(-Greeting.date)
greetings = greetings_query.fetch(10)
if users.get_current_user():
url = users.create_logout_url(self.request.uri)
url_linktext = 'Logout'
#print ("Current date & time = %s" % i)e
else:
url = users.create_login_url(self.request.uri)
url_linktext = 'Login'
template = jinja_environment.get_template('index.html')
self.response.out.write(template.render(greetings=greetings,
url=url,
url_linktext=url_linktext))
class Guestideabook(webapp2.RequestHandler):
def post(self):
greeting = Greeting(parent=Guestideabook_key())
if users.get_current_user():
greeting.author = users.get_current_user()
greeting.content = self.request.get('content')
greeting.put()
self.redirect('/')
class DelHandler(webapp2.RequestHandler):
def post(self):
key = self.request.get('content')
item = ndb.get(key)
n = item.content.key()
ndb.delete(n)
item.delete()
self.redirect('/')
application = webapp2.WSGIApplication([
('/', MainPage),
('/sign', Guestideabook),
('/delete', DelHandler)
], debug=True)
的index.html
<!DOCTYPE html>
{% autoescape true %}
<html>
<head>
<link type="text/css" rel="stylesheet" href="/bootstrap/css/bootstrap.css" rel="stylesheet">
<link type="text/css" rel="stylesheet" href="/bootstrap/css/bootstrap-responsive.css" rel="stylesheet">
<style type="text/css">
body {
padding-top: 40px;
padding-bottom: 40px;
background-color: #f5f5f5;
}
blockquote {
margin-bottom: 10px;
border-left-color: #ccc;
}
form {
margin-top: 10px;
}
.form-signin input[type="text"] {
font-size: 16px;
height: auto;
margin-bottom: 15px;
padding: 7px 9px;
}
.row {
margin-left: 0px;
margin-top: 10px;
overflow: scroll;
}
</style>
</head>
<body>
<div class="navbar navbar-inverse navbar-fixed-top">
<div class="navbar-inner">
<div class="container">
<button type="button" class="btn btn-navbar" data-toggle="collapse" data-target=".nav-collapse">
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
<a class="brand" href="#">App Engine Ideabook</a>
<div class="nav-collapse collapse pull-right">
<a href="{{ url|safe }}" class="btn">{{ url_linktext }}</a>
</div>
</div>
</div>
</div>
<div class="container">
{% for greeting in greetings %}
<div class="row">
{% if greeting.author %}
<b>{{ greeting.author.nickname() }}</b> wrote:
{% else %}
An anonymous person wrote:
{% endif %}
<blockquote>{{ greeting.content }} </blockquote>
<div><form action="/delete" method="post" >
<input type="submit" name= "delete" class= "delbtn" value="Delete comment">
</form></div>
<blockquote>Date:{{ greeting.date }} </blockquote>
</div>
{% endfor %}
<form action="/sign" method="post">
<div><textarea name="content" class="input-block-level" rows="3"></textarea></div>
<div><input type="submit" class="btn btn-large btn-primary" value="Submit Idea"></div>
</form>
</div>
</body>
</html>
{% endautoescape %}
答案 0 :(得分:3)
这是不正确的
item = ndb.get(key)
应该是这样的:
item = key.get()
请参阅:https://cloud.google.com/appengine/docs/python/ndb/entities#retrieving_entities
答案 1 :(得分:0)
你目前有
key = self.request.get('content')
item = ndb.get(key)
我假设key是实体键。如果是这样,那么你应该做
item = key.get()
文档说 - 要从其键中检索实体,请调用Key对象的get()方法documentation - Retrieving Entities from Keys
但是,根据您对@Paul Collingwood的回复,看起来你所拥有的不是Key而是Id,在这种情况下获取密钥,你应该这样做
key = Key('Greeting', key)
要删除(当不再需要实体时,可以使用密钥的delete()方法将其从数据存储中删除:documentation - Deleting entities
key.delete()