[Fixed]-Add confirmation step to custom Django management/manage.py command

20👍

You can use Python’s raw_input/input function. Here’s an example method from Django’s source code:

from django.utils.six.moves import input

def boolean_input(question, default=None):
    result = input("%s " % question)
    if not result and default is not None:
        return default
    while len(result) < 1 or result[0].lower() not in "yn":
        result = input("Please answer yes or no: ")
    return result[0].lower() == "y"

Be sure to use the import from django.utils.six.moves if your code should be compatible with Python 2 and 3, or use raw_input() if you’re on Python 2. input() on Python 2 will evaluate the input rather than converting it to a string.

👤knbk

Leave a comment