Sudeep's Blog

URL Shortener with Django (Part 3) - Creating django app and model

Now, Let us create a Django app. From your project’s root directory run this command

django-admin startapp shortenersite

This will create a new folder ‘shortenersite’ inside our root directory which is actually a Django app. You can see the following files and folder inside shortenersite:

  1. migrations - Contains database migration file.

  2. - We will use this file to configure Django Admin settings for this app.

  3. - Contains the model (generally deals with database.)

  4. - Used to create views.

  5. - Contains the test suite for our app but we will not be using this file in our tutorial.

Now add this app to our project. Open up “urlshortener/” file and add ‘shortenersite’ to INSTALLED_APPS. So now the INSTALLED_APPS will look something like this.


# Application definition


Now let us work in our Open up “shortenersite/” and add the following code to it.


from django.db import models

# Create your models here.
class Urls(models.Model):
    short_id = models.SlugField(max_length=6,primary_key=True)
    httpurl = models.URLField(max_length=200)
    pub_date = models.DateTimeField(auto_now=True)
    count = models.IntegerField(default=0)

def __str__(self):
    return self.httpurl

What we just did?

We created table named ‘Urls’ in our database with the following field:

  1. short_id (it will store short URL id), (It is of max_length 6 and it is a primary key)

  2. httpurl (it will store URL), (It is of max_length 200)

  3. pub_date (It will store the date and time)

  4. count (It will store the number of times the URL is visited)

Run this command to make changes to database.

python makemigrations
python migrate

We just updated our database. We are using sqlite3 which is set up by default for us. So, we do not need to configure anything regarding database settings for now, but if you want to use other database rather then sqlite3 then you can change DATABASES option in “urlshortener/”.

Now let us add this model to Django admin.

But before that let us be sure that you do not have comment in the following line in “urlshortener/”.


url(r'^admin/', include(,

Open your “shortenersite/” And add the following:


from django.contrib import admin
from shortenersite.models import Urls
# Register your models here.

class UrlsAdmin(admin.ModelAdmin):
    list_display = ('short_id','httpurl','pub_date', 'count')
    ordering = ('-pub_date',), UrlsAdmin) # Register the Urls model with UrlsAdmin options

Simply the above code registers the Urls model we created above to Django Admin.

Run the Django project with

python runserver

and Open up


Enter your Login details i.e the credentials of superuser that you have created before. Now you should see something like this.

django admin

We have Urlss(Model name in plural form) under Shortenersite(App Name). I hope you understand this. Django admin is one of the beautiful feature i like most about Django. And i am sure you will also feel same about Django admin.

Open up Urlss and you can add Urls by clicking Add urls. You can see the fields like Short id, Httpurl and count that we have created. DateTime is added automatically so, you do not have to worry about it.

Alright, This is the end of the Part 3, and we have created app, models for our app and registered the model to see it in Django Admin.

Till now we have just done the Back-end, In the next two tutorials we will be working on Front-end. We will build web app from where user can input the URLs and will get the short URL for his URL.

Post Series:

Part 1: Create a virtual environment and install Django

Part 2: Create a new project and make sure it works

Part 3: Create Django App, Model for our App and work with Django Admin

Part 4: Working with URLs and Views

Part 5: Working on Templates, Static files and finishing up