Search code examples
pythondjangourlget

Django: sending GET messages


I'm using Django 1.1 on a project, and I ran into a problem.

I need to send a GET request to an external URL. I want to create a method that would act like this:

def Send_msg(object):
    converted_url = "http://example.com/some/link/?title=" 
                    + object.title + "&body=" + object.body
    LoadURL(converted_url)
    return True

Another problem that title and body should be translated to equivalent of rawurlencode() in PHP

I tried to search in Django Docs, but no success.


Solution

  • def Send_msg(request, object):
        import urllib2, urllib
        base_url = "http://example.com/some/link"
        values = { 'title': object.title, 'body': object.body }
        data = urllib.urlencode(values)
        urllib2.urlopen(base_url+"?"+data).read()                
        return HttpResponse()
    

    Something like this. The request bit is not necessary, depends on your requirements, though.