Was this page helpful?

Getting started with Contentful and Django

This Django getting started 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 getting started 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 getting started 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:
git clone https://github.com/contentful/contentful_django_tutorial.git
  • Navigate into the repository's directory:
cd contentful_django_tutorial
  • Install dependencies:
pip install -r requirements.txt
  • Optional Create your space (may result in additional charges if the free spaces available in your plan are exhausted) using Contentful Bootstrap:
Note: If skipped, the example will use a read-only space that's already provided for you.
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:

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:
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:
heroku login
  • Create a new instance:
heroku create
  • Set Python Buildpack:
heroku buildpacks:set heroku/python
  • Deploy to Heroku:
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:

heroku open

Next Steps

After this guide, you should be able to start using Contentful with your Django getting started 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 client library 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.

Next steps

Not what you’re looking for? Try our FAQ.