Javascript Django中的URL检测与缩短
我只是想知道Django中是否有一种方法可以从一堆文本中检测URL,然后自动缩短它们。我知道我可以使用urlize来检测URL,但我不确定是否可以使用bitly或其他方法来缩短链接Javascript Django中的URL检测与缩短,javascript,python,django,Javascript,Python,Django,我只是想知道Django中是否有一种方法可以从一堆文本中检测URL,然后自动缩短它们。我知道我可以使用urlize来检测URL,但我不确定是否可以使用bitly或其他方法来缩短链接 而且,用javascript而不是python来完成这项任务会更好吗?如果是这样的话,我该怎么做呢 对于bit.ly,如果您只想缩短URL,它非常简单: 首先创建一个帐户,然后访问以获取API密钥 向API的发送请求,结果是缩短的URL: from urllib import urlencode from urlli
而且,用javascript而不是python来完成这项任务会更好吗?如果是这样的话,我该怎么做呢 对于bit.ly,如果您只想缩短URL,它非常简单: 首先创建一个帐户,然后访问以获取API密钥 向API的发送请求,结果是缩短的URL:
from urllib import urlencode
from urllib2 import urlopen
ACCESS_KEY = 'blahblah'
long_url = 'http://www.example.com/foo/bar/zoo/hello/'
endpoint = 'https://api-ssl.bitly.com/v3/shorten?access_token={0}&longUrl={1}&format=txt'
req = urlencode(endpoint.format(ACCESS_KEY, long_url))
short_url = urlopen(req).read()
您可以将其包装到模板标记中:
@register.simple_tag
def bitlyfy(the_url):
endpoint = 'https://api-ssl.bitly.com/v3/shorten?access_token={0}&longUrl={1}&format=txt'
req = urlencode(endpoint.format(settings.ACCESS_KEY, the_url))
return urlopen(req).read()
然后在模板中:
{%bitlyfyhttp://www.google.com %}
注意:标记中的位置参数是django 1.4的一个特性
如果您想了解bit.ly API的所有功能,请先阅读上的文档,然后下载官方版本 对于bit.ly,如果您只想缩短URL,它非常简单: 首先创建一个帐户,然后访问以获取API密钥 向API的发送请求,结果是缩短的URL:
from urllib import urlencode
from urllib2 import urlopen
ACCESS_KEY = 'blahblah'
long_url = 'http://www.example.com/foo/bar/zoo/hello/'
endpoint = 'https://api-ssl.bitly.com/v3/shorten?access_token={0}&longUrl={1}&format=txt'
req = urlencode(endpoint.format(ACCESS_KEY, long_url))
short_url = urlopen(req).read()
您可以将其包装到模板标记中:
@register.simple_tag
def bitlyfy(the_url):
endpoint = 'https://api-ssl.bitly.com/v3/shorten?access_token={0}&longUrl={1}&format=txt'
req = urlencode(endpoint.format(settings.ACCESS_KEY, the_url))
return urlopen(req).read()
然后在模板中:
{%bitlyfyhttp://www.google.com %}
注意:标记中的位置参数是django 1.4的一个特性
如果您想了解bit.ly API的所有功能,请先阅读上的文档,然后下载官方版本 如果要使用Bitly API,模板标记应为:
from django import template from django.conf import settings
import bitly_api import sys import os
register = template.Library()
BITLY_ACCESS_TOKEN="blahhhh"
@register.simple_tag def bitlyfy(the_url):
bitly = bitly_api.Connection(access_token=BITLY_ACCESS_TOKEN)
data = bitly.shorten(the_url)
return data['url']
但有一件事我无法在模板中管理:
{% bitlyfy request.get_full_path %}
{% bitlyfy {{request.get_full_path}} %}
这两个都不起作用,不知道如何解决。
欢迎任何帮助 如果要使用Bitly API,模板标记应为:
from django import template from django.conf import settings
import bitly_api import sys import os
register = template.Library()
BITLY_ACCESS_TOKEN="blahhhh"
@register.simple_tag def bitlyfy(the_url):
bitly = bitly_api.Connection(access_token=BITLY_ACCESS_TOKEN)
data = bitly.shorten(the_url)
return data['url']
If you are using bit.ly then the best code to shorten url is:
import urllib
import urllib2
import json
link = "http://www.example.com/foo/bar/zoo/hello/"
values = {'access_token' : BITLY_ACCESS_TOKEN,
'longUrl' : link}
url = "https://api-ssl.bitly.com/v3/shorten"
data = urllib.urlencode(values)
req = urllib2.Request(url,data)
response = (urllib2.urlopen(req).read()).replace('\/', '/')
bitly_url = (json.loads(response))['data']['url']
return bitly_url
但有一件事我无法在模板中管理:
{% bitlyfy request.get_full_path %}
{% bitlyfy {{request.get_full_path}} %}
这两个都不起作用,不知道如何解决。
欢迎任何帮助 但是如果我有一整段由用户输入的文本,并且该段中有一些URL呢。因此,我希望自动检测URL,然后缩短URL。是否全部由bit.ly完成?不,您必须修改模板标记,或者编写一个模板上下文处理器来搜索文本;识别链接,然后通过bit.ly缩短它们。您可以使用的源代码获取文本功能中的URL搜索。您好,很抱歉再次打扰您,但我尝试为bitly编写自定义模板标记,但它似乎不起作用。我已经更详细地定义了这个问题,但是如果我有一整段由用户输入的文本,并且该段中有一些URL呢。因此,我希望自动检测URL,然后缩短URL。是否全部由bit.ly完成?不,您必须修改模板标记,或者编写一个模板上下文处理器来搜索文本;识别链接,然后通过bit.ly缩短它们。您可以使用的源代码获取文本功能中的URL搜索。您好,很抱歉再次打扰您,但我尝试为bitly编写自定义模板标记,但它似乎不起作用。我已经更详细地定义了这个问题
If you are using bit.ly then the best code to shorten url is:
import urllib
import urllib2
import json
link = "http://www.example.com/foo/bar/zoo/hello/"
values = {'access_token' : BITLY_ACCESS_TOKEN,
'longUrl' : link}
url = "https://api-ssl.bitly.com/v3/shorten"
data = urllib.urlencode(values)
req = urllib2.Request(url,data)
response = (urllib2.urlopen(req).read()).replace('\/', '/')
bitly_url = (json.loads(response))['data']['url']
return bitly_url