Managing Django Static Files on Heroku

Deploying and running Django apps on Heroku is really a no-brainer, except for one thing — serving static files via collectstatic.

I run collectstatic as usual, using heroku run command just like how I did it for syncdb. It worked but I’m getting 404 error when serving static files.

It turns out that running collectstatic via heroku run spins up a new dyno and collectstatic is running in an isolated environment. So, all the collected static files are deployed to that location and only for this new dyno and the dyno attached to our Django app can’t access that. — Heroku support staff

Solution

The dead simple solution would be to run collectstatic as part of the Procfile before starting the app. We need to “chain” together our Procfile commands for the web process, like so:

1
web: python my_django_app/manage.py collectstatic --noinput; bin/gunicorn_django --workers=4 --bind=0.0.0.0:$PORT my_django_app/settings.py
view raw Procfile This Gist brought to you by GitHub.

OK there you go, no more 404 error when serving static files from your Django app on Heroku. Plus, every time you deploy your app, newly added static files will be collected automatically.

Update

There are a lot of questions about the configurations. Cross check with my settings.py here http://pastebin.com/H43gRKsJ

Important thing here is your STATICFILES_DIRS. Make sure to include your project_name/app_name/static here. In my case, I have project_name/staticfiles for the STATIC_ROOT. Change STATIC_URL = ‘/static/’ if you want to serve from Heroku, same goes to ADMIN_MEDIA_PREFIX = ‘/static/admin/’

Finally add this to your urls.py in order to serve from Heroku.

urlpatterns += patterns(”,
(r’^static/(?P.*)$’, ‘django.views.static.serve’, {‘document_root’: settings.STATIC_ROOT}),
)

Files could be access as such:

/app/project_name/staticfiles/style.css > http://flaming-fire-999.herokuapp.com/static/style.css

/app/lib/python2.7/site-packages/django/contrib/admin/media/css/login.css > http://flaming-fire-999.herokuapp.com/static/admin/css/login.css
Please note that this is not the best way to serve static files but it works. One last thing, you still need to go through all these things if you are using django storages and django compressor to serve static files via Amazon S3 because django compressor needs a temp file system cache on Heroku.

Django “Global” Template Variable

Depending on the project you are working on, you might want to access one or more template variables across all your templates like the User object. If you are making news or blog project, one example would be navigation bar item, say a category list.

You might be tempted to pass the category list from your views or create a template tag for that; but that just violate the Django’s DRY principle. So the best way would be through custom context processor. Writing your own is actually very easy.

1. Writing the context processor function.

You can put this function anywhere in your project. My preference is to place this code inside a context_processors.py file under the related app.

1 2 3 4 5 6 7
# The context processor function
def categories(request):
all_categories = Category.objects.all()
 
return {
'categories': all_categories,
}

Note: Each context processor must return a dictionary and takes only one arguement, HttpRequest object.

2. Point it to your TEMPLATE_CONTEXT_PROCESSORS in your settings.py file.

1 2 3 4 5 6 7 8 9
TEMPLATE_CONTEXT_PROCESSORS = (
"django.contrib.auth.context_processors.auth",
"django.core.context_processors.debug",
"django.core.context_processors.i18n",
"django.core.context_processors.media",
"django.core.context_processors.request",
# ...
"yourapp.context_processors.categories"
)
view raw settings.py This Gist brought to you by GitHub.

Now categories is available for use in all your templates.

Done!

Flvrd and What I’ve Learned

Flvrd, a project I’ve been working with @koekoecrunch for the last four months has finally launched on May 18, 2011, about a week ago. You can read the launch announcement here. Have been fixing bugs and pushing updates for the past week, so a little delay here. Anyway…

Some History

Before Flvrd, I’ve been poking around with two projects — a casual Facebook game (codename Traveloot) and a location-based looting/scavenging mobile game (codename Lootpop). I abandoned Traveloot because at that time, the Facebook apps platform is very buggy. So I pivoted the concept (of looting “virtual” items) to Lootpop. While working on Lootpop I learned a decent amount of Objective-C and iOS programming but what makes me abandon this project is because it’s a little too much to take for a person who is learning new stuff. My takeaway here is, if you are learning new stuff (programming languages), work on something that is simple and interest you, period.

Back to Flvrd

So, I had this idea… jump straight in and start coding (Why? See my bottom line). The idea of Flvrd has been “refreshed” and simplified several times during the development period. The original concept was about expressing comments without the need of words. You use “flavors” to express your thoughts (what I try to solve is to reduce comments that are rubbish and spam). So, long story short, Flvrd is now what it is — flavoring pictures or videos with flavors using flavicons. Simple yet fun.

What I’ve Learned

After four months of hacking and hustling…

  • Learned Django (and Python) and some Javascript + JQuery.
  • Level up my CSS.
  • Learned and relearn sysadmin stuff (setting up servers, config, etc)
  • Launched a real project/product to the World.
  • Starting a web startup is NOT easy BUT cheap (and I’ve nothing to lose, just my time)

Bottom Line…

So entrepreneur wannabe, if you have an idea, just do it and start hacking! Stop worrying whether your is good or bad, whether there is a market for it or whether the world will use it or not. You can spend time all you want finding the perfect market, writing a 100+ pages of business plan or validating your idea so it could be the next Google or Facebook. BUT, the only thing that matter is your product or prototype. Why? Because you are nobody and just a wannabe. People don’t give a damn about your idea or your sugar coated, well crafted business plan unless you are somebody. Really… seriously. So, don’t waste time worrying, spend time working on it. Need more motivation? Read this.

BTW, you… get flvrd now.