0

I am trying to implement a delete button on a guestbook in which users will be able to remove their comments if they wish and am getting the error below. I am new to GAE/python and any help provided will be greatly appreciated. My environment is python 2.7 api version 1 running on windows 8.

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'

Source code: 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 %}

2 Answers 2

3

This is incorrect

item = ndb.get(key)

It should be this:

item = key.get()

See this: https://cloud.google.com/appengine/docs/python/ndb/entities#retrieving_entities

Sign up to request clarification or add additional context in comments.

2 Comments

I tried that but I am getting this error>>> AttributeError: 'str' object has no attribute 'get'
Then you no doubt don't have a key, but a string representation of a key. Have a look at the answer to this question: stackoverflow.com/questions/16834797/…
0

You currently have

key = self.request.get('content')
item = ndb.get(key)

I assume key is the entity key. If so, then you should be doing

item = key.get()

The documentation says - To retrieve an entity from its key, call the Key object's get() method documentation - Retrieving Entities from Keys

However, from your response to @Paul Collingwood, it looks like what you have is not the Key but the Id in which case to get the key, you should do

key = Key('Greeting', key)

And to delete (When an entity is no longer needed, you can remove it from the data store with the key's delete() method: documentation - Deleting entities

key.delete()

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.