url shortener python project source code

url shortener python project source code


Do you want to build a url shortener? If yes, then you should read this tutorial.

A URL shortener is a service that allows you to shorten long URLs into shorter ones. This is because google has shortened the link for us.

I will show you how to create a simple url shortening service using Python.


Python 2.7 or higher

Django 1.4 or higher


1) Install Django and paste bin in your system.

2) Create a new Django application called “short”. I have created it under my home directory.

3) Now we need to install PasteBin module. So go to paste bin website and download the latest version of the module. Extract the downloaded file and place it in your home directory.

4) Open terminal and type following command to activate django development mode.

python manage.py runserver

You can use any name you like but remember to change it later.

6) Click on “New Paste” button. A form will appear with some fields. Fill up all the required details.

7) After filling the form click on submit. The page will reload and you will see an error message saying “The given data was invalid”.

8) In the same window copy the raw HTML from the browser. You can do so by pressing CTRL + U key combination.

9) Go back to your home directory and open the file named “paste_bin.html”. Copy the raw HTML content from the browser.

10) Now paste the copied HTML content to the paste_bin.html file. Save the file.

11) Run the server again. It will work fine now.

You will get the following screen.

13) Click on “Copy Link” button. A pop-up box will appear asking you to enter a title for the link. Enter a title and press OK.

14) Now select the text field which says “Shorten this URL”. Paste the link you want to be shorted. Press Ok.

15) Now click on “Create Shortened URL” button. The page will refresh and you will see the newly generated short link.

16) Try clicking on the link. It will redirect you to the original link.

17) That’s it! Your first url shortner is ready.

18) To make it more user friendly you can add a few features. We will discuss them in next steps.

19) Before going further let me tell you one thing. Don’t forget to replace the IP address 0.0.0 in step 6.

20) Let’s start coding.

21) First create a model class named “Link”. Add the following attributes to it.


22) Now we need to define our views. Create a view function named “shortlink”.

23) Define the following variables inside the view function.

24) Now write the following lines of code inside the view function. Remember to import the models.

25) Write the following line of code at the end of the view function.

26) Now save the file as “views.py”.

27) Now create another view function named “get_links”.

28) Inside the view function write the following lines of codes.

29) At last, write the following line of code in the view function.

30) Now save the file.