Executing the Ideas with Django!

Sai Raghava
Code Lore

--

Everyone in this world comes up with their own ideas. But, how many of them execute their ideas. Very few, right? Reasons might be many. Few of them could be, being lazy to work on it, or idea being too far from practicality to work on it.

Trying to work on your idea is the most important part of ideation. You may fail in implementing that. No worries. In the worst case, you will learn which ideas are worth giving a try. One thing is for sure, you’ll end up teaching your brain to start ideating more practical ideas. Which means, over a period of time, you come up with more practical ideas than others.

Wait there. Why am I discussing all this? There is a reason for me to talk about this. Recently I came up with an idea and was finally able to implement it. I wanted to share my ups and downs in the journey of working on my idea which might help you in building your idea. Because,

Learning from others mistakes is easy and risk-less.

I’m a frequent listener of podcasts, especially the ones that belong to Travis(have a look here). I found difficulty in surfing the podcasts. So, I came up with an idea “What if I include a search bar for these podcasts?”. Beep, beep boop. That’s it. I was done with ideating.

Now starts the actual game, Execution. The best way to work on my idea was to come up with a web app. I searched a few web development frameworks and finally decided to work with Django which was built upon Python.

Never heard the word Django? Don’t panic. You will not have that weird expression when you hear the next time. I know how it feels for a newbie like you. We both are sailing on the same sea where I’m just a day ahead of you. My bitter experiences can serve as warning boards to you. A basic knowledge about python and a strong belief on your idea are the only prerequisites for getting started.

Django is one of those MVC Frameworks. Which means we need to design three main modules to complete our web app, Model, View and Controller(Both are much similar in Django, no clear distinction between view and a controller). Let me brief how to craft those three main modules.

Model

A blueprint of a database schema (or) a programmatic representation of the database. The beauty of Django lies here. Creation of Model provides a layer of abstraction for your app and the actual database. In simple words, you can write a common piece of code for any back-end database. Be it MySQL or Oracle, modify your settings.py in your app directory accordingly and your code is safe and working.

  • Models must be written in a dedicated python file named models.py
  • Model is a class which extends Model.
  • Data variables in the class are the corresponding columns in database.
  • Few datatypes that are allowed are IntegerField, CharField, TextField.
  • Override the function __unicode__ to ensure what to be printed when an instance of object is called(similar to toString() in Java).

You are done with programming a Model. A basic Model would look like

View

Views form the core of the web app. It is that part of app which accepts requests from user, process the request and return the response. A simple view can be a python function which contains the whole processing logic. It may process database instances using models created earlier or simply redirect to other URL.

Views may look simple but they are the most difficult and deciding part of an app. Have a clear idea about the logic which is used to process the user request. The response from a view, in general, is a web template. We populate data onto that template dynamically by passing a dict to that template.

  • A view is a python function which takes user request as input.
  • A view function must be mapped with corresponding url in urls.py file and the views are to be written in a dedicated python file named views.py
  • We load a template(usually HTML file) into the function to be attached with response.
  • Don’t forget to import models into the views.py file. You need to process information from database using model objects only.
  • Process the user request accordingly and set the context for the template. A context is simply a data-set which the template uses to fill the gaps.
  • Return a HttpResponse to the user, a web template, containing processed information.

You are done with creating a view. You can have a look of basic view here.

Container

Controller is the driver of your web app. It defines which request must be handled by which part of the code and where the response but be redirected to, how to connect to database and which add-ons to be loaded.

Thank yourself for reaching this far. A small giveaway from myside,

You need not create any container to build your web app in Django.

Yeah, its true. In Django there is no need of explicitly creating a container. These four components indirectly implement the work of a container.

  • views.py
  • urls.py
  • settings.py
  • web template

So, let’s not waste our time and learn how do we create a template. It is that part of your app that directly interacts with the user. So, be as creative as you can. The web template which we are going to create now is not a static file as HTML, instead we add dynamic nature to the file by writing few tags like {{…}} and {%…%}. All programming constructs like for, if can be used within {%…%} but be sure to end them using end tags. As we discussed earlier all data which we want to populate onto template can be embedded in {{…}}. Let me show you how a basic template would look like.

One useful tip to make your page more materialistic. Refer this and add snippets to your snippets to add material design to your page. Experiment with few tags, colors and use one that suits you best.

You are done with your app. Now that sets you apart from others. Feel proud about yourselves that you did not stop just dreaming about an idea but finally ended in implementing it. Kudos to you.

Your idea may not be the same as mine. But what I wanted to teach you is the process that everyone will follow in coming up with an app. You could link your idea to mine and add different functionalities. Make use of Google and refer documentation whenever you are struck. You can download the Django documentation and refer that offline here.

ALL THE BEST for your ideas. Come up with the coolest ideas and be sure you implement them.

Meet you in my next article. Till then, Happy Coding!

Originally published at iamraghava.wordpress.com on July 9, 2016.

--

--