librelist archives

« back to archive

Question on add_url_rule and routes

Question on add_url_rule and routes

From:
Harihara Vinayakaram
Date:
2013-07-06 @ 17:31
Hi
   The following code works

app = Flask(__name__)
class MyClass:
     @app.route('/',)
      def hello:
          print 'xxx'
          return 'xxx'

But when I try to do

class = MyClass()
app.add_url_rule('/', 'class.hello', class.hello)

The server starts up but when i hit the URL I get a 500 error . Can you
tell me where I am going wrong and how to debug this  I tried doing getattr
etc but did not succeed .

Thanks for the help

Regards
Hari

Re: [flask] Question on add_url_rule and routes

From:
Steven Kryskalla
Date:
2013-07-06 @ 17:53
On Sat, Jul 6, 2013 at 10:31 AM, Harihara Vinayakaram <hvram1@gmail.com> wrote:
>       def hello:

That is not valid python, a function definition always has a list of
arguments. Also, since you defined this method inside of a class,
"self" needs to be first parameter of the method.

> class = MyClass()
> app.add_url_rule('/', 'class.hello', class.hello)

This does not work either, "class" is a keyword in python can not be
used as a variable name.

You can turn debug mode on to see what errors you're getting:

http://flask.pocoo.org/docs/quickstart/#debug-mode