我正在尝试使用bitly构建一个简短的url。 我在模板中使用这样的模板过滤器。
{{long_url|shorten_url}}
缩短网址是这样的:
@register.filter(name='shorten_url')
def shorten_url(value):
username = settings.BITLY_USERNAME
password = settings.BITLY_PASSWORD
bitly_url = "http://api.bit.ly/v3/shorten?login={0}&apiKey={1}&longUrl={2}&format=txt"
req_url = bitly_url.format(username, password, 'https://'+ value)
short_url = urlopen(req_url).read()
return short_url
错误追溯就是这样。
short_url = urlopen(req_url).read()
File "/usr/lib/python2.7/urllib2.py", line 127, in urlopen
return _opener.open(url, data, timeout)
File "/usr/lib/python2.7/urllib2.py", line 410, in open
response = meth(req, response)
File "/usr/lib/python2.7/urllib2.py", line 523, in http_response
'http', request, response, code, msg, hdrs)
File "/usr/lib/python2.7/urllib2.py", line 448, in error
return self._call_chain(*args)
File "/usr/lib/python2.7/urllib2.py", line 382, in _call_chain
result = func(*args)
File "/usr/lib/python2.7/urllib2.py", line 531, in http_error_default
raise HTTPError(req.get_full_url(), code, msg, hdrs, fp)
HTTPError: HTTP Error 500: Internal Server Error
它以前工作过,但意外地破了。
试图把那个short_url = urlopen(req_url).read()
和捕获异常说,INVALID_URI,请帮我调试这个问题。