This is the fourth part of a series on how to deploy a Django app on Heroku.
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.
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
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:
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):
And finally, add the locale middleware in the correct position:
These are the first steps for preparing your app to allow different languages. Later on we will discuss the next steps.
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! 🙂
Marina Mele has experience in artificial intelligence implementation and has led tech teams for over a decade. On her personal blog (marinamele.com), she writes about personal growth, family values, AI, and other topics she’s passionate about. Marina also publishes a weekly AI newsletter featuring the latest advancements and innovations in the field (marinamele.substack.com)