The difference between sending data to other forms and your case is that you will first need to get the CSRF token. This can be done by first executing a GET request on the page, and then csrfmiddlewaretokenparsing it using a suitable parser.
, cookie, .
:
import urllib, urllib2, cookielib
from BeautifulSoup import BeautifulSoup
cj = cookielib.CookieJar()
opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cj))
urllib2.install_opener(opener)
url = urllib2.urlopen('http://localhost:8000/accounts/login/')
html = url.read()
doc = BeautifulSoup(html)
csrf_input = doc.find(attrs = dict(name = 'csrfmiddlewaretoken'))
csrf_token = csrf_input['value']
params = urllib.urlencode(dict(username = 'foo', password='top_secret',
csrfmiddlewaretoken = csrf_token))
url = urllib2.urlopen('http://localhost:8000/accounts/login/', params)
print url.read()