Re-discovering Django

A few years ago I had a job where I mostly did Django development. The sites were mainly informational - a place to host text and pictures that were easy for non-technical people to edit. Primarily they were cookie-cutter replicas of previous websites with some updated content. Django was a good fit for this, especially when combined with one of the CMS packages built on top of it. There were plenty of other good options as well, however. Drupal, Wordpress and others are fine for this type of site.

Eventually, I was ready for a change. I moved on to other challenges that didn't involve Django. Micro-frameworks and APIs were the thing, and I spent time hanging out in the land of Flask.

Recently, I've been motivated to do some side projects, have dipped my toe back into the Django pool, and oh yeah, the water is nice. Lots of included batteries, less time reinventing the wheel. Migrations that just work without a lot of fuss. It's good to be back.

But isn't Django some crusty relic of a previous era? Isn't Jamstack and Serverless the new hotness? Well, not so fast there, buckaroo - server-side frameworks like Django and Rails still have a lot of life left in them.

The tagline for the Django Project is "The web framework for perfectionists with deadlines". This is a nice summation, because Django is great for getting stuff done. Templating, database access, an admin site, form validation, APIs, security - all solved problems (except the last one of course, which will never be truly solved, but Django takes security very seriously).

Django, for those not familiar with it, is based on Python. If you have a recent version of Python installed (3.6+), it's easy to get started. First, create a new directory and Python virtualenv for the project:

$ mkdir mysite
$ cd mysite
$ python3 -m venv --prompt mysite venv
$ . venv/bin/activate

Folks on Windows and PowerShell can use the same commands, with a slight difference in how the environment is activated:

PS C:\mysite> .\venv\Scripts\activate

Once that's done, you have the environment for your project ready to go. These are the same steps you'd use to create essentially any Python project. Next, the Django-specific stuff:

(mysite) $ pip install Django
(mysite) $ django-admin startproject mysite .
(mysite) $ python manage.py migrate
(mysite) $ python manage.py runserver

The above lines do the following:

  1. Install the Django web framework into the virtual environment you created above
  2. Create a new website project titled "mysite" in the current directory
  3. Run the initial database migrations to setup the site database (using the built-in sqlite database engine)
  4. Run the development web server so you can view the site locally.

Four simple commands and you have a website. At this point, you can browse to localhost:8000 to view your new site. It will be a simple splash page for the Django project, and there are links to the documentation, a starting tutorial, and how to connect with the (substantial) Django community.

Under the hood, a basic framework has been created with plumbing for authorization, sessions, and more.

Next time we'll, explore what you can do with your shiny new website project. Until then...

No Comments Yet