views:

70

answers:

2

I have been trying and trying for several hours now and there must be an easy way to retreive the url. I thought this was the way:

#from data.models import Program

import basehandler

class ProgramViewHandler(basehandler.BaseHandler):
    def get(self,slug):
#        query = Program.all()
#        query.filter('slug =', fslug)
        self.render_template('../presentation/program.html',{})

Whenever this code gets executed I get this error on the stacktrace:

appengine\ext\webapp_init_.py", line 511, in call handler.get(*groups) TypeError: get() takes exactly 2 arguments (1 given)

I have done some debugging, but this kind of debugging exceeds my level of debugging. When I remove the slug from def get(self,slug) everything runs fine.

This is the basehandler:

import os

from google.appengine.ext import webapp
from google.appengine.ext.webapp import template



class BaseHandler(webapp.RequestHandler):
    def __init__(self,**kw):
        webapp.RequestHandler.__init__(BaseHandler, **kw)

    def render_template(self, template_file, data=None, **kw):
        path = os.path.join(os.path.dirname(__file__), template_file)
        self.response.out.write(template.render(path, data))

If somebody could point me in the right direction it would be great! Thank you! It's the first time for me to use stackoverflow to post a question, normally I only read it to fix the problems I have.

A: 

If you do this:

obj = MyClass()
obj.foo(3)

The foo method on MyClass is called with two arguments:

def foo(self, number)

The object on which it is called is passed as the first parameter.

Maybe you are calling get() statically (i.e. doing ProgramViewHandler.get() instead of myViewHandlerVariable.get()), or you are missing a parameter.

Sjoerd
Ah I understand the problem now. I read somewhere about that. Your explanation is great I hope I can fix it now. Will keep you updated.
Sam S
+3  A: 

You are getting this error because ProgramViewHandler.get() is being called without the slug parameter.

Most likely, you need to fix the URL mappings in your main.py file. Your URL mapping should probably look something like this:

application = webapp.WSGIApplication([(r'/(.*)', ProgramViewHandler)])

The parenthesis indicate a regular expression grouping. These matched groups are passed to your handler as arguments. So in the above example, everything in the URL following the initial "/" will be passed to ProgramViewHandler.get()'s slug parameter.

Learn more about URL mappings in webapp here.

David Underhill
Yes I think I had that, but I'm not sure if I put the r before it. What means the r? I saw it before.Does it mean this should be regex parsed?
Sam S
The `r` prefix indicates that the following string literal is a [raw string literal](http://docs.python.org/reference/lexical_analysis.html#string-literals). Raw strings basically treat backslashes as any other character instead of escape sequences (except you can still escape a quote mark which would otherwise signal the end of the string literal). These are often used when specifying regular expressions because the syntax for these makes heavy use of backslashes. In the example I provided, the `r` doesn't make any difference since there are no backslashes in the string literal.
David Underhill
Ah Thanks for the explanation. I was stupid to forget to change my main. Guess too long working days are not good.
Sam S
You're welcome. It's an easy mistake to make - next time you'll recognize the somewhat cryptic error message and be able to patch it easily :).
David Underhill