Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Google App Engine: TypeError: 'tuple' object is not callable

Currently trying to create a basic blog using Google App Engine in Python. Here's the python code that I am using:

import os
import re
import webapp2
import jinja2
from string import letters
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))

def post_key(name = "dad"):
    return db.Key.from_path('blog', name)

class Blogger(db.Model):
    name = db.StringProperty()
    content = db.TextProperty()
    created = db.DateTimeProperty(auto_now_add = True)

    def render(self):
        self._render_text = self.content.replace('\n', '<br>')
        return render_str("post.html", p = self)

class MainPage(Handler):
    def get(self):
        self.response.write("Visit our blog")

class BlogHandler(Handler):
    def get(self):
        posts = db.GqlQuery("SELECT * FROM Blogger order by created desc")
        self.render("frontblog.html", posts = posts)    

class SubmitHandler(Handler):
    def get(self):
        self.render("temp.html")    
    def post(self):
        name = self.request.get("name")
        content = self.request.get("content")
        if name and content:
            a = Blogger(parent = post_key(), name = name, content = content)
            a.put()
            self.redirect('/blog/%s' % str(a.key().id()))
        else:
            error = "Fill in both the columns!"
            self.render("temp.html", name = name, content = content, error = error)        

class DisplayPost(Handler):
    def get(self, post_id):
        po = Blogger.get_by_id(post_id)
        if po:
            self.render("perma.html", po = po)
        else:
            self.response.write("404 Error")        

app = webapp2.WSGIApplication([('/', MainPage),
                              ('/blog', BlogHandler), 
                              ('/blog/submit', SubmitHandler)
                              ('/blog/([0-9]+)', DisplayPost)], debug=True)

However, when I try to run this code on my local server, this is what I get as an error:

File "F:\Python 2.7\engineapp1\HelloApp\appapp\main.py", line 66, in <module>

    ('/blog/([0-9]+)', DisplayPost)], debug=True)

TypeError: 'tuple' object is not callable

What seems to be the problem here?

like image 671
Manas Chaturvedi Avatar asked Apr 26 '26 09:04

Manas Chaturvedi


2 Answers

You forgot to add a comma.

 ('/blog/submit', SubmitHandler)   <---- missed comma over here
 ('/blog/([0-9]+)', DisplayPost)], debug=True)

It's acting like a function in this case, you're passing a parameter to a tuple which result in an error that the tuple is not callable.

('/blog/submit', SubmitHandler)(parameter)

There's a missing comma at the line:

('/blog/submit', SubmitHandler)

It should be:

('/blog/submit', SubmitHandler),

Without the comma, you have ('/blog/submit', SubmitHandler)('/blog/([0-9]+)', DisplayPost), which is trying to call ('/blog/submit', SubmitHandler) as a function, with '/blog/([0-9]+)' and DisplayPost as parameters. Since that's not a function, but a tuple, you get that error.

like image 39
Pedro Werneck Avatar answered Apr 28 '26 21:04

Pedro Werneck



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!