VSCode for Django Development in 2024
Setting up your editor properly will make your development experience better and save you time. In this article, let me show you my Visual Studio Code settings for working with Django.
If you're new to Python or to VSCode, check out our blog post How to set up Visual Studio Code for Python development for the initial editor set-up when working with Python.
Setting up VSCode for Django
There are just a few extensions you need to work with Django effectively in VSCode:
- Django
- Djlint
- Tailwind CSS IntelliSense (optional, but recommended!)
More info on what each one does below!
The Django extension
This extension helps us write Django templates using HTML and the Django templating language. It gives us:
- Syntax highlighting in the templates, so we keep HTML syntax and also the Django templating language.
CMD+Click
(orCTRL+Click
on Windows) to "Go to definition" in templates. For example, in your{% extends 'base.html' %}
, you canCMD+Click
'base.html'
to go to that file. Neat!- Support for snippets, but I don't tend to use these.
Linting HTML with Djlint
Djlint is a library for linting HTML. Just as we use Ruff for linting our Python code, Djlint can tell us of any issues in our HTML code.
To use Djlint, install the extension and also install the djlint
Python package in your virtual environment:
source .venv/bin/activate
pip install -U djlint
Not using a virtual environment for your project? I strongly recommend you do, and I've written a complete guide to virtual environments for your enjoyment. It's our most popular free tutorial by a mile!
Optionally, formatting HTML with Djlint
You can use Djlint to format your HTML code as well as linting. This just means adding newlines to break down long lines, and indenting HTML attributes evenly.
I really like formatting my HTML, because doing it by hand takes a while.
But I tend to use Django with AlpineJS, and Djlint formatting doesn't work with AlpineJS well because the HTML attributes AlpineJS uses can get quite long.
If you don't use super long HTML attributes, I recommend you use Djlint for formatting! You can enable it in your settings.json file like so:
"[django-html]": {
"editor.defaultFormatter": "monosans.djlint",
"editor.detectIndentation": true,
"editor.formatOnSave": true,
"editor.tabSize": 2,
"djlint.profile": "django"
}
This will automatically format on save. Feel free to disable it if you'd rather format manually.
To run formatting manually you'd open the command palette with CMD+Shift+P
(or CTRL+Shift+P
on Windows) and then "Format Document".
If you enable format on save and you want to save without formatting, you can use the shortcut CMD+k s
(or CTRL+k s
on Windows). That shortcut is a two-part shortcut, so you'd press CMD+k
first, and then s
.
As I said earlier, I don't use Djlint for formatting because it doesn't work well with AlpineJS, so I keep format on save off.
Tailwind CSS IntelliSense
If you use TailwindCSS (I do, and recommend it), install the Tailwind CSS IntelliSense extension. It will make it so in HTML files, when you start typing a class name, you get autocomplete and documentation as to what the class does.
Note: it only works if you have a tailwind.config.js
in the root of your project (the folder you opened with VSCode). I've sometimes created an empty file there just so the extension works, if I have my config file somewhere else.
Conclusion
That's about it! We've installed a number of VSCode to make our life easier:
- Django
- Djlint
- Tailwind CSS IntelliSense
And some from our VSCode for Python blog post:
- Python (and Pylance)
- Ruff
- indent-rainbow
- Rainbow Brackets
- vscode-icons
- LiveShare
If you've enjoyed this blog post, consider subscribing below! We're working on a completely new Django mega-course that will be released in a couple months.
Subscribing will let me notify you when the course is available, and also you'll get new blog posts in your inbox (once a week or so).
See you in the next one!