Skip to content

Marina Mele's site

Reflections on family, values, and personal growth

Menu
  • Home
  • About
Menu

How to deploy a Django app on Heroku. Part II

Posted on December 23, 2013May 18, 2015 by Marina Mele

This is the second part of a series on how to deploy a Django app on Heroku. If you haven’t looked at the first part, you might want to do it now. Go to How to deploy a Django app on Heroku. Part I.

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, PosgreSQL installed, and much more.

PART II

The topics covered here are:

  • set different settings.py files for development, production and testing
  • save all the templates of your app in the same location
  • set debug false in production
  • install HTML5 Boilerplate

Django settings – Advanced configuration

On thing you might want is to have different settings.py files for development, production and even testing.

We leave the normal settings.py file for production (on Heroku) and we define two different files for development and testing. These files are called development_settings.py and test_settings.py and are located at the same level of settings.py.

touch development_settings.py test_settings.py

These files will contain the settings that are specific to this environment, but also import all the global settings (which in this case is the file settings.py). Write, in each of these files:

# -*- coding: utf-8 -*-

from .settings import *

DEBUG = True

TEMPLATE_DEBUG = DEBUG

Note: the dot before settings is not a typo 🙂

Then, on the virtual environment used for development, append this to your bin/activate script:

DJANGO_SETTINGS_MODULE=”myproject.development_settings”

export DJANGO_SETTINGS_MODULE

where myproject is the name of your main project. In the script bin/activate of the virtual environment for testing:

DJANGO_SETTINGS_MODULE=”myproject.test_settings”

export DJANGO_SETTINGS_MODULE

OS X Mavericks Error: When working with these virtual environments, if you run

$ python manage.py validate

you might see an error with something like:

Referenced from: myvirtualenv/lib/python2.7/site-packages/psycopg2/_psycopg.so

Reason: image not found

To solve this error, open the bin/activate script and add the following line:

export DYLD_LIBRARY_PATH=/Library/PostgreSQL/9.*/lib

where you should replace * for your PostgreSQL version.

Templates App

Another useful thing is to have all your templates in the same place. You can accomplish this by creating a Django app and use it to contain the templates and the template tags. Go to your project folder, at the same level of the settings.py file, and type:

$ python ../manage.py startapp mytemplates

This way, this app will be inside the myproject folder. Finally, you should include this app in the settings.py file:

INSTALLED_APPS = (
    …
    myproject.mytemplates
    …
)

In mytemplates folder, where you have the usual files of a Django app, create a templates folder

$ mkdir templates

Inside this folder you should place all your project templates. A good practice is to create a different folder for each app on your project. Moreover, the templates folder will contain the error pages: 403.html, 404.html and 500.html.

$ cd templates

$ touch 403.html 404.html 500html

For now edit them and write something like “Error page 403” (if you have more time you can make them nicer…). In this folder, you can also place your base.html template, that will be used throughout your application.

Note: if you want to create template tags, I recommend placing them on a new folder inside your mytemplates app, at the same level of the templates folder, called templatetags.

Setting debug False on production

In the production environment, in Heroku, we don’t want to use the debug mode. Therefore, in your manage.py file, change

DEBUG = False

TEMPLATE_DEBUG = DEBUG

and then, upload the application on Heroku

heroku login

git add .

git commit – m “Different settings.py files”

git push heroku master

With this configuration, if you followed Part I of this post, you should see the error message you put on the 404.html template. This is because your don’t have yet your urls.py file configured.

HTML5 Boilerplate

If you want to create a nice and responsive Django app, I recommend you install HTML5 Boilerplate. Go to the webpage and click the Download button to download a zip file (in my case, it was the version 4.3.0).

Once in your computer, unzip the file and place its contents inside myproject/mytemplates/templates folder. Note that you should override the file 404.html. Moreover, I usually change the name of the file “index.html” to “base.html“, but it’s up to you.

However, the two folders js and css, and the two images favicon.ico and apple-touch-icon-precomposed.png, contain or are static files. Go to the projects folder, at the same level of the settings.py file, and create a static folder:

$ mkdir static

