PWA and Django #4: Installing a PWA as a native application

posted Originally published at andresalvareziglesias.substack.com 2 min read

Welcome to the fourth entry on the Progressive Web Application with Django series. In this chapter we will learn how to install our webapp as a native application. Very useful and really easy.

image

Allowing native installation

With a small change to the source code, we can ask the user if they want to install our supercool webapp as a "native" app.

let beforeInstallPromptEvent = null;
let installed = false;

async function installPWA() {
   if (beforeInstallPromptEvent === null || installed) {
       return;
   }

   try {
       beforeInstallPromptEvent.prompt();

       const { outcome } = await beforeInstallPromptEvent.userChoice;
       if (outcome === 'accepted') {
           console.log("App install dialog accepted!");
           beforeInstallPromptEvent = null;
           installed = true;
       }

   } catch(e) {
       console.error(e);
   }
}

We can also listen to a couple of events to customize the responses and behavior of our webapp in every step of the process:

window.addEventListener('beforeinstallprompt', (e) => {
   beforeInstallPromptEvent = e;
});

window.addEventListener('appinstalled', () => {
   installed = true;
});

The user needs to fire the event, so an install button is a good place to call the new code:

image

When clicked, the user will see a browser dialog asking for the installation like this:

image

If accepted, the webapp will be registered on the operating system, with its own launcher icon:

image

Executing as a standalone app

Once installed, the web app will launch in its own window, providing a more integrated experience with styling based on the manifest (see previous chapters). Like this:

image

Note that you can customize the icon, the behavior of the webapp, colors, etc. in the PWA manifest. You can read more about this here:

About the list

Among the Python and Docker posts, I will also write about other related topics, like:

  • Software architecture
  • Programming environments
  • Linux operating system
  • Etc.

If you found some interesting technology, programming language or whatever, please, let me know! I'm always open to learning something new!

About the author

I'm Andrés, a full-stack software developer based in Palma, on a personal journey to improve my coding skills. I'm also a self-published fantasy writer with four published novels to my name. Feel free to ask me anything!

If you read this far, tweet to the author to show them you care. Tweet a Thanks
Andres This article does a solid job of walking through how to get your PWA up and running as a native app with Django. The code is clear and to the point, and the whole installation flow is smooth. But hey, quick question—why do you think it’s so important for users to have that “native” feel for a web app? It’s a cool feature, but what’s the real impact on user experience? Overall, great job explaining the process, and I love the energy you bring to the topic! Just a few more insights on the “why” could make it even stronger. Keep it up!
Thank you for your comment and your advices!

More Posts

PWA and Django #3: Online and offline resources in a PWA - Developing Progressive Web Applications with Django

Andres Alvarez - Nov 27, 2024

When to Choose FastAPI Over Django or Flask: A Comprehensive Guide with Practical Examples

Esubalew - Jan 22

Django TemplateDoesNotExist Solved

Tejas Vaij - Jun 1, 2024

Deploying a Django 5 Project to Vercel

Kelvin - Feb 2

How to Build a Dual-LLM Chat Application with Next.js, Python, and WebSocket Streaming

John - Feb 27
chevron_left