N.P
N.P

Reputation: 245

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?

Upvotes: 2

Views: 320

Answers (3)

N.P
N.P

Reputation: 245

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

Upvotes: 0

RocketDonkey
RocketDonkey

Reputation: 37249

You will need to modify the URL path that you have in your application. If I'm understanding correctly, you'll want to do something like this:

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

And then define your mainhandler as:

class mainhandler(webapp.RequestHandler):
    def get(self, post_id=None, post_title=None):

Although it doesn't seem like you are doing anything with them at the moment, once in your mainhandler you will have those variables available to you. The reason your first mapping wasn't working was because it wasn't set up to handle additional /'s and items in the URL.

And per our subsequent conversation, the cause of the syntaxError (which is a JavaScript-related error) is because of the missing / in front of include/load.js. Without that prefix, the path is relative to the current location. In the case of localhost:8080/1234/a-test-case, it becomes /1234/a-test-case/include/load.js, which doesn't map to anything specific in app.yaml and therefore falls through to the .* handler (and not the Javascript handler you have defined), which returns you to the main script and follows the hanlder paths there. The link to your .js. now returns your index.html instead of your .js file, which causes an error (similar to what can be found here).

Upvotes: 1

Tombatron
Tombatron

Reputation: 1347

I would consider using webapp2 which has support for extended routes and Python 2.7.

WebApp2 Routing

Upvotes: 0

Related Questions