$ mv mytemplates/templates/{js,css,favicon.ico,apple-touch-icon-precomposed.png} static/

Then, open the base.html file, inside mytemplates/templates, and change the urls that point to these two folders. For example, you should change the lines

<link rel=“stylesheet” href=“css/normalize.css”>
<link rel=“stylesheet” href=“css/main.css”>

<script src=“js/vendor/modernizr-2.6.2.min.js”></script>

to 
<link rel=“stylesheet” href=“{{STATIC_URL}}css/normalize.css”>
<link rel=“stylesheet” href=“{{STATIC_URL}}css/main.css”>

<script src=“{{STATIC_URL}}js/vendor/modernizr-2.6.2.min.js”></script>

and so on. To include the icon images write in the header:

<link rel=”icon” type=”image/png” href=”{{STATIC_URL}}apple-touch-icon-precomposed.png”>

<link rel=“shortcut icon” href=“{{STATIC_URL}}favicon.ico” type=“image/x-icon”>

Moreover, I usually comment the script for Google Analytics to use it later.

To see the effects and assure that everything works, edit the myproject/urls.py file:

urlpatterns = patterns(”,

    url(r’^$’, ‘myproject.views.home’, name=’home’),

)

Then, edit or create the myproject/views.py file:

# -*- coding: utf-8 -*-
from django.shortcuts import render_to_response
from django.shortcuts import RequestContext
def home(request):
    return render_to_response(“base.html”, RequestContext(request, {}))

Note: the first line indicates the coding to use. It is a good practice to insert this line at the beginning of the files of your django application, like views.py, admin.py, models.py, and settings.py.

Moreover, to see more clearly if the static files are rendered, edit the myproject/static/css/main.css file and include:

body {

    background-color: #DAF;

}

Try your Django project locally, with foreman start, to see if everything works as expected. You should see the text “Hello World! This is HTML5 Boilerplate.” and a purple background.

Then, upload the application on Heroku:

heroku login

git add .

git commit – m “HTML5 Boilerplate added”

git push heroku master

Hopefully, you will see the same text and background on Heroku 🙂

This is the end of this post. The third part is available here –> Part III

Please, click on the g+1 if you found this post useful! 🙂

Recommendations and comments are welcome 🙂

Marina Melé
Marina Mele

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)

Leave a Reply Cancel reply

You must be logged in to post a comment.

Categories

  • Personal Growth and Development
  • Artificial Intelligence
  • Mindful Parenting and Family Life
  • Productivity and Time Management
  • Mindfulness and Wellness
  • Values and Life Lessons
  • Posts en català
  • Other things to learn

Recent Posts

  • BlueSky Social – A Sneak Peek at the Future of Social Media
  • The Incredible Journey of AI Image Generation
  • AI and Fundamental Rights: How the AI Act Aims to Protect Individuals
  • Overcoming Regrets: Finding the Strength to Move Forward
  • Thinking Outside the Box: Creative Problem-Solving with Critical Thinking

RSS

  • Entries RSS
Follow @marina_mele
  • Cookie Policy
  • Privacy Policy
©2023 Marina Mele's site | Built using WordPress and Responsive Blogily theme by Superb
This website uses cookies to improve your experience. If you keep navigating through this website, we'll assume you're ok with this, but you can opt-out if you wish.Accept Read More
Privacy & Cookies Policy

Privacy Overview

This website uses cookies to improve your experience while you navigate through the website. Out of these, the cookies that are categorized as necessary are stored on your browser as they are essential for the working of basic functionalities of the website. We also use third-party cookies that help us analyze and understand how you use this website. These cookies will be stored in your browser only with your consent. You also have the option to opt-out of these cookies. But opting out of some of these cookies may affect your browsing experience.
Necessary
Always Enabled
Necessary cookies are absolutely essential for the website to function properly. This category only includes cookies that ensures basic functionalities and security features of the website. These cookies do not store any personal information.
Non-necessary
Any cookies that may not be particularly necessary for the website to function and is used specifically to collect user personal data via analytics, ads, other embedded contents are termed as non-necessary cookies. It is mandatory to procure user consent prior to running these cookies on your website.
SAVE & ACCEPT