Beginning a Blog - Django Web Development Tutorial

In this sixth Django web development with Python tutorial, we're going to cover working with models in Django. Our plan is to add the final element of our website: a blog. So far, we've worked with views and a controller, but have not really done anything with models.

First off, what are models? Models contain "meta data" regarding your application's data. Generally, these models are used to describe just the elements in your database, but they can be built up to be much more than just that.

Second, we know we want to make a blog. Is this blog a part of our "personal" website, or would it make more sense to be its own app? It could be a part of our personal app, but it really ought to be its own entirely. Plus, chances are, we're going to want to incorporate a blog on other apps, so let's make a new app: python startapp blog.

We just added a new app. What is the first thing we should do? Right, install it!

mysite/ (just a portion of it)


Next, we need a path to blog. /blog/ seems like a reasonable path to start with, thus in mysite/

from django.conf.urls import url, include
from django.contrib import admin

urlpatterns = [
    url(r'^', include('personal.urls')),
    url(r'^blog/', include('blog.urls')),

If we're going to go about including blog.urls, I suppose we had better make it, but, actually, first we're going to make a!

from django.db import models

class Post(models.Model):
    title = models.CharField(max_length = 140)
    body = models.TextField()
    date = models.DateTimeField()

    def __str__(self):
        return self.title

Each class here is basically a database table. Each variable is a table column, and then a datatype and maybe some further attributes if you like, like max_length and so on. If you wish to reference these objects and get something back besides that it is a Post or Category object, then you need to define the __str__ method. If you are using Python 2.7, shame on you!... but you need to do __unicode__ instead of __str__.

This file is straight forward, especially once you know all of the possible datatypes and such. Check out the Django Docs for the Model Fields, you have a lot of options.

After we define our model, we're ready to define our blog/ file, set up the views, and go live, which is what we're going to be doing in the next tutorial.

Download the entire site's code for this tutorial here: Part 6

The next tutorial:

  • Django Web Development with Python Introduction
  • First Website - Django Web Development Tutorial
  • Jinja Templates - Django Web Development Tutorial
  • Design with HTML/CSS - Django Web Development Tutorial
  • Jinja Variables - Django Web Development Tutorial
  • Beginning a Blog - Django Web Development Tutorial
  • Views and Templates - Django Web Development Tutorial
  • Database migrations - Django Web Development Tutorial
  • Admin control panel - Django Web Development Tutorial
  • Finishing blog - Django Web Development Tutorial
  • Publishing Django Project to a web server tutorial
  • Securing Django web server with SSL - HTTPS and Lets Encrpyt