How I Built a URL Shortener using Django
Building a URL Shortener, Is one of the Best Beginner Project to hone your Skills
Starting Off
We need some things setup before we start with our project.
We will be using Virtual Environment for our project.
pip install virtualenv
virtualenv urlShort
source urlShort/bin/activate
Above Command will create, activate virtual environment named urlShort.
Installing Important Packages
We need to Install some packages before hand,
pip install django
Django is a Web Framework written in Python for Python.
Starting With Our Project
First of all,
We need to create our project by,
django-admin startproject urlShort
cd urlShort
Above Command, Creates A Django Project and then cd into that directory
After that, We also need to create an app inside of our project.
App is sort of a container, where we will store our code. A project can have Multiple Apps and they can be interconnected
python manage.py startapp url
Above Command Creates an App named url in our Project.
Our File Structure Now will be —
urlShort
├── manage.py
├── url
│ ├── admin.py
│ ├── apps.py
│ ├── __init__.py
│ ├── migrations
│ │ └── __init__.py
│ ├── models.py
│ ├── tests.py
│ └── views.py
└── urlShort
├── asgi.py
├── __init__.py
├── __pycache__
│ ├── __init__.cpython-37.pyc
│ └── settings.cpython-37.pyc
├── settings.py
├── urls.py
└── wsgi.py
We will learn which file does what, when we will work in it.
Checking If all is Great…
You can check if all is working by just typing this in Command Line. But cd into the main folder, here urlShort.
python manage.py runserver
runserver will run a local server where our website will load. Move to url
https://localhost:8000
Keep Your Console Window Open.
Let’s Code!!!
Tighten your seat belts as we are starting to Code.
First of all, we will play with views.py.
views.py is basically used to connect our database, api with our Frontend.
Open views.py and type
from django.http import HttpResponsedef index(request):
return HttpResponse("Hello World")
Save it and open localhost and check if it changes.
It does not change because we have not map it to any route.
Basically, If you write any function inside views.py it does not work but we need to map it inside urls.py.
So, Create a urls.py inside url folder.
url/urls.py
from django.urls import path
from . import viewsapp_name = "url"
urlpatterns = [
path("", views.index, name="home")
]
Now after this changes check if it works.
It Definitely Works!!
Let’s Breakdown Both Files
views.py
from django.http import HttpResponsedef index(request):
return HttpResponse("Hello World")
So, Here we are importing HttpResponse from django.http module which will help us in sending Http Response to our Server, which will inturn print sentence in parentheses as HTML.
In views.py every function is has request as a parameter.
Our Index Function returns a HttpResponse and Hello World is printed on screen.
urls.py is like a Phone Directory, it contains which URL maps to which function/class of view.py.
We will end Part 1 of this Series.
2nd Part will contain Configuring Database and Forms.
For Video Series — https://youtu.be/xnw3l8cTNFI
Peace Out✌️