Django Book Tutorial doesn’t work – CSRF error


If you’ve been working your way through the Django Book tutorial, you may have run into a spot of trouble with the form chapter. Even though I had basically cut and pasted in the chapter code, I kept getting a CSRF Verification Failed error, which was definitely not listed in the book.

We don't like this.

We don’t like this.

CSRF stands for Cross Site Request Forgery, which is a type of attack on your web page. This occurs when a malicious Web site contains a link, a form button or some javascript that is intended to perform some action on your Web site, using the credentials of a logged-in user who visits the malicious site in their browser. CSRF protection in Django is meant to prevent just that, which is why we get an error if we haven’t properly secured our forms. It seems to have been enforced in newer versions of Django, but still not updated in the online book.

The problem can be solved as follows:

  1. Add the middleware 'django.middleware.csrf.CsrfViewMiddleware' to your list of middleware classes, MIDDLEWARE_CLASSES.
  2. In any template that uses a POST form, use the csrf_token tag inside the html.
    <form action="" method="post">
        	{% csrf_token %}
                {{ form.as_table }}
            <input type="submit" value="Submit">
  3.  Instead of using a context in your forms, use a RequestContext in The code looks like this:
from django.template import RequestContext
def contact(request):
    initialData = {'form': form}
    return render_to_response('contact_form.html', initialData,

The RequestContext contains a CSRF token which is necessary to prevent this error. Completing these steps should resolve the problem! 🙂


Checking if Python is 32-bit or 64-bit version

I keep forgetting this one, but it comes in really handy to track versions. It was especially handy 2 days ago, when the Orange installation silently overwrote my 64-bit Python with a 32-bit version. Sigh.

import platform

Keyboard shortcuts for remote desktop

Useful keyboard shortcuts for working with a remote access server:

  • Ctrl-Alt-End – display the Task Manager (Ctrl + Shift + Esc is the local command)
  • Alt + Page Up – Switch between programs (Alt + Tab is the local command)
  • Alt + Home – Brings up the Start menu on the remote computer
  • Ctrl + Alt + (+) Plus/ (-) Minus – Minus takes a snapshot of the active window and plus takes a snapshot of the entire remote desktop window.

OMG! 64-bit versions of best scientific Python libraries!

This page is the answer to all my dreams come true. Well, some of them, anyway.

32 and 64-bit versions of scientific open-source Python libraries, for Python 2.6 and Python 2.7! Saved me from a 64-bit Orange disaster.

Unofficial Windows Binaries for Python Extension Packages

Working with Python behind a proxy server

If you’re working behind a proxy server, you may run into some unexpected issues when configuring a work environment with Python and Eclipse. Here are some quick fixes:

Configuring the proxy server in Eclipse:

  1. Window -> Preferences -> General -> Network Settings.
  2. Set the Active Provider drop-down box to Manual.
  3. Edit the HTTP entry in the Proxy Settings list.
  4. Fill in the proxy server and port number (user name and password if necessary).
  5. And here’s MKYong’s long version, with screenshots!


Configuring the proxy server for other tasks, like easy_install:

Set an environmental variable:

set http_proxy=

(The %20‘s are only needed if you happen to have spaces in your password.)


Configuring PyDev in Eclipse to work with Django

The pre-requisite for working with Django is that it is installed in the Python interpreter you want to use.

Eclipse will add the Django options to the menu automatically. At this point, you can:

  1.  create a new Django project: Ctrl+N > PyDev Django Project
  2.  change an existing project to Django: right-click the project and choose
    PyDev -> Set as Django Project.
  3. (you can also change projects back in the same way).

In order to compile and run the web server, use the standard CTRL-F11.

  1. Don’t forget to add runserver to the Run Configuration
  2. Add –noreload as well so you can see the console output.

You can find a complete tutorial here.

Adding Numpydoc to Sphinx

This post is a continuation to the Sphinx Autodoc Tutorial for Dummies.

Sphinx has great documentation, but sometimes its documentation requirements can be somewhat awkward when looking at the code.

Here is a Sphinx function documentation example, which looks like this:

def public_fn_with_sphinxy_docstring(name, state=None):
    """This function does something.

    :param name: The name to use.
    :type name: str.
    :param state: Current state to be in.
    :type state: bool.
    :returns:  int -- the return code.
    :raises: AttributeError, KeyError


Not that intuitive, IMHO.

Numpy is an excellent Python package for scientific computing, and they also offer their own Sphinx extension for documentation, numpydoc, that 1) looks good in html, and 2) looks clear in code.

Here is a Numpydoc function documentation example, which looks like this:

def foo(var1, var2, long_var_name='hi')
    """This function does something.

    var1 : array_like
        This is a type.
    var2 : int
        This is another var.
    Long_variable_name : {'hi', 'ho'}, optional
        Choices in brackets, default first when optional.

    describe : type

I personally find the numpy version quite a bit more intuitive, and it looks great.


Super simple (easy_install reference).

easy_install numpydoc

And then add

extensions = ['numpydoc']

to your file. If you’re already using autodoc or other extensions, the line will look like this:

extensions = ['sphinx.ext.autodoc', 'numpydoc']

That’s it! Running

make html

should compile your documentation using numpydoc. If you don’t care for it – no problem – just remove the extension from