Emacs minor mode for Django.
This package is based on the introspection of your Django project. It
executes Python code to get all necessary information about model,
views, and templates inside INSTALLED_APPS
. Works the same way for
applications written by you and for third-party code installed with
pip
.
No more broken code conventions. No more flaky guesses about your project layout. If Django can understand it, Djangonaut can understand it.
Djangonaut mode provides following features
- intelligent navigation in a Django project
- management commands execution
- works transparently with vagrant, docker and remote hosts
2.7, 3.4, 3.5, 3.6
1.8, 1.9, 1.10, 1.11, 2.0
All you need to do is install the package from Melpa
M-x package-install RET djangonaut RET
Djangonaut minor mode available in all buffers related to current django project (python and html files, dired buffers). First of all enable minor mode:
M-x global-djangonaut-mode RET
Now you need to configure Emacs environment to run your django
project. For example, run-python
command should be able to run
django shell. Emacs require to know three things:
- path to the python interpreter or virtual environment
- location of the project
- django settings module of your project
Django itself is a python package you usually install with pip
.
If you install Django into your system globally with apt
or yum
you can skip this section. But most of times python packages are
installed somewhere else like virtual environment on your host, inside
Docker container or virtual machine orchestrated by Vagrant.
If you setup project inside virtual environment, use this command to tell Emacs where it can find an interpreter:
M-x pythonic-activate RET /path/to/your/venv/ RET
If you use Docker or Docker Compose for development, open any of the project files using tramp. Optionally you can specify interpreter location inside container. Also you need to install docker-tramp package to use remote interpreter this way.
C-x C-f /docker:root@container:/app/config/urls.py
;; Optionally...
M-x set-variable RET python-shell-interpreter RET "/usr/local/bin/python"
If you use Vagrant for development, first of all add your ssh key to the trusted list in your VM, so you will not be annoyed with password prompt pops up frequently. Then open any project file in the running virtual machine.
;; SSH config (done once).
ssh-copy-id vagrant@localhost -p 2222
;; Open project file (each time at the begging).
C-x C-f /ssh:vagrant@localhost#2222:/app/config/urls.py
Optionally point Emacs to the remote interpreter this way
M-x set-variable RET python-shell-interpreter RET "/usr/bin/python" RET
The key point here - provided python interpreter should be able to import modules from your project. There is a lot of options here. You can use setuptools to wrap your project into proper python package and install it into editable mode
pip install -e .
You can use pth
file to include project location into interpreter
import path
echo $PWD > venv/lib/python3.7/site-packages/project.pth
Or you can use old plain PYTHONPATH
environment variable to tell
Emacs where to look for your project
M-x set-variable RET python-shell-extra-pythonpaths RET '("/path/to/the/project/")
In case you use Docker you can also set this variable directly inside container either with command line arguments or via environment key of the docker compose file
docker run -e PYTHONPATH=/code/ web django-admin runserver 0.0.0.0:8000
Also Emacs needs to know your django settings module. We can provide it the same way as we do with project path via environment variable
M-x set-variable RET python-shell-process-environment RET '("DJANGO_SETTINGS_MODULE=project.settings")
And in the case of docker you can setup this variable inside container directly or with compose file environment key
docker run -e DJANGO_SETTINGS_MODULE=project.settings web ...
Another way to set the above variables is via directory variables. The values you set will then be valid when editing all files in the directory where .dir-locals.el
is located and all directories underneath that one. To set the same values as in the examples above, we would use the following .dir-locals.el
file:
((nil
(python-shell-process-environment . ("DJANGO_SETTINGS_MODULE=project.settings"))
(python-shell-extra-pythonpaths . ("/path/to/the/project/"))
(python-shell-virtualenv-root . "/path/to/your/venv/")))
Note that you should specify nil
for the major-mode specification in the directory variables, indicating that the values will be valid in all Emacs buffers associated with files under the directory, not just, say, buffers of open Python files.
If you open file or directory related to the project, you should see
Django
minor mode is activated for this buffer. Note, you should
open project files over tramp method, if you use remote interpreter.
For example, open /docker:root@container:/code/manage.py
instead of
manage.py
on the local host.
Key | Command |
---|---|
C-c ' M | djangonaut-find-model-manager |
C-c ' S | djangonaut-find-settings-module |
C-c ' a | djangonaut-find-admin-class |
C-c ' c | djangonaut-find-management-command |
C-c ' h | djangonaut-find-template-filter |
C-c ' g | djangonaut-find-template-tag |
C-c ' i | djangonaut-dired-installed-apps |
C-c ' j | djangonaut-find-static-file |
C-c ' m | djangonaut-find-model |
C-c ' n | djangonaut-find-migration |
C-c ' p | djangonaut-find-drf-permission |
C-c ' q | djangonaut-find-sql-function |
C-c ' r | djangonaut-find-signal-receiver |
C-c ' s | djangonaut-find-drf-serializer |
C-c ' f | djangonaut-find-form |
C-c ' w | djangonaut-find-widget |
C-c ' t | djangonaut-find-template |
C-c ' u | djangonaut-find-url-module |
C-c ' v | djangonaut-find-view |
C-c ' d | djangonaut-find-middleware |
All navigation commands can open definitions in the other window. For example use C-c ' 4 m to open model definition in the other window.
Use C-c ' ! to run management command in the comint buffer.
You can call it with prefix argument C-u
to set command arguments
via interactive menu.