How to run the Django Shell in Emacs

How to run the Django Shell in Emacs

Wednesday - January 16, 2019

Demo

Code

(defun django-shell ()
  (interactive)
  (let ((python-shell-interpreter (read-file-name "Locate manage.py "))
        (python-shell-interpreter-args "shell"))
    (run-python (python-shell-calculate-command) nil t)))

Commentary

I was looking for an easy way to access the Django shell in Emacs. Previously, I used ansi-term and just ran ./manage.py shell like normal. The big problem with this method was that I didn’t get all of the awesome code-completion and similar features that I would get from the run-python shell.

This elisp function prompts the user to locate their manage.py file. It then temporarily sets the “python shell interpreter” to manage.py and adds “shell” to the argument of manage.py. run-python is called. It uses python-shell-calculate-command that basically pieces together the interpreter and args variable we set earlier.

Notes:

Leave a comment