Formation incluse

Turn Your Django Application into a Progressive Web App (PWA)

Fotsing Tchoupe
Fotsing Tchoupe
3 Rechercher 2025 · 4,00 min lecture
1
Django
Turn Your Django Application into a Progressive Web App (PWA)

toTurn Your Django Portfolio into a Progressive Web App (PWA)

You can turn your Django-based portfolio into a Progressive Web App (PWA) so that users can install it on their phones when they open it in a browser. Here’s how to do it:

Steps to Make Your Portfolio Installable on Phones

1️⃣ Add a Web App Manifest

Create a file named manifest.json inside your Django static/ directory.

{
  "name": "AppName",
  "short_name": "AppName",
  "start_url": "/",
  "display": "standalone",
  "background_color": "#ffffff",
  "theme_color": "#000000",
  "icons": [
    {
      "src": "/static/icons/icon-192x192.png",
      "sizes": "192x192",
      "type": "image/png"
    },
    {
      "src": "/static/icons/icon-512x512.png",
      "sizes": "512x512",
      "type": "image/png"
    }
  ]
}

Replace /static/icons/icon-192x192.png and /static/icons/icon-512x512.png with actual icons.

2️⃣ Link the Manifest in Your Base Template

In your base.html (or the main template), add:

<link rel="manifest" href="{% static 'manifest.json' %}">

3️⃣ Register a Service Worker

Create a service-worker.js file inside static/:

self.addEventListener("install", (event) => {
  console.log("Service Worker installed.");
});

self.addEventListener("fetch", (event) => {
  console.log("Fetching:", event.request.url);
});

Then, register it in your base template:

<script>
  if ("serviceWorker" in navigator) {
    navigator.serviceWorker.register("{% static 'service-worker.js' %}")
      .then((reg) => console.log("Service Worker Registered", reg))
      .catch((err) => console.log("Service Worker Failed", err));
  }
</script>

4️⃣ Show the Install Prompt

Add this script in your template to trigger a beautiful install popup at the top:

<script>
  let deferredPrompt;
  
  window.addEventListener("beforeinstallprompt", (event) => {
    event.preventDefault();
    deferredPrompt = event;
    
    let installPrompt = document.createElement("div");
    installPrompt.innerHTML = `
        <div class="install-prompt">
            <span>Install Our App</span>
            <button class="install-btn">Install</button>
        </div>
    `;
    document.body.appendChild(installPrompt);

    let style = document.createElement('style');
    style.innerHTML = `
        .install-prompt {
            position: fixed;
            top: 20px;
            left: 50%;
            transform: translateX(-50%);
            background-color: #000;
            color: #fff;
            padding: 10px 20px;
            border-radius: 5px;
            display: flex;
            justify-content: space-between;
            align-items: center;
            font-family: Arial, sans-serif;
            box-shadow: 0px 4px 8px rgba(0, 0, 0, 0.2);
            z-index: 1000;
        }
        .install-btn {
            background-color: #4CAF50;
            border: none;
            color: white;
            padding: 8px 15px;
            text-align: center;
            font-size: 14px;
            border-radius: 4px;
            cursor: pointer;
            font-weight: bold;
        }
        .install-btn:hover {
            background-color: #45a049;
        }
    `;
    document.head.appendChild(style);

    const installBtn = installPrompt.querySelector(".install-btn");
    installBtn.addEventListener("click", () => {
        deferredPrompt.prompt();
        deferredPrompt.userChoice.then((choiceResult) => {
            if (choiceResult.outcome === "accepted") {
                console.log("User installed the app");
            }
            installPrompt.remove();
        });
    });
  });
</script>

Final Steps

✅ Ensure your site is served over HTTPS.
✅ Test by visiting your site on mobile and checking if the "Install" option appears.

Now, when users open your site on their phones, they'll see a stylish "Install App" popup at the top! 🚀 

1

Applaudissez pour montrer votre soutien

Fotsing Tchoupe

Fotsing Tchoupe

4 Suivez-nous · Rédacteur pour Django

My objective is to apply my expertise in technology and problem-solving to create innovative solutions, develop optimized systems, and continuously … Lire la suite