Day 19+20 Django-tastic

This post is a little late, but I’ve been doing well the past two days. Django all day, every day! On Thursday, I picked up more of the basics by porting a bunch of completed Flask assignments over to Django. Ninja Gold, Disappearing Ninjas, and Dojo Survey to name a few. Doing so gave me practice in correctly formatting regex patterns for my routes as well as using the new templating language rules. The Django templating language is pretty similar to Jinja2, but there are a few differences that make it better, in my opinion. The first thing I noticed was that square brackets are not allowed in Django template expressions. Passed variables’ values are accessed only by . notation, which I think is great. Now there’s only one period to type instead of two brackets and two quotes. The other difference I noticed has to do with linking static files. The url syntax for linking files in Jinja2 is kinda ugly: "{{ url_for('static', filename='css/styles.css') }}". I think the Django syntax looks much nicer: "{% static, 'app_name/css/styles.css' %}" Though with Django there are a couple extra bits that are required. Before linking files, I have to put this in the head: {% load staticfiles %}. Also, all forms that have post methods need to have a {% csrf_token %} in between the opening and closing tags. These differences are pretty minor and it didn’t take long to get used to them.

By the end of Thursday I was getting into the Django ORM (which stands for Object Relational Mapping). I’m lovin it! The ORM is awesome. I still don’t really know how to describe what exactly an ORM consists of, but what I do know is that it allows me to interact with Django’s built-in SQLite database super easily. It turns model classes I define into tables and then provides me with methods via a built-in model manager to get, add, update, or delete database rows without ever writing a line of SQL. It’s great.

On Friday I spent most of the day figuring out how to use all the model Manager methods. See the sports_orm assignments (1, 2) for examples. By the end of the day I was writing my own Managers to add custom functionality. One example is for a User class, I over-wrote the built-in model manager with a UserManager class I defined that inherits all the methods of the default model manager and adds .login() and .register() methods. These let me handle the whole validation and database insertion process within the User class. By creating custom methods on my models like these to do most of the number crunching I am able to clean up the logic in my views.py and keep my controller methods concise and readable. I’m definitely liking Django.

On an unrelated note, Friday was also potluck day! Everybody at the dojo brought in food and we had a huge feast. There was some pretty good home-made dishes, and plenty of desserts! I brought in two big tubs of Oyako Yakisoba! It’s pretty nice that Coding Dojo organizes fun little events like these every once in a while. Getting a chance to chill out, eat and socialize is a welcome break in such an intense bootcamp.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s