How to deploy a Django app on Heroku. Part IV

This is the fourth part of a series on how to deploy a Django app on Heroku.

How to deploy a Django app on Heroku. Part I.

How to deploy a Django app on Heroku. Part II.

How to deploy a Django app on Heroku. Part III.

You will see that the main idea behind these posts is not to build a functional Django app. Instead, what I want to do is to give you a bunch of good practices on how to build a Django app with different environments for testing, developing and production, useful packages installed, Internationalization and Localization properly configured, PostgreSQL installed, and much more.

PART IV

The topics covered here are:

  • Install South and run it both on your local machine and on Heroku
  • Prepare your app to support different languages
  • Prepare your app to support Localization

Installing South

South is a very useful Django package that manages changes in your database. To install it, activate your virtual environment and type:

$ pip install South

You should include the installed version of South in your requirements.txt file. If you followed this tutorial from Part I, you should have created a common.txt file with the common requirements of the development, production and testing environments. Type

$ pip freeze

to see which version of South you have installed, and add that line into your common.txt file (for me it was South==0.8.4). If you only have one requirements.txt file, you can type directly

$ pip freeze > requirements.txt

to save all the dependencies needed to a single file.

On your settings.py add:

INSTALLED_APPS = (
    …
    ‘south’,
    …
)

As we don’t have any app created, we can sync the database to see if South is installed properly. If on the contrary, you have created an app, and you want it to be managed by South, don’t do this step yet.

$ python manage.py syncdb

We can do the same on Heroku:

$ git add .

$ git commit -m “South installed”

$ git push heroku master

$ heroku run python manage.py syncdb

Every time you make a change on your models, you should use South following these steps:

  • run a schema migration on your development environment
  • perform the migration on the development environment
  • push your changes on Heroku
  • perform the migration on Heroku.

Preparing your app to support different languages

Another thing you might need is to prepare your app to support different languages. First, create a folder named locale at the same level of your settings.py file.

$ mkdir locale

Open your settings.py file and make sure you have the internationalization flag set to true: USE_I18N = True. Then, add the following at the bottom (change your languages accordingly):

ugettext = lambda s : s
LANGUAGES = (
    (‘en’, ugettext(‘English’)),
    (‘ca’, ugettext(‘Catalan’)),
)
LOCALE_PATHS = os.path.join(BASE_DIR, ‘locale’)

And finally, add the locale middleware in the correct position:

MIDDLEWARE_CLASSES = (
    …
    ‘django.contrib.sessions.middleware.SessionMiddleware’,
    ‘django.middleware.locale.LocaleMiddleware’,
    ‘django.middleware.common.CommonMiddleware’,
    …
)

These are the first steps for preparing your app to allow different languages. Later on we will discuss the next steps.

Time zone

You might also want to change the variable TIME_ZONE in your settings.py file to your local time zone. In my case, that was

TIME_ZONE = ‘Europe/Madrid’

This is very important if you want to use Localization on your project.

This is the end of this Series of Posts on How to Deploy a Django app on Heroku. For more information about how to write your models, views and forms, stay tuned to this Blog! 😉

Hope it was useful! 🙂

Google+TwitterLinkedInFacebookReddit

Please, add +Marina Mele in your comments. This way I will get a notification email and I will answer you as soon as possible! :-)