Python with Django pointers – Part 2: Apps, MVC(T) and Models

Django Apps

A Django project is typically formed from several Apps, each pertaining to a certain function within a website. Usually, I would expect to create one App for say, site updates and announcements, another App for commercial transactions with a shopping cart and checkout functions, yet another App for a forum etc.

Django comes with a built in Admin Site App, allowing trusted site administrators to quickly create, edit, delete and otherwise manage content. This is considered a common function that is required for just about ANY website, and covers things like management and authentication of users, display and management of forms, input validation, and translating all that into SQL statements for updates into the database. Having this by default really saves a lot of hassle when building a website and is a key feature available in the Django Framework.

Overall, since Apps can be made up of other Apps, it is easy to take a top down approach in designing the overall website functions, as breaking them down into Apps (or smaller Apps within an App) would make it much easier to architect the entire system. The team can then focus on individual Apps which will generate the functionalities required.

Key things for Apps:

  1. Use “python manage.py startapp name_of_app”
  2. Edit “settings.py” at the INSTALLED_APPS line with name_of_app.apps.Name_of_appConfig

Every App typically embodies the MVC structure which is what makes Apps so powerful because it is “self-sufficient” and can thus be replicated and utilized in different projects, with some minor tweaks done to the settings.

The MVC Structure

One of the coolest things about frameworks, is the ability to manage data separately from the logical layer, as well as the presentation layer. The MVC structure really makes things easier as you can split the management of all three layers making it cleaner and faster to make changes.

Edit: I previously stopped at describing Django as using the MVC concept above, but would like to correct it by saying that in there is a difference from a typical MVC structure as opposed to Django’s MVT structure. I think this explains the concept a lot better!

Models

Models are the foundation for data that drives your website. Every piece of dynamic data has to be defined in a model and stored in the database. Being able to manage how data is structured is quite typical in a framework, and Django does this very well by providing migration functions. Changes to the database level structure can be affected by using the “check”, “makemigrations” and “migrate” commands. This ensures consistency while developing where you can choose to update a column of a table for example by simply running these commands.

Of course, Django’s models can also have additional attributes which is on top of the database layer, making them extremely flexible.

Recap

So just a few key things for me:

  1. In models.py of an app, use following syntax
    class Name(models.Model):
    field = models.TypeField()
  2. Add this to Model Class to display human readable objects in models
    def __str__(self):
    return self.field
  3. To add a model to Django Admin, in admin.py, use following syntax
    from .models import Modelname
    admin.site.register(Modelname)
  4. For date, time and numbers data types (non-string), to allow blank fields, use
    field = models.TypeField(blank=True, null=True)

Python and Django pointers – Part 1: Using Virtualenv and Context Syntax

So I’ve decided to try (again) to learn how to code a full Web Application in Python using Django as the framework.

The number of false starts in my endeavour is enough to make me discouraged, but I just can’t totally let it go and I seem to keep coming back to it. Somehow, the desire to actually build something is still there, and I’m just hoping that this time I get somewhere. I’m blogging this experience in part to keep myself in check, and to also document some of my learnings so that I can always come back to it.

I would’ve loved to be coding on a MacBook, but I don’t have one and until I reach a stage where it would be more productive for me to get one, it would make more sense for me to continue using the existing Windows based laptops I have.

First, a few things I’ve (re)learned:

  1. Use pip to install virtualenv in Windows Subsystem for Linux (WSL or Windows Bash). Using Virtual Environment to manage Python Environments and versions are essential to making sure your development environment is consistent. It should essentially be part of your workflow.
  2. Context syntax for multiple variables is as follows: { ‘var_name’: ‘value’, ‘var_name2’: var}
  3. Context syntax for dictionary is as follows: {‘dictionary_name’: {‘var_name’: ‘value’}}
  4. Use “django.shortcuts import render” and then “return render(request, ‘template.html’, {‘context’: bar})”

Ok that’s about as far as I got. Considering it’s only a few hours in, with so much more to go, I really needed to put this down so I can make sure I keep going. At least it’s a start.

Next step is Apps and Migrations within a Project.