Skip to main content

TemplateCommand

Copy either a Django application layout template or a Django project layout template into the specified directory.

Attributes

AttributeTypeDescription
requires_system_checkslist = []List of system checks that must be performed before the command is executed.
url_schemeslist = ["http", "https", "ftp"]The supported URL schemes for downloading remote templates.
rewrite_template_suffixestuple = ((".py-tpl", ".py"),)Rewrite the following suffixes when determining the target filename.

Constructor

Signature

def TemplateCommand()

Methods


add_arguments()

@classmethod
def add_arguments(
parser: [CommandParser](../base/commandparser.md?sid=django_core_management_base_commandparser)
)

Configures the command line argument parser with options for project/app names, target directories, templates, and file filtering.

Parameters

NameTypeDescription
parser[CommandParser](../base/commandparser.md?sid=django_core_management_base_commandparser)The argument parser instance to which the command-specific arguments will be added.

handle()

@classmethod
def handle(
app_or_project: str,
name: str,
target: str = null,
options: dict
)

Executes the template copying logic, including directory creation, context rendering for Python files, and permission management.

Parameters

NameTypeDescription
app_or_projectstrA string indicating whether to create an 'app' or a 'project'.
namestrThe name of the new application or project to be created.
targetstr = nullThe optional destination directory path where the template should be unpacked.
optionsdictAdditional command line arguments including verbosity, extensions, and file exclusions.

handle_template()

@classmethod
def handle_template(
template: str,
subdir: str
) - > str

Determine where the app or project templates are. Use django.path[0] as the default because the Django install directory isn't known.

Parameters

NameTypeDescription
templatestrThe path, URL, or file URI provided by the user to load the template from.
subdirstrThe default subdirectory name within the Django core to look for if no template is specified.

Returns

TypeDescription
strThe absolute local file path to the directory containing the template files.

validate_name()

@classmethod
def validate_name(
name: str,
name_or_dir: str = name
)

Validates that the provided name is a valid Python identifier and does not conflict with existing modules.

Parameters

NameTypeDescription
namestrThe identifier string to validate for the new app or project.
name_or_dirstr = nameA label used in error messages to specify if the validation failed for a name or a directory.

download()

@classmethod
def download(
url: str
) - > str

Download the given URL and return the file name.

Parameters

NameTypeDescription
urlstrThe remote URL from which to fetch the template archive.

Returns

TypeDescription
strThe local file path to the downloaded template archive.

splitext()

@classmethod
def splitext(
the_path: str
) - > tuple

Like os.path.splitext, but takes off .tar, too

Parameters

NameTypeDescription
the_pathstrThe file path or filename to be split into base and extension.

Returns

TypeDescription
tupleA tuple containing the base filename and the full extension (e.g., '.tar.gz').

extract()

@classmethod
def extract(
filename: str
) - > str

Extract the given file to a temporary directory and return the path of the directory with the extracted content.

Parameters

NameTypeDescription
filenamestrThe path to the archive file that needs to be extracted.

Returns

TypeDescription
strThe path to the temporary directory containing the extracted files.

is_url()

@classmethod
def is_url(
template: str
) - > bool

Return True if the name looks like a URL.

Parameters

NameTypeDescription
templatestrThe string to check for a URL scheme.

Returns

TypeDescription
boolTrue if the string starts with a supported URL scheme, False otherwise.

apply_umask()

@classmethod
def apply_umask(
old_path: str,
new_path: str
)

Applies the current system umask to the new file based on the permissions of the source file.

Parameters

NameTypeDescription
old_pathstrThe path to the original template file used to read source permissions.
new_pathstrThe path to the newly created file where permissions will be applied.

make_writeable()

@classmethod
def make_writeable(
filename: str
)

Make sure that the file is writeable. Useful if our source is read-only.

Parameters

NameTypeDescription
filenamestrThe path of the file to modify to ensure user write permissions.