Python and Django Part 1

python djangoIn my last article, PHP vs Python Part 1, I was writing about how much I love Python. Although it’s a great programming language packed with lots of nice libraries and other neat stuff, if you are looking to build a web app, then a framework will help you a lot (if you are not one of those guys who like to write everything from scratch or if your project allows for the use of frameworks)!

Before starting on my Python project, I researched a bit on the internet and decided to use Django. I don’t think I could have come up with a better decision. Developing with Django, for me, was like flying on a freakin’ pterodactyl… with Scarlett Johansson!

It is a bit harder to install than a PHP framework, but don’t get discouraged! The good things don’t come easy in life! ;) Once you get things rolling, it’s as easy to develop with as any other framework, maybe even easier than most frameworks.

One of its strengths comes from the fact that it’s a Python framework. You can do things with it that you cannot possibly do with a PHP framework. Even if you can do them with a PHP framework, I’m sure that Djangowill enable you to do them easier. Let’s take a look at the following example:

PHP and Yii:

        Reporter::model->findByAttributes(array('full_name' => 'mith'))
    

Python and Django:

        Reporter.objects.get(full_name = 'mith')
    

See how in the PHP framework you need to pass an array, when in Djangoyou simply pass the field’s name? This is just one of the many differences between Djangoand PHP frameworks.

As you may have noticed from the short, but precise, example above, Djangolooks pretty simple, that’s why it is easy to learn and use, but it might take at least a couple of weeks to get the hang of things, because it is so feature-rich.

The documentation is great and there are plenty of resources on the internet, so you won’t have to worry about not knowing how to get things done.

Like other frameworks, Djangois a MVC framework. A few slight differences are that the controllers are called “views” and the views are called “templates”.

The language syntax of the Djangotemplate system is easy to learn even by those not accustomed with programming as it does not involve writing Python code (like a lot of PHP frameworks which embed PHP into HTML).

At a 1st glance you may think that the template system is a bit rudimentary, but it does just what it is supposed to do: to express presentation, not program logic, that’s why the data you send to the templates needs to be as easy to render as possible, and you won’t have to worry about messy templates.

The template system comes with quite a lot of built-in tags and filters, which will make your life pretty easy. Moreover, if those are not enough, you can write your own.

If you don’t like Django’s template system, you can chose to use another one. It’s up to you.

There is so much interesting info to share, but I will leave this for future articles. So please stay tuned and I hope you liked my little introduction to Django.

Leave a Reply