Issues implementing URL slug

156 views
Skip to first unread message

NP

unread,
Jan 15, 2013, 4:17:50 PM1/15/13
to google-a...@googlegroups.com
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? 

Julie Smith

unread,
Jan 16, 2013, 8:56:09 AM1/16/13
to google-a...@googlegroups.com
Hello NP,

That code works for me running on the development server on Windows 7 running Chrome 23.

You haven't included your app.yaml, so I used

handlers:
  
- url: /.*
  script: main.py

Is that what you have?

Regards,

Julie


--
You received this message because you are subscribed to the Google Groups "Google App Engine" group.
To view this discussion on the web visit https://groups.google.com/d/msg/google-appengine/-/YesSG15kKXYJ.
To post to this group, send email to google-a...@googlegroups.com.
To unsubscribe from this group, send email to google-appengi...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/google-appengine?hl=en.

NP

unread,
Jan 16, 2013, 1:03:06 PM1/16/13
to google-a...@googlegroups.com
Yea, that's what I have

NP

unread,
Jan 16, 2013, 1:04:02 PM1/16/13
to google-a...@googlegroups.com
I'm running my development server on Windows XP

Jesse

unread,
Jan 16, 2013, 3:02:52 PM1/16/13
to google-a...@googlegroups.com
You're not actually loading myhost/<post_id>/<post_title> right? You're loading something like:

myhost/123123/this-is-the-title-slug

?

Jesse

unread,
Jan 16, 2013, 3:04:07 PM1/16/13
to google-a...@googlegroups.com
Since <> are not valid URL characters.

NP

unread,
Jan 16, 2013, 3:53:41 PM1/16/13
to google-a...@googlegroups.com
yea, i only used the anchors (in my post) to signify they are placeholders

Julie Smith

unread,
Jan 16, 2013, 9:05:10 PM1/16/13
to google-a...@googlegroups.com
NP, can you give an actual example of a URL that you are having problems with?
Which version of the SDK and Python are you running?
It works for me using App Engine SDK release: "1.7.4" and Python 2.7.3

I agree with Jesse that < and > should not be used in a URL, however as far as I can tell from RFC3986, < and > are not explicitly reserved characters (however they are also not included in the unreserved list either).

Whilst I don't recommend using < or > in a URL, it seems that IE, FF and Chrome convert those characters to percent-encoded octets before sending them to the server Within the app, self.request.url shows those characters as %3C and %3E.


--
You received this message because you are subscribed to the Google Groups "Google App Engine" group.
To view this discussion on the web visit https://groups.google.com/d/msg/google-appengine/-/WHVS3FVFrwsJ.

NP

unread,
Jan 16, 2013, 9:29:10 PM1/16/13
to google-a...@googlegroups.com
I am running on SDK 1.6.3 and python 2.5

Also, I don't actually use '<' in my url. I only used it in the post here to indicate it is meant to be a placeholder. A real url on my Dev server would be something like localhost:8081/1234/a-simple-example

and the error I get from the Firebug console is 


- SyntaxError: syntax error
If I view the error in Chrome, it says

Uncaught SyntaxError: Unexpected token <

Julie Smith

unread,
Jan 16, 2013, 9:41:43 PM1/16/13
to google-a...@googlegroups.com
Is it possible that there is indeed a syntax error in your index.html template file?

If you check the console output in Chrome
    Tools > Javascript Console > Console
it should indicate the where the error is. If you click on the error line number on the right hand side, the console will show the source code at that line. Note that the line number displayed in the console may not be the same as the line number in your index.html file, as the template variables are filled in.



To view this discussion on the web visit https://groups.google.com/d/msg/google-appengine/-/sxFetiKClD0J.

NP

unread,
Jan 16, 2013, 10:44:06 PM1/16/13
to google-a...@googlegroups.com
Finally found the solution (with the help of someone from Stackoverflow) but now I'm trying to understand the solution.

In my index.htm file, I load a javascript file using <script src="include/load.js" type="text/javascript"></script>.
The above worked for urls such as localhost:8081/1234 but fails when the url is localhost:8081/1234/a-test-case

I changed it to  <script src="/include/load.js" type="text/javascript"></script> i.e. added a forward slash before the include folder and everything works now.  So now I'm trying to understand why this works.

My first guess would have been it couldn't find load.js without the extra forward slash but that doesn't make sense because the Javascript was being fired correctly without the extra slash (I know this because I use javascript to control a section of the page.

NP

unread,
Jan 16, 2013, 11:15:51 PM1/16/13
to google-a...@googlegroups.com
Explanation from Stackoverflow: 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 
Reply all
Reply to author
Forward
0 new messages