Django Tutorial: Using CDN

beginner
5 min

Django Tutorial: Using CDN

Welcome to the Django Tutorial where we delve into using Content Delivery Networks (CDN)! 🎯

CDN is a powerful tool that enhances the performance and scalability of your Django applications by delivering content from servers located around the world. Today, we'll guide you through the process of incorporating a CDN in your Django projects.

Why Use a CDN?

πŸ’‘ Pro Tip: CDN speeds up your website by reducing the distance between the user and the server. By serving content from multiple geographic locations, it significantly improves load times and ensures a smoother user experience.

Prerequisites

Before we dive in, make sure you have the following prerequisites in place:

  1. A basic understanding of Django
  2. Python installed and configured
  3. A Django project set up and running

Setting Up a CDN

Step 1: Choose a CDN Provider

πŸ“ Note: Many popular CDN providers include Cloudflare, Amazon CloudFront, and Akamai. For this tutorial, we'll be using Cloudflare.

Step 2: Sign Up and Set Up Your CDN

  1. Create an account with Cloudflare and add your domain.
  2. Configure the DNS settings to point to Cloudflare.

Step 3: Enable CDN for Your Website

  1. In the Cloudflare dashboard, select your domain.
  2. Navigate to the "Caching" settings.
  3. Enable the "Automatic Cache Levels" option.

Step 4: Configure Django for CDN

  1. Install the whitenoise and django-compressor packages:
bash
pip install whitenoise django-compressor
  1. In your settings.py, add the following:
python
from django.conf import global_settings from whitenoise.django import WhiteNoise # ... STATICFILES_STORAGE = 'whitenoise.storage.CompressedManifestStaticFilesStorage' STATIC_URL = 'static/' STATICFILES_DIRS = [ base/static, ]
  1. In the urls.py, include the following:
python
from django.contrib import admin from django.urls import path, re_path, include from django.conf.urls.static import static urlpatterns = [ # ... ] + static(STATIC_URL, document_root=STATICFILES_DIRS[0])

Serving Static Files via CDN

To serve static files like CSS, JavaScript, and images, place them inside the base/static folder in your Django project.

Example: Serving a CSS File

Create a style.css file inside the base/static folder and add some basic styling:

css
body { background-color: #f0f0f0; }

Now, your Django application will serve this CSS file via the CDN.

Testing Your CDN Setup

To ensure your CDN is functioning correctly, you can test it using the following steps:

  1. Access your website.
  2. Right-click and select "Inspect" or "Inspect Element".
  3. Navigate to the "Network" tab.
  4. Refresh your page and observe the requests for static files.
  5. Check the URLs for the served static files. They should now include the Cloudflare URL:
https://cdn.yourwebsite.com/static/your_file.css

Quiz

Quick Quiz
Question 1 of 1

Which CDN package should be installed for this tutorial?

With this, you've successfully set up a CDN for your Django application! πŸš€ Happy coding!