About Finding Spelling Mistakes in the User's Input

In a management command you can allow to enter an app name and check if there are no spelling mistakes in the user's input with a code like this:

1
2
3
4
5
6
7
8
9
from django.apps import apps
from difflib import get_close_matches

app_labels = [config.label for config in apps.get_app_configs()]
app_label = input("Enter an app name: ")
if app_label not in app_labels:
    close_matches = get_close_matches(app_label, app_labels, n=2)
    if close_matches:
        print(f"Did you mean: {', '.join(close_matches)}?")

Tips and Tricks Programming User Experience Development Accessibility Django 4.2 Django 3.2 Django 2.2 Python 3