On Jan 1, 2011, at 9:23 AM, Arun K.Rajeevan wrote: > Ok, I'm gonna experiment with raw_args. > I was wondering what's this #routes_apps_raw=['myapp'] thing before (I was > customizing routes)
Don't forget to remove the #. > > When I use this functionality, am I correct in assuming that, I've to do > encoding and decoding both? > > I can decode request.raw_args. > But where did I encode? > I'm using URL function. > So, should I encode return value of URL function or individual arguments and > variables that I'm passing? Good question. URL encodes args with urllib.quote, and vars (the query string) with urllib.urlencode. From the Python docs: http://docs.python.org/library/urllib.html#urllib.urlencode > urllib.quote(string[, safe]) > Replace special characters in string using the %xx escape. Letters, digits, > and the characters '_.-' are never quoted. By default, this function is > intended for quoting the path section of the URL.The optional safe parameter > specifies additional characters that should not be quoted — its default value > is '/'. > > Example: quote('/~connolly/') yields '/%7econnolly/'. > urllib.quote_plus(string[, safe]) > Like quote(), but also replaces spaces by plus signs, as required for quoting > HTML form values when building up a query string to go into a URL. Plus signs > in the original string are escaped unless they are included in safe. It also > does not have safe default to '/'. > urllib.urlencode(query[, doseq]) > Convert a mapping object or a sequence of two-element tuples to a > “percent-encoded” string, suitable to pass to urlopen()above as the optional > data argument. This is useful to pass a dictionary of form fields to a POST > request. The resulting string is a series of key=value pairs separated by '&' > characters, where both key and value are quoted usingquote_plus() above. When > a sequence of two-element tuples is used as the query argument, the first > element of each tuple is a key and the second is a value. The value element > in itself can be a sequence and in that case, if the optional parameter doseq > is evaluates to True, individual key=value pairs separated by '&' are > generated for each element of the value sequence for the key. The order of > parameters in the encoded string will match the order of parameter tuples in > the sequence. The urlparse module provides the functions parse_qs() and > parse_qsl() which are used to parse query strings into Python data structures. We do not pass either 'safe' or 'doseq', but we do the equivalent of doseq before we call urlencode(). > > Also, will you help me by giving difference between urllib.quote() and > urllib.quote_plus() and it's use case. > I haven't got that much experience with urllib class. The quote() description above is pretty straightforward. Notice that it will encode spaces as %20 and (by default) not encode '/'. It's used for args, in our case. quote_plus() is used indirectly by urlencode(), so it's relevant for query strings (vars). Spaces become '+' instead of %20 (notice that '+' in the string itself gets encoded). So: don't encode anything; URL will handle it for you, even in the current system. At least that's the way I read it.