a URL shorting app for Django.
django-url-shortening的Python项目详细描述
django url缩短
django的自定义url缩短应用程序,带有api。
用法
将urlshortening应用程序添加到INSTALLED_APPS并执行 migrate
通过添加到urlconf连接重定向视图
('^linkshortening/', include('urlshortening.urls'))
添加设置(有关参数的更多信息,请进一步了解)
INITIAL_URL_LEN = 6 RETRY_COUNT = 5 SHORT_URL_PATH = 'http://example.com/short-prefix/' REDIRECT_PREFIX = 'r'
现在您可以使用api制作短链接
POST linkshortening/short/
使用json数据{"full_url": "http://example.com/a/b/c/d/e"}
并获得响应
{"data": { "short_id": "123456", "short_url_path": "http://example.com/short-prefix/" }, "error": ""}
您还可以在代码中使用urlshortening。
from urlshortening.models import get_short_url, invalidate_url, get_full_url
url = "http://example.com/a/b/c/d/e" short_url = get_short_url(url) # Url object print(short_url.short_id) # id for short url
full_url = get_full_url(short_url.short_id) # Url object print(full_url.url) # "http://example.com/a/b/c/d/e"
# You could also invalidate url invalidate_url(full_url.short_id)
API
get short link
url
/short/
方法:POST
数据参数
- 完整的URL
成功响应:
- 代码:200内容:{ data: { "short_url_path": "000001" }, error: "" }
错误响应:
- 代码:400内容:{ error : "full_url is empty" }
- 代码:400内容:{ error : "full_url is too long" }
获取完整链接
url
/expand/:short_id/
方法:GET
url参数
short_id=[string]
成功响应:
- 代码:200内容:{ error : "", data: { full_url: "http://example.com/to-000001" }}
错误响应:
- 代码:404内容:{ error : "Link is expired" }
- 代码:404内容:{ error : "Url doesn\'t exist" }
get重定向
url
/REDIRECT_PREFIX/expand/:short_id/
方法:GET
url参数
short_id=[string]
成功响应:
- 代码:302
错误响应:
- 代码:404内容:{ error : "Link is expired" }
- 代码:404内容:{ error : "Url doesn\'t exist" }
使url无效
url
/invalidate/
方法:POST
数据参数
- 短ID
成功响应:
- 代码:200内容:{ error : "", data: { "short_id": "000001", "invalidated": "true" } }
错误响应:
- 代码:400内容:{ error : "short_id is empty" }
- 代码:400内容:{ error : "Link is already expired" }
- 代码:404内容:{ error : "Url doesn\'t exist" }
设置
可用设置为:
INITIAL_URL_LEN
URL的短ID的初始长度。一旦你的身份证比 在INITIAL_URL_LEN符号的所有组合中都是可能的 将增加一个
RETRY_COUNT
在增加INITIAL_URL_LEN之前要检查多少次。
SHORT_URL_PATH
将与short_idon一起返回的URL POST linkshortening/short/请求。也许能帮你 构造完整的url。
REDIRECT_PREFIX
选择前缀以使用重定向链接。例如 REDIRECT_PREFIX="r"我们得到格式为 /r/expand/{short_id}/