Search code examples
pythongoogle-app-engineslug

Implementing URL slugs in GAE


I'm trying to implement URL slugs in my app (python-based). I want the slugged URL to be of the format myhost/{post_id}/{post_title}.

When I try to access the page using the above slugged URL format, I get an error and (in Chrome, the error says - unexpected token <). If I delete the /<post_title> from the URL, the page loads correctly. Specifically, I noticed that once I have a 'forward slash' after the <post_id>, I have issues. Everything works fine without that extra slash (extra directory)

My code is:

class mainhandler(webapp.RequestHandler):
    def get(self):
        if (self.request.path == '/test'):
            path = os.path.join (os.path.dirname (__file__), 'test.htm')
            self.response.headers ['Content-Type'] = 'text/html'
            self.response.out.write (template.render (path, {}))
        else:                       
            path = os.path.join (os.path.dirname (__file__), 'index.htm')            
            self.response.headers ['Content-Type'] = 'text/html'                 
            self.response.out.write (template.render (path, {}))

    application = webapp.WSGIApplication( [('/.*', mainhandler)],  debug=True) 

Basically, I want to load the index.htm file and on that file, I have JavaScript which is supposed to extract the post-id from the URL and do some stuff with it.

Anybody know what I'm doing wrong here?


Solution

  • After some extensive discussion with RocketDonkey, I tried something which worked.

    I changed my script file from

    <script src="include/load.js" type="text/javascript"></script>
    

    to

    <script **src="/include/load.js"** type="text/javascript"></script>
    

    RocketDonkey's explanation for the working solution: when you use a front slash, you are saying 'take this URL path from root'. But without it, it is in terms of the current location

    This explains why the original code worked when my url was simply localhost:8081/1234 but not in the second scenario where I have a second directory

    NB: The solution described here was used in conjunction with the original proposal from rocketDonkey