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.

 

Intro to Python ? – Part 2

You can find Part 1 here.

Intro

In this part of the series we are going to cover the very basics of Python. You may be asking: “Its Python, how hard can it get ? Its just sudo code” well that is both true and false to a degree.

Variables

Python is a dynamically typed language, however it is strongly typed. This just means that you can set a variable to any sort of data type you want without declaring it first. However it doesn’t implicitly convert types for you.

Collections

Python also contains different types array like structures. And they are all get dynamically sized of course. You have Lists and Dictionaries, they all work the way you intuitively think do, coming from another programming language. The only new sort of collection type, you will encounter is the Tuple. A Tuple is pretty much the same thing as a List however they are immutable and can not contain repeats of the same value.

Control Flow

It’s pretty basic.

Loops

Python does not find “for” loops pythonic that is way it favors “For In” loops instead. maybe asking: “What if I just want to print something 3 times ? do I have to make a list with three elements ?” The answer is no, you can use a “generator” shown in the gists below. Python also has something called “list comprehension” its very nifty to write compact and concise code.

 

You can go here to find all the relevant code, in order.

Django – Part 1

Django_Reinhardt_(Gottlieb_07301)
Django Reinhardt a jazz legend

No I am not talking about this guy to the left.

I am actually talking about the web framework written in Python. Now the reason that I have even started learning this, is cause I wanted to see what all the hype is about. Plus I wanted to get a better understanding of the Python language its self.

Now I come from the world of Express.js which is kinda of like Flask, in terms of its bare-bones nature. So when I first saw Django I was reminded of ASP.NET, and Ruby on Rails. You might be thinking what sort of nonsnese is this kid spouting ? How the can he put two of the most loved opens sourced projects next to, Microsofts evil ways. And to that I respond with: A) Microsoft is not evil, and B) I say that since all three of these frameworks have so much built in functionality, that they become opaque. Opaque as in not transparent as in have to use for along time to really understand how they work.

Anyways this is a post letting you guys or gals out there know, I am going to be making a tutorial series as I start to learn the framework.

Yes I know that all my posts are starting to seem like tutorials or updates. I promise they will eventually be about something else 😅

Intro to Python ? – Part 1

Intro

You may have seen the title and been a bit surprised. In a previous post here, I said I don’t like Python. So you might be asking why I am even writing this. It’s cause I decided to give Python another shoot to wow me. Therefor I am going to start going into the cycle of learning a bit about it and then writing about it, and so on.

What is it ?

Its just a general purpose programming language.

What type of programming language is it really ?

It is a interpreted , dynamically typed, and strongly typed.

What can it do ?

Just like Java, C#, or C, it can do anything. That includes everything from Machine Learning to Desktop App Development.

What are it’s main selling points ?

  • Readable syntax to the point where, you can mistake it for pseudo code
  • Hugh and active community that supports it
  • Large selection of libraries
  • Large enterprises use it and actively contribute to it, such as Google, Reddit, The New York Times, and etc.

What are the draw backs ?

  • Its interpreted so it won’t be as fast as a compiled language
  • Far from the metal aka a thick layer of abstraction
  • No static typing
  • Object Oriented paradigm isn’t flushed out, for example there are no accesses modifiers on class variables (instance variables)
  • There are two different versions of language 2.7 and 3.x
    • Some libraries choose not to move to 3.x

 

Why I don’t like Python

Yes I know that statement above is pretty much unheard of. Before I explain, let me give you some background.

So as we all know there is no such thing as the “best” language, i.e the language that you should use for everything. Each language has its own uses in its own specific problem domain. Since each language has its own unique strengths, weakness, and paradigms that it caters to, like Java with OOP or Scala with functional programming.

For example if you want to write fast, highly efficient code that can run on embedded systems like those found in a CAN-Bus hub, you should use C, C++, or assembly. Where as if you wanted to write super interactive web applications you should use some form of Java Script, or something that ends up compiling to Java Script.

However in this day and age, almost every single widely used programming language is multi paradigm and can compile to something that can run on multiple platforms.

So the question is “With so much selection, why use Python ?”.

Python is a language that is almost universally loved by small children, to Google engineers. One of the main reasons for it adoption and wide use is that it’s syntax looks like pseudo code. Which is a great thing, since it lets you tell a machine what to do in a almost human like language. Thus the difference between what you want the computer to do, and what you have to write in order for the computer to do it, becomes minimal.

My biggest problem with Python is that it forces you to write code in a certain way. The pythonic way aka the thoughtful, zen like way, stressing “less is more” and “clean code is good code”. This way of programming does not suit us all. Some of us are cowboys or cowgirls, we shoot first and think later. Us cowboys and cowgirls like to really experiment with what works and what does not work, we really like to tinker with our code till we get it right.

Other languages don’t really care, they know that they are. They are simply a means to an end. You can be that gun-sling cowboy or cowgirl that codes before even having a good idea about what he or she has to do. You can get into a stack-overflow frenzy of copy pasting code, ripping out what works and what doesn’t. And you eventually end up with something that works. You may ask “can’t you do that with python too ?” well kinda… see python does not have the greatest piece of programming syntax ever. It doesn’t have the “}” . So if you trying to be that cowboy or cowgirl you will end up with parsing errors galore or end up have to re-indent all of your code, just to get it to run.

Honestly I think python would end up being the greatest programming language ever, if it just had curly braces 😦