开发者

Django: sending GET messages

开发者 https://www.devze.com 2023-03-11 23:49 出处:网络
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:

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=" + o开发者_JAVA百科bject.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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消