The Django Template LSP server enhances your Django development experience with powerful features for navigating and editing template files. This LSP supports:
- Custom Tags and Filters: Autocomplete for your custom template tags and filters.
- Template: Suggestions for
extendsandincludesstatements. - Load Tag: Autocomplete for
{% load %}tags. - Static Files: Path suggestions for
{% static %}tags. - URLs: Autocomplete for
{% url %}tags.
- Template: Jump directly to the templates used in
extendsandincludes. - URL Tag: Navigate to the views referenced in
{% url %}tags. - Tags and Filters: Quickly access the definitions of custom tags and filters.
- Context Variables: Partial support for jumping to context definitions.
- URLs: Inline documentation for
{% url %}tags. - Tags and Filters: Detailed descriptions for template tags and filters.
- Python: 3.10, 3.11, 3.12, 3.13
- Django: 4.2, 5.0, 5.1, 5.2
To install the package globally and isolate it from other Python environments, use pipx:
pipx install django-template-lspOnce installed, the Django template LSP server is accessible via the following commands:
djlspdjango-template-lsp
env_directories(list[string]) default (list of relative or absolute paths): ["env", ".env", "venv", ".venv"]docker_compose_file(string) default: "docker-compose.yml"docker_compose_service(string) default: "django"django_settings_module(string) default (auto detected when empty): ""cache(boolean/string) default (either true/false or a filepath to the cachefile): false
The Django Template LSP Server collects project data by executing a script in the following order:
-
Virtual Environment:
- Checks for a virtual environment in the root directory within one of these folders:
env,.env,venv, or.venv. - If found, runs the
django-collector.pyscript using the virtual environment's Python interpreter.
- Checks for a virtual environment in the root directory within one of these folders:
-
Docker Compose:
- If a Docker Compose file (
docker-compose.ymlby default) is present and includes the specified service (djangoby default), the script is executed within that Docker service.
- If a Docker Compose file (
-
Global Python:
- If neither a virtual environment nor Docker Compose is detected, the script runs using the global
python3installation on the system.
- If neither a virtual environment nor Docker Compose is detected, the script runs using the global
Note: The data collection process will fail if there are Python syntax errors or missing imports in your project.
Due to the highly dynamic nature of Python and Django, it can be challenging to identify the available context data within templates. To address this, basic type hint support is provided directly in the template files:
{# type blog: blogs.models.Blog #}In your global or project languages.toml add the following
[[language]]
name = "htmldjango"
file-types = ["html"]Project settings .helix/languages.toml:
[language-server.djlsp.config]
django_settings_module="<your.settings.module>"In your lspconfig add the following
require'lspconfig'.djlsp.setup{
cmd = { "<path-to-djlsp>" },
init_options = {
django_settings_module = "<your.settings.module>",
docker_compose_file = "docker-compose.yml",
docker_compose_service = "django"
}
}To use the Django template LSP with VSCode read the following readme
For local development, using Helix is the easiest approach. The configuration for using the source Django template language server, with logging enabled, is already set up.
To start the Helix editor with the environment activated and the correct workspace loaded, run:
make helixLocally install the package
make developPoint neovim's djlsp to the locally installed copy
require("lspconfig").djlsp.setup({
cmd = { "/path/to/django-template-lsp/.venv/bin/djlsp" },
root_dir = require("lspconfig.util").root_pattern("manage.py", ".git"),
})If you want to access the log while developing, add the --enable-log flag to the cmd.
The logfile will be written to a file in your current working directory named djlsp.log.
require("lspconfig").djlsp.setup({
cmd = { "/path/to/django-template-lsp/.venv/bin/djlsp", "--enable-log" },
})