Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to pass a url as a url parameter when there is a question mark in it?

I am using this line to pass the a url, main_id and user_tag_list to the handler:

self.response.out.write("""

<a href="/tc?url=%s&main_id=%s&user_tag_list=%s" title="edit tag set">edit tag set</a>

""" %
(item.url, main_id, item.tag_list))

this works fine except that when there is already a question mark in the passed url (the first parameter) I can no longer get the main_id with

main_id = self.request.get("main_id")

in the target handler, instead I get the error

m = Main.get_by_id(int(main_id))
ValueError: invalid literal for int() with base 10: ''

Is there a way around this?

like image 920
Zeynel Avatar asked Mar 25 '11 03:03

Zeynel


2 Answers

You just want to use urllib.quote

>>> import urllib
>>> urllib.quote('http://www.google.com?q=Zombie+Apocalypse')
'http%3A//www.google.com%3Fq%3DZombie%2BApocalypse'

So:

urllib.quote(item.url)
like image 192
orokusaki Avatar answered Oct 09 '22 02:10

orokusaki


urllib.quote ought to help with this.

like image 22
jcomeau_ictx Avatar answered Oct 09 '22 03:10

jcomeau_ictx