Thursday, January 3, 2013

Tutorial: Deploy Django app on AppFog


AppFog is PaaS provider which make management of code a little easier. There are bunch of PasS providers out there Heroku, Elastic Beanstalk to name a few. I tried Googling easy to use tutorial for deploying Django app on AppFog couldn't find any so following article is compilication of what has worked for me based on trial and errors.

Before delving into details of the application, there are few pre-requisites we assume

1. You must have AppFog account.
2. You must have virtualenv installed on development machine. Pointer that might help you.
3. You mush have af gem installed locally. Instructions to install.

Optionally you can also you Git hub repo, for version controlling your code. You can find sample code for this tutorial on GitHub.

Steps for Deploying Django app on AppFog

1. Create a New App

2. Select Python Django framework

3. Choose infrastructure Region

4. Wait till the App is deployed

5. Download Source Code

   We need to follow this step because AppFog gives us Jumpstart code with all pre-built configuration incluing but not limited to DB settings.

6. Create Virtual Env and install dependencies

  • virtualenv ~/Installs/env/django-appfog
  • source ~/Installs/env/django-appfog/bin/activate

   This is optional so that we can test our work locally.

7. Update settings and created static files

  • Create directory called static at same level as manage.py file. Run 
  • cd static/
  • python ../manage.py collectstatic
  • Open up setting.py file and import settings
  • Add and change ROOT_PATH = os.path.dirname(__file__) and STATIC_ROOT = os.path.join(ROOT_PATH, 'static')
  • Add following line to urls.py url(r'^static/(?P.*)$', 'django.views.static.serve', {'document_root': settings.STATIC_ROOT}),

   By default Django doesn't serve static in production, so we need to create separate static folder and put all necessary files. In our case we're going to be checking admin page to see if our static is getting served.

8. Update the Application

  • af update django-appfog
Note: django-appfog is our application name for this tutorial purpose. If you've created a different application name, be sure to use right value. 


The other thing I couldn't figure out was to admin username and password for newly created apps. But couple of posts suggest doing it programmatic or creating an entry in DB, which to be honest for a PaaS solution seems little lame.

Found better solution? Do let us know in comments

3 comments:

  1. one way I have tested is by changing manage.py to load admin user's json data which was dumped on local. after af update the code, roll back to original django manage.py and update again.

    I think south migrate command could also done like this.

    ReplyDelete
  2. anyway, maybe using an app to write database table will be more flexible.

    ReplyDelete
  3. Great tutorial.
    Oddly enough I get the below error:

    "^static/(?P.*)$" is not a valid regular expression: unknown specifier: ?P.

    ReplyDelete