Django Basics : Part-2

magic-pony-django-wallpaper

‘M happy to be back to  you all.

So, this post will help you setup your first django web app and will also fill in details and notes on how many of its small components work and interact. I have just explained the basic components in my previous post which is the first part without any hint of python-django code. Let me do that this time and get you acclimatized to the things that may go wrong often as well, apart from the things that will go right! This part 2 tutorial will comprise a few defined set of steps.
Before we begin I want you to refer this tutorial if you want to know anything about writing your first django app beyond this tutorial please visit : https://docs.djangoproject.com/en/1.9/intro/tutorial01/
Also, I assume that you have python 2.7 installed in your machine as this tutorial will be on django 1.9x and python 2.7. this tutorial should work on both Windows and Unix based systems, including Mac, though the directory structure is assumed to be of Mac. One can use it in Windows by making slight changes to the tutorial’s instructions.

So, let’s begin…

To know how to install pip please refer my tutorial on installing pip. All the commands below are to be executed in the terminal.

Install django

$pip install django

 

Check if Installation is successful

$python
>>>import django
>>>print django.get_version()

This will tell you the version of django that you just installed. If you did not install it properly you will see error messages in executing the first or the second function.
When done, type :

>>exit()
or
>>>ctrl + D

to exit from the python prompt

 

Create your project directory and ‘cd’ to it

$cd ~/Desktop/
$mkdir djangoTutorialStuff
$cd djangoTutorialStuff

 

Make a django project

$django-admin startproject musicStore 

Check if it worked and you may want to check the directory structure too.
$cd musicStore

You’re gonna see two things inside the project. A manage.py file and a musicStore directory. The manage.py is the principle python file governing the whole project. Anything you want to do with the project goes like python manage.py <your_command>.

Confirm if the musicStore directory has the following files : 

__init__.py  – Tells that this directory is a django directory
settings.py  – Contains the overall settings of the whole django project (musicStore)
urls.py  – Contains the root url configurations for the musicStore project
wsgi.py  – contains the application callable which the application server uses to communicate with your code

As of now, only the project has been setup without any setting changes.
Next you have to create app(s) of your choices to actually do something of its own. These apps are pluggable django entities that can also be used by other projects like musicStore does.  Apps will be created in the next tutorial – Part 3 !

Confirm if project setup works 

$cd ~/Desktop/musicStore
$python manage.py runserver

Verify if the following output shows up :

Performing system checks…
System check identified no issues (0 silenced).
You have unapplied migrations; your app may not work properly until they are applied.
Run ‘python manage.py migrate’ to apply them.
February 29, 2016 – 15:48:41
Django version 1.9.1, using settings ‘musicStore.settings’
Starting development server at http://127.0.0.1:8000/
Quit the server with CONTROL-C.

Now visit : http://127.0.0.1:8000/

If django welcome page shows up you’re good to go. If not, please leave a comment.

Install pip – D awesome Python Package Manager

python-pip-and-the-staff-group-a

Folks, install pip n enjoy your python installations now :

  1. Visit https://bootstrap.pypa.io/get-pip.py
  2. Copy the content and save in a file naming it as get-pip.py
  3. Run the following command from the CLI in the directory where you saved the aforementioned file.
    $python get-pip.py
  4. If you don’t have permissions try :
    $sudo python get-pip.py

Congrats, pip is now installed in your system. Keep updating it regularly!

Now onwards, all you need to do to install a python package is:
$ sudo pip install <package name>

 

Django Basics : Part -1

django

INTRODUCTION

So, what is Django? It is a very popular web framework based on Python. I love it because it is fast and because it is Python. I like it because it is simple, easy to learn and has all the things necessary to build a web application with a full fledged 3 tier architecture ready. Its ORM or object relational mapping system is world class and so is its template system. As of today, the latest version of Django is 1.9.2.

The intention of this tutorial is to help you understand its components and help you build a basic Django website with a database and views, available to serve pages using is temporary server(Apache etc. are USEFUL in production). Django is huge and so is its documentation. So, I won’t delve beyond the point where you can tell the differences between a view, template, model and static pages in this PART 1. Oh wait! I forgot to inform you that this tutorial on Django is divided into multiple Parts and I will keep posting them one by one based on my time as I sometimes really get busy with my work. But you see, I love writing my blog posts and hence, I WILL find out some time to help my readers create awesome web apps!!!!

Before I proceed, I would like to inspire my readers by letting them know who are the players using Django today :

  • Instagram
  • Pinterest
  • Mozilla
  • The Washington Post
  • Bitbucket
  • Speir.tv
  • More…

BASIC COMPONENTS of a DJANGO APP

A Django app consists mainly of the following components :

  • Models
  • Views
  • Templates
  • URL Dispatcher
  • Database

the-django-web-application-framework-2-12-728Let me introduce every component briefly:

MODEL

It is the Model equivalent from the MVC Framework. For example, if your web application is about an online Music Portal having details of artists and their albums, there could be two probable models, namely, Artist and Album. Each model will have its own attributes. For example, Artist will have artist_id, name, age, twitter_id etc. as its attributes whereas Album will have album_id, name, date_of_release and artist_id as its attributes where artist_id would be a foreign key corresponding to the artist to whom the album is attributed to. It is simple stuff for you if you have prior object oriented programming experience. There’s a lot more about models which will be properly explained in a future tutorial, probably with a running example.

As a side note, as soon as you create a model a database entry corresponding to the model DOES NOT get created. But, once you ‘migrate’ the changes, the Database is updated with the new model data. This and a lot more will be explained later.

VIEW

The funny part is that this is NOT actually the equivalent of the View in the MVC architecture but more like the Controller equivalent of the MVC architecture. Confused? Don’t be. It is simple. Just get used to the idea of the View in Django being the main logic controlling component that handles the requests based on certain URL patterns and generates output accordingly. In a very simple tone, every view responds to a certain request by presenting a template in some form or the other. When I say a template…..

TEMPLATE

…it basically means the front end component in Django. It is filled or not filled with ‘context’ data that is , when sent, sent from the view that led the application to the template.  Generally, html files but sometimes could be anything else, like JSON, XML etc. Content type and other response variables are sent from the view.

In layman terms :

  1. Request_URL leads to View A
  2. View A processes Context_Data + Template
  3. Template presented with Context_Data

DATABASE

The actual database that generally the user or developer need not see. But can be easily seen. PostgreSQL, SQLite, MySQL…could be anything.

URL DISPATCHER

A file, urls.py contains all the URL mappings as what URL pattern leads to what view.

IMPORTANT METADATA

It is very important to note that a Django Project may contain 1 or more Django apps where each app comprises the aforementioned components. Now, I would like to explain briefly the directory structure of a Django project.

screen-shot-2009-09-14-at-2-08-32-pm

Here, project.domain.com is our Django project which has a testapp app inside corresponding to the parent project. As you can see,testapp has a file called settings.py which has all the settings for the entire project and there is ONLY 1 settings.py. The public directory is for publicly accessible, mostly static, files.

Note : Every directory in an app needs to have a file __init__.py to tell Django that it is a django directory.

To Be Continued…