Django – Part 2

Intro

Django’s slogan is “The web framework for perfectionists with deadlines.” It accomplishes this in a few different ways, forced compartmentalization, built in development tools, and every feature you could ask for. Below we will discuss how to get started, and how see how Django handles MVC.

Getting started:

You first want to setup a virtual env. I am using Anaconda, so I would type in:

  1. conda create –name <your env name> django
  2. source activate <your env name>

Then you would use Django’s great command line tools to create your first project:

  • django-admin startproject <your app name>

Once you the command above you will have your very first Django app. However it won’t do very much. You can execute the following command to run the application:

  • python manage.py runserver

As you can see its pretty boring and does not due much. Django is built around the idea of applications, being different modules in your over all application. These modules are separate from the main project and encapsulate the different features of your application. You create a Django app by then running:

  • python manage.py startapp <app name>

By running the command you get a new directory generated in your folder structure.

Getting started with MVC (MTV):

Dajango does not use the MVC design pattern per say, rather it uses the MTV design pattern. MTV stands for:

M – model : This is a simple python classes.

As you can see it is very easy to start building the inter model relationships. All models need to inherit from the Model class. This is all possible due to Djangos built in ORM. The ORM abstracts away the whole concept of having to work with a SQL DB, since it also provides a easy method revise your tables to reflect changes to your python models. This is simply done by using the:

  • python manage.py migrate
  • python manage.py make migrations < Dajango app name>

T – template : This is your html that displays model data through server side rendering.

Note that to serve these files, they need to be outside your main Django app, and they need to be reregistered in the Django settings file.

V – view : This is interestingly your controller.

As you can see above, you tie the data from your controller to your view by way of dictionaries. And tie your templates to your controllers to your views by specifying the url path name, and the file name.

A point of interest here, is that unlike some other frameworks your mapping a url to a method, rather then a whole class that contains different methods. This method mapping to is done using url.py files that exist both in the directory of the main project and the django app.

The main project directory:

The Django app:

As you can see your using regex, to find a specific url, then mapping it to a method. The main urls.py file contains the main urls for the whole project and imports the urls specifically for the Django app. Whereas the Django app just contains the sub urls for that particular app.

The “http://127.0.0.1:8000/first_app&#8221; would hit the “first_app” app, then it would then hit any of the sub urls stored in the Django app urls file.

As you can see Django is not that hard use at all, and in most ways is significantly easier then other frameworks, that don’t have all these built in features. In the next blog post I will be discussing Models, the Django admin console, and how the ORM handles model changes.

You can find part one here.

 

Getting Started with Express.js

Intro

Raise your hand if you already know the MVC design pattern ! … oh right you can’t.

You can look at one of my previous blog posts here. Now that we have gotten that out of the way. Express.js provides an incredibly simplistic and transparent way to develop APIs for your applications. It’s one of the most popular frameworks to run on the Node.js platform.

You simply define your routes, define what you want to send out and pull in, and your golden 👍

Setting it up:

Routes:

Here you can see the route in question is just a slash. That means that when ever anyone hits the https://”your domain”/, this route gets triggered (since it is a GET request). Once the route gets triggered it executes the function, that has the “req”, and “res” variables. The “req” is your request object, and the “res” is your response object. You can put different things in the res object and it gets passed along to be rendered in HTML.

This same sort of logic can be applied to POST and PUT requests.

Folder Structures:

You can use a variety of different folder structures. What ever folder structure you use, make sure its suited for the type of app your going to be building. You can checkout an example over here.

And your done ! Easy as that. You may be thinking WTF is that really it ? Why do people want to use Express.js if this is it ? Well the answer is that the frame work does not lock you into, using any specific way of doing things. Therefor you can use any sort of authentication, object relational mapper, session manager, or data base you want.

You can find the best 40 packages most commonly used here.