Where do I host my Django 5 projects for free?
Vercel surprisingly offers free Django 5 hosting for low-traffic sites. Read on to learn how.
As a software developer, you want your creations visible to everyone. Be it as a portfolio to market your skills or to create a project for volunteer work, the ability to host your Django project for the least cost is every developer's objective.
Luckily, Vercel allows us to deploy Python projects on a pay-as-you-go basis via their serverless infrastructure. This loosely translates as "free" for low-traffic sites (more on this nuance later).
The fastest and easiest way to host a Django app on Vercel is to deploy a working template, and then modify the template to suit your needs.
1. Fork ardeearam/django-on-vercel from Github
Vercel requires that the code resides in your own Github account, or in an organization account that you are a member in.
2. Deploy your repository into Vercel
In the "Overview" tab, click "Add New..." > "Project.
Import django-on-vercel.
3. See and test
If the import is successful, it will take you to the Production Deployment dashboard.
You can click the link under Domains to confirm that our Python template is running well on Vercel. If all is well, you will be able to see the Django rocketship splash page.
If successful, your deployment should look like this: https://django-on-vercel-beh8.vercel.app/
Vercel Production Deployment Dashboard
Glorious Rocketship
4. (Optional) Configure the project to use production-grade database like ๐ Vercel Postgres
This template uses sqlite as proof-of-concept to make things easy to deploy and get started. However, I'm guessing your project is non-trivial and would be needing more features than sqlite can handle.
You can reconfigure the database settings by declaring several environment variables.
Go to the Settings tab. Look for Environment Variables in the left sidebar. You can see the form where you can input your environment variables. Be sure to provide the values for the following variables:
- DATABASE_ENGINE (e.g. if you are using PostgreSQL, this must have a value of django.db.backends.postgresql)
- DATABASE_NAME
- DATABASE_USER
- DATABASE_PASSWORD
- DATABASE_HOST
- DATABASE_PORT
- SECRET_KEY (Not actually part of database settings, but since we are here, we might as well add a difficult-to-guess value here to up our security)
Vercel Environment Variables
5. (Optional) For existing projects, copy over your apps.
Once we have the template up and running with no errors, we can now copy over the apps from your existing projects to the running one that we have right now. Also, let's not forget any custom changes in settings.py and urls.py needed by your business logic.
Why use ardeearam/django-on-vercel over django-hello-world Vercel template?
The ardeearam/django-on-vercel repository is actually in itself a fork of https://vercel.com/templates/python/django-hello-world, with a few added goodness from my end:
- I upgraded the Django version from 4 to 5.1.
- I added django-environ to allow credentials to be placed in environment varialbes and to prevent hard-coding of database credentials in settings.py.
- I added whitenoise so that static files (especially in the Django Admin) will load properly.
Before: Django Admin without whitenoise
After: Django Admin with whitenoise
- I added the default rocketship view because the default vercel template is lame, and we can do better.
Vercel, seriously? This robs me of all the joy of software development.
Hold on, is it really free?
For low-traffic sites, yes. Let me expound on that.
Vercel's hosting infrastructure is serverless. This means that we are not billed on a monthly basis but mostly on these two things:
- Function Invocation - the number of invocations of our Python serverless functions, and
- Edge Network Data Transfer - the data consumed when delivering static content (CSS, images.
I will assume that you are on the Hobby plan (as we want to really push our costs down). These are the rates in this plan according to Vercel's pricing page:
100,000 invocations /month included
100 GB /month included
(There are more complexities to it, but these would be 80% of the cost, so I think this would serve well as a back-of-the-napkin estimate)
With the assumption that each page of your site would have 3 function invocations (1 main site + 2 AJAX calls), we have an allowance of 33,333 page visits per month.
Let's ignore the edge network data transfer for now, since I host my static files in AWS S3 anyway. I'm assuming that your project has minimal images as well, and for goodness sake host your videos in Youtube or Vimeo.
If you are expecting less than 33K visitors per month, then you will not be paying anything!
Caveats
Vercel's Python Runtime is still in Beta at the time of this writing. To quote:
For now, Django in Vercel is best for personal sites, portfolio sites, and staging sites.
It's best to consider alternatives for now for high-traffic and mission critical sites with low tolerance for down times.
Happy coding! ๐