Skip to content

Intro to Django Part 2

christine edited this page Feb 17, 2017 · 10 revisions

Learning by Doing

In order to get familiar with the fundamentals of Django, let's practice making a Django project from scratch. To start, let's create a new folder in your project directory:

mkdir fec-example
cd fec-example

Activate your virtual environment.

Once your virtual environment is activated, check your Python version to make sure it is 3.4+:

python -V

Install Django

pip install django

Start a New Project

With Django installed, let's a start a new project:

django-admin startproject fec .

The startproject command creates a /fec folder that contains starting project files.

Note: Project names have to be a character only string, no dashes or underscores allowed.

Create An App

python manage.py startapp blog

This creates a /blog folder containing app specific files.

Init Database

For this example project, we can just use a flat sqlite database for simplicity. To initialize the database:

python manage.py migrate

This will create a db.sqlite3 where our data will be stored.

Setup First App

In /fec/settings.py, add the blog to the list of installed apps:

INSTALLED_APPS = [
    'blog.apps.BlogConfig',
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
]

Setup Model

For our blog app example, let's define a very simple model in /blog/models.py:

from django.db import models

class BlogEntry(models.Model):
      title = models.CharField(max_length=200)
      entry = models.TextField()
      published_date = models.DateTimeField('date published')

      class Meta:
          verbose_name_plural = "Blog Entries"

A basic title, entry, and published date for a blog entry.
We are also setting a verbose pluralization of "BlogEntry" for the admin site so it doesn't show up as default "BlogEntrys."

Make Migrations

Every time we add, change, or delete fields in the models.py file, we need to make a migration file in order to instruct changes to the database.

python manage.py makemigrations

After running this, we should get this output from the terminal:

Migrations for 'blog':
  blog/migrations/0001_initial.py:
    - Create model BlogEntry

This means the model migrations were successful. To actually enact change in our database, we must next run:

python manage.py migrate

To which we should get this output:

Operations to perform:
  Apply all migrations: admin, auth, blog, contenttypes, sessions
Running migrations:
  Applying blog.0001_initial... OK

It ran the migration successfully and now our database schema is updated.

Setup Django Admin

Create an admin user/password to log into the admin site:

python manage.py createsuperuser

Then run the development server:

python manage.py runserver

And login at http://127.0.0.1:8000/admin/

Currently we only see Groups and Users administration, but we want to modify blog entries from the database.
To do this, we add this to /blog/admin.py:

from django.contrib import admin

from blog.models import BlogEntry

admin.site.register(BlogEntry)

Now we can add some blog entries into the database directly through the Django admin site.

Clone this wiki locally