Starting Our First Django Project

Now that we have a working virtual environment created in our free Python Anywhere account and on our local development system, we are ready to begin creating our project files and applications.

To start a new project, use the django-admin command and fire up the Ubuntu terminal.

> craigderington@ubuntu $ django-admin startproject blog

In Django, we have a root project folder which contains our project's , , and This is usually the same name of the project.

If you wish to view the directory structure in the terminal, enter the command below.

tree blog  

This will show you the files that the django-admin created for us.

tree blog  
| --
| --
| --
| -- 

This is the beginning stages of our project. We have a few edits to make to our file before running the migrate and createsuperuser commands.

In , enable the Django admin in the INSTALLEDAPPS section and set up the static root folder. You should also set the BASEDIR and the template directory so Django knows where to find the site's templates.

You should also ensure the database backend is set to SQLite3.

Now, run your migrate command from the terminal.

craigderington@ubuntu $ python migrate  

and then create your super user so we can login to the built-in admin site Django creates for us.

craigderington@ubuntu $ python createsuperuser  

OK, now launch the built-in development server by running the runserver command.

craigderington@ubuntu $ python runserver  

This starts a light-weight web server for development purposes. It should be noted that this built-in server should never be used in a production environment.

Fire up your web browser and navigate to

Login using the credentials you created during the createsuperuser step.

Awesome. We have a working admin section. Now, let's start scaffolding our apps. In Django, apps exist outside the project root. This is contrary to almost all other web frameworks where the project and site files all exists under a single common root folder. Django's modular design is meant to allow you to create apps and use them in any project.

We are starting our project with a simple blog... so, from the terminal, enter:

craigderington@ubuntu $ django-admin startapp myblog  

Django creates most of the necessary files we need to build our blog application. If we view the directory structure for myblog, we'll notice this folder heirarchy:

tree myblog  
| --
| --
| --
| --

We will need to add once we get to the step where we need to set up routing. But we can do that later. For now, we need to start by creating the model for our blog.

I use Atom as my default text editor, but you can use whatever you want.

from django.db import models  
from django.contrib.auth.models import User  
from django.core.urlresolvers import reverse

# create your models
class BlogEntry(models.Model):  
    title = models.CharField(max_length=200, help_text='Enter the article title')
    slug = models.SlugField(max_length=200, unique=True, help_text='Enter the article URL slug')
    body = models.TextField()
    publish = models.BooleanField()
    created = models.DateTimeField(auto_add_now=True)
    modified = models.DateTimeField()
    tags = models.ManyToManyField(Tag)

    objects = BlogEntryQuerySet.as_manager()

    def __str__(self):
        return self.title

    def get_absolute_url(self):
        return reverse('detail', kwargs={'slug':self.slug})

    class Meta:
        verbose_name = 'Blog Entry'
        verbose_name_plural = 'Blog Entries'
        ordering = ['-created']

Please note that the model class for the ManyToManyField for the blog Tags has not yet been created, so any migrations run on this current model will fail validation. I also want to take a moment and explain the BlogEntryQuerySet.as_manager() (new in Django 1.7) above so you have a solid understanding what this custom Manager is doing as far as retrieving records from the database.

Craig Derington

Secular Humanist, Libertarian, FOSS Evangelist building Cloud Apps developed on Red Hat Enterprise Linux and Ubuntu Server. My toolset includes Python, Celery, Flask, Django, MySQL, MongoDB and Git.

comments powered by Disqus