Sudeep's Blog

URL Shortener with Django (Part 5) - Working with Templates and finishing up

Create folder ‘templates’ inside ‘shortenersite’ and then inside ‘templates’ create folder ‘shortenersite’.

Inside “shortenersite/templates/shortenersite/” create ‘base.html’ and ‘index.html’. So now paste the following to them.


{% raw %}
<!DOCTYPE html>
 <title>{% block title %}Welcome to mysite{% endblock %}</title>
 {% load staticfiles %}
 <link rel="stylesheet" href="">
 <link rel="stylesheet" type="text/css" href="{% static 'shortenersite/css/custom.css' %}" />
 <script src="//"></script>
 <meta name="viewport" content="width=device-width, initial-scale=1">
<script type="text/javascript">
 $(function() {
 $('#submitButton').click(function() {
 type: "POST",
 url: "/makeshort/",
 data: {
 'url' : $('#url').val(),
 'csrfmiddlewaretoken' : $("input[name=csrfmiddlewaretoken]").val()
 success: returnSuccess,
 dataType: 'json'

function returnSuccess(data, textStatus, jqXHR) {
 if(data.url) {
 } else {
 $('#url-result').text("Do not submit blank.");

<div id="wrap">
 <h1><a href="{% url 'shortenersite:home' %}">URL Shortner</a><small>Shorten the URL</small></h1>

<div id="content">
{% block content %}

{% endblock %}
{% endraw %}

We are using twitter bootstrap for making our site beautiful and jQuery for making POST request.


{% raw %}
{% extends "shortenersite/base.html" %}

{% block title %}URL Shortner - Make your URL shorter!{% endblock %}

{% block content %}

<div class="panel panel-default">
 <div class="panel-body">
 <div class="col-sm-9">
 <input type="text" name="url" id="url" class="form-control input-sm" placeholder="" />
 <div class="col-sm-3">
 {% csrf_token %}
 <button id="submitButton" class="btn btn-primary">Submit</button></p>
 <div class="panel-footer"><span id="url-result">Enter the URL above and click submit!</span></div>
{% endblock %}
{% endraw %}

Now let us add static files. Similarly as above create ‘static’ folder inside ‘shortenersite’ and inside ‘static’ create ‘shortenersite’ and inside it create folder ‘css’. Now create ‘custom.css’ inside ‘static/shortenersite/css/’ and add the following code to it:


#content {
margin-top: 20px;
margin-bottom: 60px;

text-decoration: none;

header {
border-bottom: 1px solid #eeeeee;
margin-bottom: 30px;
padding-bottom: 10px;
clear: both;

#wrap {
max-width: 700px;
margin: 5px 10px 10px 10px;
padding: 10px;
text-align: left;
#url-result {
color: #00e000;
display: hidden;

So now we are done creating our URL Shortener with django. Run the django app and visit localhost:8000/ in browser then the final result will look something like this:

final url shortener built with django

Now you can enter some URLs and click submit and then it should return the short URL. You can visit the short URL and it will redirect to your original URL. Go and see the admin panel, select the URLss and you will see the URLs are added there and also the count will increase if you visit the short URL.

That’s all for this tutorial. Hope everything works fine here.

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