2011-06-08 55 views
0

我在項目上使用Django 1.1。我遇到了一個問題。Django:發送GET消息

我需要加載/發送GET到外部URL。我想創建方法會像這樣:

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

這標題和正文應在PHP

翻譯成rawurlencode()的等價另一個問題我想在Django的文檔進行搜索,但沒有成功。

+0

可能的重複[在Python中給給定的URL添加參數](http://stackoverflow.com/questions/2506379/add-params-to-given-url-in-python) – 2011-06-08 10:42:34

回答

3
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() 

就像這樣。儘管如此,request位不是必需的,取決於您的要求。

+0

是的,HttpResponse無法訪問這個案例。 – JackLeo 2011-06-08 10:53:38