Deploy Django to Cloudflare Pages
This page is a step by step guide on how to deploy the (static output of) a Django project to Cloudflare pages. All of these guides assume you have followed the installation and integration steps to the point where you have a Django site that can be distilled locally and the following command works correctly to build a static copy of your site into a directory:
$ ./manage.py distill-local [optional /path/to/export/directory]
Python versions on Cloudflare pages
The image Cloudflare pages currently uses to build sites has Python 3.7
available. This is compatible with Django 3.x but not Django 4.x. Until
Cloudflare pages supports Python 3.8 or later you will need to pin Django at
3.x in your project via your package manager such as pip
to get your site
to build when you push commits.
Step 1: build command
Once you have a Django site integrated with django-distill make a note
of the command that can build your site. The site output directory should be
added to your .gitignore
. For example, to build to a directory called
public
in your repo root the full command might be:
$ python ./manage.py distill-local public
Check and make sure this command works locally.
Step 2: put your site on GitHub or GitLab
Cloudflare Pages needs to check out your sites code from somewhere, the easiest option currently is to use GitHub or Gitlab as these have integration with Cloudflare Pages. This can be a private repository.
Step 3: link Cloudflare Pages to your repo
Log into your Cloudflare account and go to the Pages page. Click connect to GitHub or GitLab, whichever you are using to host your site repository. You'll be asked to give access to Cloudflare to access the repository and select the repository or repositories you want Cloudflare to have access to.
Select the repository you've comitted your Django site integrated with django-distill.
Step 4: set up the site build
When you create your Cloudflare Page you'll be asked to give it a name, this
will be used for the [sitename].pages.dev
default URL.
Next, you'll need to enter a build command and the output directory the static content is built into.
For example in the command box you could enter for the build command:
python ./manage.py distill-local public
Then in the build output directory (from the above command):
public
Root directory should work left as /
. Click save and you should be done!
Now, every time you push to your Django site to GitHub or GitLab Cloudflare will automatically trigger a rebuild of the project, build the site and deploy it to their CDN.
Next steps
Cloudflare Pages has a lot more features not covered here, such as integrating dynamic serverless functions written in JavaScript, custom domains, analytics and more. You can find the Cloudflare pages documentation here.
Workflow idea
- Build a blog with Django (maybe with Wagtail) and wrap the URLs to export the pages with django-distill, you can integrate SASS or LESS and other server-side rendering for your site
- Use an SQLite database comitted into the repository or a secure and remotely accessible MySQL or Postgres server
- Use
./manage.py runserver
locally to use the Django admin and add, edit and manage your sites content - Commit and push changes including the database to your repository, any changes you make will automatically be deployed in seconds to Cloudflare pages
- The site will be able to handle near unlimited traffic
- Extend the sites functionality with either Cloudflare functions or by integrating externally hosted services (for example Disqus for commtents)
Example
This website is built via CI/CD directly onto Cloudflare Pages as detailed above! You can view the source code on GitHub.