Django Tutorial – Building URL Shortener with Django

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

17 thoughts on “Django Tutorial – Building URL Shortener with Django”

  1. Instead of settings.SITE_URL you can use ‘reverse’ with your redirectOriginal URL. Then you don’t need to change for different sites.


  2. Your use of CamelCase for function, directory and file names is inconsistent with standard Python naming patterns (see PEP-8).


  3. The current example will create a new record each time an url is submitted, code below returns the previous shortcode if a url is re-submitted :

    def shortenUrl(request):
    url = request.POST.get(“url”, ”)
    print url
    urll = Urls.objects.get(httpurl=url)
    short_id = urll.short_id
    except (Urls.DoesNotExist):
    print “Does not exist”
    if not (url == ”):
    print “URL not empty !”
    short_id = getShortCode()
    print “Got shortcode”
    b = Urls(httpurl=url, short_id=short_id)
    print “Saved URL”
    response_data = {}
    response_data[‘url’] = “/” + short_id
    return HttpResponse(json.dumps({“error”: “error occurs”}),content_type=”application/json”)
    response_data = {}
    response_data[‘url’] = “/” + short_id
    return HttpResponse(json.dumps(response_data), content_type=”application/json”)


    1. Actually it is useful for tracking purpose if the new shortcode is generated for URL, even same URL is submitted twice.

      Anyway you can also implement in that way.


  4. Sir,
    When i run this code there is an error on base.html tells that
    Error during template rendering

    In template /home/jobi/Recursive/django/tutorial/urlShortner/shortenerSite/templates/shortenerSite/base.html, error at line 40
    u’shortenerSite’ is not a registered namespace
    please help me to solve it


  5. My site doesn’t display the short URL that’s created. (it does create the short URL though) and also doesn’t display any warning for empty addresses. Rest everything works well.


  6. Hey there found your tut online. A couple of things there are some changes in Django 1.10 and beyond with a few things, and I downgraded to 1.7x like you have here and i’m having an issue with a traceback stating:

    “^(?P<short_id>\w{6})$” is not a valid regular expression: unknown specifier: ?P&

    Seems that when I open shortenersite/ and comment out the redirection url everything works accept the re-direct is there something up there?


      1. Hey that was the ticket, must have been an issue with the code in the form on your wordpress site. Thanks Sudeep! Might also be cool to refresh this with Django 1.10 cause some things changed which breaks your tutorial. 🙂



  7. It would be quicker to check for existence of the DB record, rather than retrieve it fully. `if not Url.objects.filter(pk=short_id).exists(): return short_id` 🙂


  8. First of all thanks for this great tutorial. I want to add in the project that if all 6 digits end then the length to get increment by 1. Can you help to come over this.Thanks in advance.


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.