views:

1583

answers:

2

So i've just finished the google app engine greetings tutorial. All well so far. I then decided to try and add a new datastore model and then set it in the existing handler. I added a 2nd content field called "content2" and then tried to set it in the handler Guestbook(), but it keeps borking out. I'm sure it will be the silliest error, but I'm stumped atm. Any ideas?

main.py

import cgi

from google.appengine.api import users
from google.appengine.ext import webapp
from google.appengine.ext.webapp.util import run_wsgi_app
from google.appengine.ext import db
import os
from google.appengine.ext.webapp import template

class Greeting(db.Model):
  author = db.UserProperty()
  content = db.StringProperty(multiline=True)
  date = db.DateTimeProperty(auto_now_add=True)
  content2 = db.StringProperty(multiline=True)


class MainPage(webapp.RequestHandler):
  def get(self):
    greetings_query = Greeting.all().order('-date')
    greetings = greetings_query.fetch(1000)

    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'

    template_values = {
      'greetings': greetings,
      'url': url,
      'url_linktext': url_linktext,
      }


    path = os.path.join(os.path.dirname(__file__), 'index.html')
    self.response.out.write(template.render(path, template_values))

class Guestbook(webapp.RequestHandler):
  def post(self):
    greeting = Greeting()

    if users.get_current_user():
      greeting.author = users.get_current_user()

    greeting.content = self.request.get('content')
 greeting.content2 = self.request.get('content')
    greeting.put()
    self.redirect('/')

class HelloWorld(webapp.RequestHandler):
  def get(self):
    self.response.out.write('Hello, webapp World!')



application = webapp.WSGIApplication(
                                     [('/', MainPage),
                                      ('/sign', Guestbook)],
                                     debug=True)

def main():
  run_wsgi_app(application)

if __name__ == "__main__":
  main()

index.html

<html>
  <body>
    {% for greeting in greetings %}
      {% if greeting.author %}
        <b>{{ greeting.author.nickname }}</b> wrote: Dogs name is: {{ pet.name }}
      {% else %}
       An anonymous person wrote:
      {% endif %}
      <blockquote>{{ greeting.content|escape }}</blockquote>
    {% endfor %}

    <form action="/sign" method="post">
      <div><textarea name="content" rows="3" cols="60"></textarea></div>
      <div><input type="submit" value="Sign Guestbook"></div>
    </form>

    <a href="{{ url }}">{{ url_linktext }}</a>

  </body>
</html>
+3  A: 

I can't see anything wrong with your code. Off the top of my head I would say check the white spacing in front of your new item.

Other than that what is the error message shown? The error message that appears in the log is a lot more user-friendly than the browser error message.

AutomatedTester
Ahh didn't think to check the logs on the dashboard. Here is the printout when I uploaded it (was previously just using the dev server):<type 'exceptions.IndentationError'>: unexpected indent (main.py, line 47)<type 'exceptions.IndentationError'>: unexpected indent (main.py, line 47)I'll check for indenting but still :S
Dominic Bou-Samra
I was meaning the logs in your dev environment. The command prompt would show the error like the live dashboard error. If that solved your issue then mark this as answered :)
AutomatedTester
A: 

Yay fixed. Turns out when copying from html to notepad++ it was not copying the tabs properly, and using just 2spaces instead of a tab for indents.

I have been struggling for days with seemingly simple python errors, so perhaps this will cure a few.

Thank you guys - much appreciated.

Dominic Bou-Samra
The Python style guide strongly recommends using spaces instead of tabs: http://www.python.org/dev/peps/pep-0008/
Colonel Sponsz
Also, you might want to mark the question as answered so it helps people in the future.
Federico Builes
Good idea to use spaces instead of tabs for any language.
Phil