Django: Web MVC and DB DAO in minutes

On Django page you can read:

Django makes it easier to build better Web apps more quickly and with less code.

I decided to validate that statement.  And yeah, after working with Django for few days it turned out to be true. Following you may see how fundamental MVC tasks were implemented by me.

Get Django

To start with Django you have to download it.  I suggest to use pip as it is tool for downloading stuff, and works well.  After having that with you now – it’s time to play.  Application is prepared automatically by tool django-admin. There are two main keywords when it comes to Django: project and application. To be honest I didn’t get it really why do we need such a division but well : using framework you are playing with framework in a way you should do it. But having executed following 3 commands you are able to run Django app. Pretty cool.

Django-admin is so nice to create all the needed skeleton, which you will use to develop this web application. So let’s start the app already!

We have our application started. But what does it mean? Here, inside, we are going to place everything what is needed in the sample MVC Web app. Let’s play with it! All of the steps are in github project created for this purpose.

The first View & Model

The application has been started already. There are some warnings while running the app. But as for now ignore them. We are going to take care of them in DB-section.
It’s really worth mentioning that there are several steps now you have to execute.

    • in settings.py add the name of your app in INSTALLED_APPS
    • create view under /templates/index.html
    • setup view to render that template after request
    • bind URL to view in new file /url.py in your app folder, and fill the routing to the view that you are going to create in
    • bind URL from main project to URL inside app in /url.py
    • rerun application

It should work.

Of course you can fill the content of the page using controller. Let’s fill it content coming from DB!

All goes to DB!

Under the hood it was Sqlite db created! This is a default engine for Django app. How do you access that database? Using „M” from MVC – Models. Models are by default created in \models.py. Let’s follow snippet domain example. This is how the file with snippets could look like

How do we create such a schema in DB? There is an automated way to accomplish it in Django. Do you remember when warnings were displayed during app startup? Now we are going to fix these warnings. Two commands will be executed:

The result should be similar to this below:

Now the scheme is visible on DB. If you will connect to the database, using some browser like this you are going to see the table there. Let’s insert the value to the table already created.

Now we are going to display this value. You can easily add it to the context like here

Now it’s time to display it as in simple-as-could-be HTML page.

Done! We have created full MVC Django app in minutes.

Whole code is available at github in this project. What do you think?