My Django Emacs

I describe here some modes I have activated to work with emacs :

Simply put these lines in your .emacs. Most of these modes are part of Emacs


When you have several buffers with same file name, like, it is really boring to switch between<3>,<4>. With Uniquify, buffer names could be prefixed with parent directory, like admin/, forms/

(require 'uniquify)
(setq uniquify-buffer-name-style 'forward)

Ido Mode

There is many modes other modes like Ido. I found it very pleasant to load and switch buffers, in conjunction with Uniquify.

(require 'ido)
(ido-mode t)

Emacs 23

Not released yes (perhaps 2009 ?), but I use emacs-snapshot on my debian, to have anti-aliased rendering.

deb lenny main

I have activated bold face for keywords and italics for comments

(set-face-bold-p 'font-lock-keyword-face t)
(set-face-italic-p 'font-lock-comment-face t)


I like Js2Mode, with syntax error highliting.

Downlad it and append these lines in your .emacs

(autoload 'js2-mode "js2" nil t)
(add-to-list 'auto-mode-alist '("\\.js$" . js2-mode))

Editing Templates

See . I personnaly use Django Html Mode, derived from html-mode : auto-indentation, smart closing html and django tags.

(autoload 'django-html-mode "django-html-mode")
(add-to-list 'auto-mode-alist '("\\.[sx]?html?\\'" . django-html-mode))

Some functions

These two functions are very useful if you have to tag texts for gettext translation. Select your text region in python or html and use the shortcuts below.

(defun django-insert-trans (from to &optional buffer)
 (interactive "*r")
     (narrow-to-region from to)
     (goto-char from)
     (iso-iso2sgml from to)
     (insert "{% trans \"")
     (goto-char (point-max))
     (insert "\" %}")
(defun django-insert-transpy (from to &optional buffer)
 (interactive "*r")
     (narrow-to-region from to)
     (goto-char from)
     (iso-iso2sgml from to)
     (insert "_(")
     (goto-char (point-max))
     (insert ")")
(add-hook 'sgml-mode-hook
        (lambda ()
          (local-set-key "\C-c\C-g" 'django-insert-trans)
          (setq indent-tabs-mode nil)
(add-hook 'python-mode-hook
        '(lambda ()
           (outline-minor-mode 1)
            tab-width 4
            python-indent 4
            outline-regexp py-outline-regexp
            outline-level 'py-outline-level)
           (local-set-key "\C-c\C-t" 'django-insert-transpy)


Doug Mai 13, 2009 at 11:13 après-midi

Thanks for posting this. uniquify alone made this worth reading. That's a huge win.

Steve Mai 14, 2009 at 1:20 après-midi

very nice setup. Post more emacs + django goodness

Adam Mai 14, 2009 at 6:17 après-midi

Amazing work, short and to the point.
Do you have your .emacs file on or the like?
Thank you very much.

fredz Mai 14, 2009 at 6:30 après-midi

Many thanks for your kind comments.

My (live) .emacs is at

Bob Mai 18, 2009 at 1:15 après-midi

Sounds good. What about a screenshot? ;)

Absortou Juin 22, 2009 at 5:54 après-midi

How about a django shell within emacs?

I guess one could start from a python shell and load some stuff, right?

fredz Juin 22, 2009 at 6 après-midi

I personnally don't like using terms in emacs, I have bad colors rendering, and some shortcuts are locked.

But if someone have suggestions, I would be be pleased to update this page.

Qrilka Juillet 11, 2009 at 10:41 matin

Thanks for good tips!


blog comments powered by Disqus