Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to run django shell from Emacs?

I'd like to be able to run ./manage.py shell in an Emacs buffer, with all the nice stuff that you get from ipython, like magic commands and autocompletion. Ideally I would also like to be able evaluate code from a buffer to the django shell.

Is this possible?

like image 452
Ryszard Szopa Avatar asked Sep 07 '09 15:09

Ryszard Szopa


1 Answers

After researching this question I think the best solution that will work for multiple django projects without changing your config each time you swap is python-django.el plus correct configuration of directory-local variables.

python-django is a great addition to python.el for django users, with many small quality of life improvements, like running commands etc.

To get it to always launch the django shell when in a project, you need to set the proper directory-local variables, by creating a .dir-locals.el file in the root of your project. You can use this config for the .dir-locals.el file. The critical part is setting your python-shell-interpreter args to manage.py shell for your project.

((python-mode
  (python-shell-interpreter . "python")
  (python-shell-interpreter-args . "/home/youruser/code/yourproject/manage.py shell")
  (python-shell-prompt-regexp . "In \\[[0-9]+\\]: ")
  (python-shell-prompt-output-regexp . "Out\\[[0-9]+\\]: ")
  (python-shell-completion-setup-code . "from IPython.core.completerlib import module_completion")
  (python-shell-completion-module-string-code . "';'.join(module_completion('''%s'''))\n")
  (python-shell-completion-string-code . "';'.join(get_ipython().Completer.all_completions('''%s'''))\n")
  (python-shell-extra-pythonpaths "/home/youruser/code/yourproject/apps/")
  (python-shell-virtualenv-path . "/home/youruser/.virtualenvs/yourproject")))

```

The config is taken from this blogpost by the author of the project.

like image 82
Alex Recarey Avatar answered Sep 22 '22 17:09

Alex Recarey