Computer >> Hướng Dẫn Máy Tính >  >> Lập Trình >> Python

Tạo ứng dụng rút gọn URL trong Django


Dùng thư viện pyshorteners (API TinyURL) để chuyển URL dài thành URL ngắn.

Triển khai

  1. pip install pyshorteners

  2. View dựa trên class View với GET/POST:

  3. POST: lấy URL từ request, gọi pyshorteners.Shortener().tinyurl.short(long_url), render lại template với short_url.

  4. Template: form nhập URL, hiển thị kết quả rút gọn.

Kết quả

Nhập link dài → bấm "Shorten URL" → hiện link ngắn có thể copy.