Skip to main content

Django template LSP

Project description

Django Template LSP server

The Django Template LSP server enhances your Django development experience with powerful features for navigating and editing template files. This LSP supports:

Completions

  • Custom Tags and Filters: Autocomplete for your custom template tags and filters.
  • Template: Suggestions for extends and includes statements.
  • Load Tag: Autocomplete for {% load %} tags.
  • Static Files: Path suggestions for {% static %} tags.
  • URLs: Autocomplete for {% url %} tags.

Go to Definitions

  • Template: Jump directly to the templates used in extends and includes.
  • 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.

Hover Documentation

  • URLs: Inline documentation for {% url %} tags.
  • Tags and Filters: Detailed descriptions for template tags and filters.

Support (tested)

  • Python: 3.10, 3.11, 3.12, 3.13
  • Django: 4.2, 5.0, 5.1, 5.2

Installation

To install the package globally and isolate it from other Python environments, use pipx:

pipx install django-template-lsp

Once installed, the Django template LSP server is accessible via the following commands:

  • djlsp
  • django-template-lsp

Options

  • 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

Data Collection Method

The Django Template LSP Server collects project data by executing a script in the following order:

  1. 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.py script using the virtual environment's Python interpreter.
  2. Docker Compose:

    • If a Docker Compose file (docker-compose.yml by default) is present and includes the specified service (django by default), the script is executed within that Docker service.
  3. Global Python:

    • If neither a virtual environment nor Docker Compose is detected, the script runs using the global python3 installation on the system.

Note: The data collection process will fail if there are Python syntax errors or missing imports in your project.

Type hints

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 #}

Editors

Helix

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>"

Neovim

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"
    }
}

VSCode

To use the Django template LSP with VSCode read the following readme

Development

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 helix

neovim

Locally install the package

make develop

Point 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" },
})

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

django_template_lsp-1.2.2.tar.gz (63.1 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

django_template_lsp-1.2.2-py3-none-any.whl (48.2 kB view details)

Uploaded Python 3

File details

Details for the file django_template_lsp-1.2.2.tar.gz.

File metadata

  • Download URL: django_template_lsp-1.2.2.tar.gz
  • Upload date:
  • Size: 63.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for django_template_lsp-1.2.2.tar.gz
Algorithm Hash digest
SHA256 15dccbb33dc7ef4cb84e1673bf0583d5452bb29b8cb246563b8c5ba4e1415c83
MD5 e7c157fab4578e2c6359147f565573af
BLAKE2b-256 0179a618cc845ea1d30df5f427dcd4ca53b0430d24d18843bc84c778ae67a1de

See more details on using hashes here.

File details

Details for the file django_template_lsp-1.2.2-py3-none-any.whl.

File metadata

File hashes

Hashes for django_template_lsp-1.2.2-py3-none-any.whl
Algorithm Hash digest
SHA256 371c1bff1b3b4c8529089837838f3540a182d493070fef0f222f7463748fa57a
MD5 22952e63378d5b4faf6c1ffe56612882
BLAKE2b-256 4b36871e69a396c5787e35ae31ad5d32fc788dae14c57727093e5eb4fc911aca

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page