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 settings.py for different sites.

    Like

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

    Like

  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
    try:
    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)
    b.save()
    print “Saved URL”
    response_data = {}
    response_data[‘url’] = “/” + short_id
    else:
    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”)

    Like

    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.

      Like

  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

    Like

  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.

    Like

  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/urls.py and comment out the redirection url everything works accept the re-direct is there something up there?

    Like

      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. 🙂

        -adam

        Like

  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` 🙂

    Like

  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.

    Like

Leave a Reply

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

WordPress.com Logo

You are commenting using your WordPress.com 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.