Netlify is one of the best cloud hosting platforms for hosting web apps. Its ease of use and ability to handle projects built in different programming languages is among the many features that allow thousands of developers to build and deploy products confidently.
Whether you are new to web development or are using Netlify for the first time, this article will teach you how to properly configure your Django web app, secure it and install all the dependencies. Finally, you will learn how to deploy your app with full confidence that your web app will run.
Can You Deploy Your Django Site on Netlify?
Yes and no.
Netlify claims that it is possible to host Django app on their platform using a static site generator called Cactus. The truth is, there’s no straightforward way to deploy your Django web app on Netlify.
What’s more, unlike platforms such as Heroku, which make it easier to deploy bigger web apps like Django e-commerce sites, Netlify only seems interested in hosting static sites. Therefore if you are planning on deploying something with a more dynamic range, consider using another friendlier platform like Heroku.
For static sites though, Netlify recommends using a tool called Cactus, which is what we will use in this tutorial. We will deploy a simple static site on Netlify, and you will learn how to go through each step of the deployment process.
Setting Up Cactus
Cactus is a python-based build tool designed to generate static sites. It uses the Django templating engine, which allows you to borrow from Django when setting up your templates. Creating a Cactus app is more or less the same as setting us your Django project.
Your site preparation involves creating a static site with Cactus and properly configuring the files. Luckily, Cactus is a pretty straightforward platform to use.
To set up Cactus:
1. Start By Creating a Birtual Env
mkvirtaulenv <env>
Make sure to replace <env> with the name of your environment.
2. Install Cactus
pip install Cactus
3. Create a folder to host your files
mkdir <dir>
Replace <dir> with the name of your directory
4. Generate a new site
Get into your newly created directory by typing cd <dir> on your CMD.
cactus create <app>
Replace <app> with your preferred app name.
5. Get Into Your App
Cd <app>
6. Serve
Type Cactus server to launch your site in localhost. You’ll know you did everything right if you don’t see any errors on your command prompt.
Once done, Cactus will create a dummy website that you can view on your browser.
Preparing Your Static Site
Once your site is up and running, you need to configure your static and Html files. There are three concepts you need to understand when preparing your site.
Templates & Pages
Unlike Django Templates, Cactus has a slightly different structure. For starters, their Templates folder is used to host the base.html file. Every other HTML file egindex.html and about.html is hosted in the pages folder.
Regardless of the file structure, the principles around templating remain the same as with a Django app. You will still need to extend from the base.html file and use Jinja on your code.
Make sure that your base file is in the right place and that the rest of the Html files for your site are in their right folder. Once done, you are now ready to build and serve your site.
.build Directory
If you look at the project’s structure on your computer, you will see a directory called .build.
This directory appears to have all the Html files with the exception of base.html. It also has other files, such as your sitemap and your static folder.
Unlike in a conventional Django app, you cannot render your files without the .build directory. This is where your local server sources the files to serve. You will learn, in the next few steps, how to actually build a site and serve it on your local host.
Build & Serve
Build and serve work the same way python manage.py runserver works. Only this time, the build command generates your site in the .build directory while the serve command serves your site on your browser.
To run the build, type this command in your terminal.
cactus build
To run your server, type this command in your command prompt
cactus serve
The Cactus serve command renders your site on the 127.0.0.8000 URL just like Django runserver does.
Deployment Process
Now that the site is ready to deploy, we will push it to GitHub first. Before pushing anything to GitHub, securing your site and hiding certain files and folders from the general public is important.
Create a .gitignore folder on the root directory of your project and add the .build directory to it. This prevents your .build folder from being accessed by other people on the internet.
Once done, create a requirements.txt file so that Netlify is aware of all the dependencies required to run your app. You can create a requirements.txt file by typing in this command in your CMD:
pip freeze > requirements.txt
Deploy to GitHub
To deploy your new site to GitHub,
- Go to GitHub and create a new repository.
- Fill out the required information without ticking the .gitignore and README options.
- Create your new repository. GitHub will give you a detailed plan for adding your site.
- Follow the prompts provided and wait for the deployment process to complete.
- Once done, you are now ready to deploy your website to Netlify.
Deploying to Netlify
From this point on, the hard work is done. Netlify allows you to quickly deploy your websites to their servers in a straightforward manner.
To deploy to Netlify:
- Start by signing up for a Netlify account.
- On the Your Sites page, click on Import from Git. You can also Import from Git on the home page.
- Click on GitHub when you see Connect to a Git provider to connect Netlify with your GitHub account.
- Confirm authorization so that Netlify can access your GitHub account.
- Click on Configure your Netlify settings on Github.
- Scroll down to Repository access and decide whether you want Netlify to access all your repositories or whether you want it to access selected repos.
- Pick Only selected repositories for now and select the repository you just created for this project.
- Click save. GitHub will redirect you to your Netlify page, where you will see the selected repository.
- Click on this new repository and scroll down to Basic build settings.
- Make the right configurations by providing the Branch, Directory, and Build command.
- Click on Deploy site and wait for the deployment process to complete.
In case you encounter an error that prevents your site from being deployed, check your build logs for the Python version. If it is not a recent version of python:
- Go to site settings.
- Scroll down to Environment variables and click “Edit variables.”
- Type in PYTHON_VERSION in the key section and 3.8 in the value section. You can use this link to check what python version Netlify supports.
- Click save.
- Deploy your site again.
Different factors will determine how long the build process will take. If everything is okay, this shouldn’t take too long. In any case, give Netlify time to completely build your site until it is ready.
Final Touches
Once your site is deployed, you can decide to make a few personal touches.
For example, notice that Netlify gives you a random Alphanumeric name for your site.
You can change this to your preferred domain name.
- Go to your site settings.
- Click on Change site name under the site settings section.
- Edit your site name and click save.
Your site will now reflect the new name, and you can visit it to see if it works.
Conclusion
While Netlify won’t host your more dynamic Django app, it does a great job of hosting your static site built off of Django. All you need is a proper static site generator and the right configurations to get your site up and running.