0
我想通過django模板來縮短url的位置。我寫了下面的模板標籤代碼,但是我得到了下面的錯誤:似乎無法想象它整天!ValueError:Django中未知的url類型
ValueError: unkown url type:unknown url type: https%3A//api- ssl.bitly.com/v3/shorten%3Faccess_token%3DR_b622c9b2d53899697d6a78c088895f20%26longUrl%3Dhttp%3A//www.google.com%26format%3Dtxt
@register.simple_tag
def bitlys(long_url):
endpoint='https://api-ssl.bitly.com/v3/shorten?access_token={0}&longUrl={1}&format=txt'
req= urllib.quote(endpoint.format(settings.ACCESS_KEY, long_url))
return urlopen(req).read()
模板
{% bitlys 'http://www.manman.com' %}
下你說的話後,我把REQ = urllib.quote(endpoint.format (long_url)),我得到錯誤「元組索引超出範圍」 – picomon
我認爲需要包含訪問密鑰?因爲它縮短了網址。請進一步解釋你的真正含義。 – picomon
你不應該編碼URL的協議部分。這就是爲什麼「api-ssl.bitly.com」;看起來像「https%3A // api-ssl.bitly.com」 'endpoint ='https://api-ssl.bitly.com/v3/shorten?access_token=%s&longUrl=%s&format=txt'%( settings.ACCESS_KEY,urllib.quote(long_url)) return urlopen(endpoint).read()' – toad013