Create and deploy a Django application using Contentful

This guide will walk you through your first steps using Contentful within a Django application. It will provide a step-by-step guide on how to get your first entries and start using the content you create on Contentful.

Requirements

  • A Heroku account
  • Python 2.7+ or 3.3+
  • Git installed
  • Basic Command Line Interface, Python and Django knowledge

Note: During this guide we'll assume that you're running inside a virtualenv. If not, append sudo when installing dependencies if you are using system-provided Python.

Optionally:

  • Ruby 1.9+ for running contentful-bootstrap to create your own space with the provided template

Start from a demo application

First we'll start with a demo project, which is a simple Django application using a basic Blog template, so that you can see it running for yourself.

Run the following commands to get started.

  • Clone the example repository:
1
git clone https://github.com/contentful/contentful_django_tutorial.git
  • Navigate into the repository's directory:
1
cd contentful_django_tutorial
  • Install dependencies:
1
pip install -r requirements.txt
  • Optional Create your space using Contentful Bootstrap:

Note: If skipped, the example will use a read-only space that's already provided for you.

1
2
bundle install
bundle exec contentful_bootstrap create_space django_demo -j bootstrap/template.json

After following the steps, the tool will provide you with a Space ID and Access Token. Then run the following commands replacing the values where they correspond:

1
2
export CTF_SPACE_ID=YOUR_SPACE_ID
export CTF_DELIVERY_KEY=YOUR_ACCESS_TOKEN

It should look something like this:

Bootstrap Tool Output

This will allow the application to use your own space, allowing you to edit content and allowing you to see your application changing. You can edit content freely in our Web App.

  • Run the application:
1
python manage.py runserver

Everything is now set up. You can view your space by opening http://localhost:8000 in your browser.

It should look something like this:

Django Demo Application

Deploy the demo to Heroku

To view the demo application live in your own production environment, follow these steps:

  • Install the Heroku toolbelt if you don't already have it.
  • Login to Heroku if you're not already:
1
heroku login
  • Create a new instance:
1
heroku create
  • Set Python Buildpack:
1
heroku buildpacks:set heroku/python
  • Deploy to Heroku:
1
git push heroku master
  • Optional Add your personalized credentials to your Heroku Instance:

    1. Open Heroku
    2. Go to your application
    3. Go to Settings
    4. Press Reveal Config Vars
    5. Add CTF_SPACE_ID and CTF_DELIVERY_KEY with their respective values
    6. Go to Resources and restart your application
  • Open the application in your browser:

1
heroku open

Next Steps

After this guide, you should be able to start using Contentful with your Django applications, but every project has different needs and we want to provide you with the best solutions we can.

You can read about the Contentful CDA library in more detail on our contentful.py GitHub or our Getting Started with CDA SDK tutorial, we also suggest taking a look at our Contentful Django Example Application.

If you want to set up new spaces via the command line we also provide Contentful Bootstrap.