May 31, 2010


Filed under: django, macports, mamp, mysql, mysqldb, osx — michaelangela @ 7:29 am

For my overly customized MAMP install, it wasn’t pretty. I forget why it’s actually set up this way now but the different parts didn’t like each other and it took quite a while to get it working. Not fun. But fortunately there were those who had gone before me and it wasn’t hard in the end. It just took a long time.



Correction. It just took a really REALLY long time. The reason? You need to have libs and headers and such in places that all make sense for everything to compile. (This is the SHORT version.) So MacPorts to the rescue. Install py25-mysql, copy some files around, use a slightly different “HOST” setting for the database connection and you’re good to go. But installing py25-mysql installs ALL the dependencies needed in order to get that running. Which is good when that’s what you need. Which is what took a long time. But at least it was a one command thing and not hopping all over the ‘net to find and install things as I was very close to doing.

And that bit about the unique “HOST” setting for mine turns out to be

DATABASE_HOST = ‘/Applications/MAMP_1.7.2/tmp/mysql/mysql.sock’


May 13, 2008

Django, Sphinx, and full-text search

Filed under: cool, django, orm, search, tool — michaelangela @ 6:27 pm

This makes some interesting things possible. And it makes sense just looking at it. That’s even better. 🙂

Announcing django-sphinx 2.0.0 Full-text Search | David Cramer’s Blog

One of my tasks lately has been updating the django-sphinx
library to work with Sphinx 0.98, as it includes GIS components which
we have been wanting to possibly utilize on our new search engine. So
today, the conclusion of.. very few hours of work, I’d like to announce
2.0.0 of the Django integration.

The SphinxSearch manager has been completely
rewritten to rely strictly on the Python Sphinx API (a request from the
author of Sphinx), which means it should have much more compatibility
with newer versions in the future. I have also updated the querysets it
returns to work like normal Django QuerySet instances, to where they
clone themselves so you can fork your queryset into several different

May 8, 2008

Django blogging apps…

Filed under: blog, django, tip — michaelangela @ 4:05 am

that are more or less ready to use. The list is at the following link.

Empty Thoughts: Django Blogging Apps

People ask about blogging apps for the Django platform all the time. I
have not investigated each and every one of these, but so far I still
feel pretty confident that not one of these is fully baked (although
some are getting close) in any sort of manner comparable to something
like WordPress or Typo. That said, there’s a lot of great code here, and if you’re not interested in creating your own, use one of the following.

Bash and Django – working together

Filed under: bash, cool, django, shell, shortcuts, tool — michaelangela @ 4:02 am

A note… definitely something to come back to.

Talk Funnel » Blog Archive » Django bash shell shortcuts

SmileyChris had a post up recently on setting up bash aliases for Django. He uses the classic alias command which works for one-line shortcuts. I got inspired to put mine up too, but if you want to do something more elaborate, bash gives you this handy scripting language along with ’shell commands’ so you can do something a bit more involved.

What I tried to do with these shortcuts was to create a set of
mini-commands that quickly do what you need to do when developing
Django. It might help someone out and squeeze an extra 2-3.5
microseconds each time you run a Django command. Hey, it all adds up.

May 7, 2008

Snippet to update Django tables to utf8

Filed under: cool, django, orm, python, snippet, tip, tool — michaelangela @ 11:54 pm

So you get the dreaded illegal mix of collations:

Pythoneer » Convert MySQL table to utf8

Illegal mix of collations (latin1_swedish_ci,IMPLICIT) and (utf8_general_ci,COERCIBLE)

What do you do? Not too bad fortunately. And if you’re working with Django, it’s a little bit easier. Launch the shell with python manage.py shell. Then in the shell do something like this:

from django.db import connection
cursor = connection.cursor()
cursor.execute('SHOW TABLES')
for row in cursor.fetchall(): results.append(row)
for row in results: cursor.execute('ALTER TABLE %s CONVERT TO CHARACTER SET utf8 COLLATE utf8_general_ci;' % (row[0]))

I’m sure this can be optimized a bit but it got the job done simply enough.

Full-text search for Django

Filed under: api, django, orm, tip — michaelangela @ 11:34 pm

Just making a note…

Extending Django’s database API to include full-text search (Mercurytide)

April 25, 2008

A Django HttpRequest QueryDict is not your normal Python dict – i.e. getting ids from a friend request-form

Filed under: dev, django, facebook, python, snippet, tip — michaelangela @ 5:34 pm

Doesn’t work:


Does work:


A typical Facebook friend invite sends the ids to be invited to your site as an array in 'ids[]'. Normally, you can pull values out with just request.GET['key']. But since the value of this particular key is an array and not just a string, you can’t (at least I wasn’t able to) just do ids=request.GET['ids[]']. That returns the first value of the array as a string. Not very helpful.

But, this is a QueryDict object, not a standard {} dict. As such, Django provides the getlist(key) method.

Django | Request and response objects | Django Documentation

getlist(key) — Returns the data with the requested key, as a Python
list. Returns an empty list if the key doesn’t exist. It’s guaranteed to
return a list of some sort.

April 24, 2008

Concat Django Querysets

Filed under: dev, django, orm, tip — michaelangela @ 9:46 pm

Here is a good discussion on concatenating Django Querysets. Very helpful! It also introduced me to itertools. This leads to some other talk about itertools. Very interesting!

The jist though is like this:

How do you concatenate two querysets? – Django users | Google Groups

import itertools
q1 = Model1.objects.all()
q2 = Model2.objects.all()
for thing in itertools.chain(q1,q2):

Very handy.

April 22, 2008

Django snippets to check http_referer

Filed under: deep-linking, dev, django, snippet, tip — michaelangela @ 6:31 pm

Django snippets: Referer-checking view decorators

Here are a couple of Django decorators for limiting access to a view based on the request’s HTTP_REFERER. Both raise a Django PermissionDenied exception if the referer test fails (or a referer simply isn’t provided).

The first, referer_matches_hostname, takes a hostname
(and port, if specified) and matches it against the referer’s. If
multiple arguments are supplied a match against any of the hostnames
will be considered valid.

The second, referer_matches_re, takes a regex pattern (like Django’s urlpattern) and tests if it matches the referer. This is obviously more flexible than referer_matches_hostname providing the ability to match not just the hostname, but any part of the referer url.

Finally there’s an simple example decorator, local_referer_only, that limits a view to the current site by using Django’s django.contrib.sites to look up the current hostname.

Working with field level permissions with Django

Filed under: dev, django, snippet — michaelangela @ 5:46 pm

This might be useful but, buyer beware.

Django snippets: FieldLevelPermissionsAdmin

Have you ever needed to customize permissions, for example, allow only some fields for editing by some group of users, display some fields as read-only, and some to hide completely? FieldLevelPermissionsAdmin class does this for newforms-admin branch. Not tested well yet (>100 LOC!).

April 18, 2008

Neat little Facebook snippet for Django

Filed under: dev, django, facebook, python, snippet, tip — michaelangela @ 7:51 pm

You can use this to set up a Facebook User within you Django app. This of course uses the middleware which is optional as described here.

Django snippets: Little middleware that create a facebook user

  1. Install **PyFacebook** package.
  2. After make all steps in tutorial above, put this code in your app’s
    models.py module (you maybe prefer split it and put the middleware
    class in some other file).
  3. Put the FacebookUserMiddleware python-path in the
    MIDDLEWARE_CLASSES in your settings.py (after

April 17, 2008

Django, generic views, and setting the media url via a template context processor

Filed under: dev, django, tip, tool — michaelangela @ 11:12 pm

So far I haven’t really used Django for “web sites”. It’s really been used as a data backend. Well the time has come for some web stuff. It’s set up to use a dev, staging, and production server. All is well and good. But each has a different media server. In Django, how do you tell it to use different media servers in the template?

A couple points. First, Django templates can extend other templates. So even if you have 1,000 templates (?!) but they all extend one basic template (often named "base.html") then changing the base.html updates them all. Keeps things DRY. Python is big on that and it’s good.

And to keep this process DRY, there is a context processor which can automatically provide data for your templates, i.e. a "media_url" variable. Better still, if you use Django’s generic views, any template context processor defined in your site’s settings will automatically be passed in.

James has a great writeup about this process explaining it. In fact, he even has template util library for just this very thing among others.

The B-List: Django tips: Template context processors

A recent example that came up on the django-users mailing list was a template tag to retrieve the base URL
for your media (typically you want to store “media” like images,
stylesheets and JavaScript in a particular location on your server, or
possibly even have a separate server for them if you’re using Apache
and mod_python — incurring the overhead of mod_python on a request
which will just serve up a file from disk wastes resources). Django
lets you specify where your media files come from via the MEDIA_URL setting.


As it turns out, Django provides an extremely easy way to do this.
Every time you render a template, you give it a “context”; this is a
dictionary-like object whose keys are variable names and whose values
are the values of the variables. When you render a template with a
given context, every key in the context dictionary becomes a variable
in the template that you can access and use.

Reduce Django facebook server load

Filed under: dev, django, facebook, python, tip — michaelangela @ 2:09 am

So say you have a Facebook app and it runs on Django. Standard practice is to load the middleware. Middleware by default adds a bit of overhead. It’s needed for some bits but completely unnecessary for anything that doesn’t need it. This is one way to take it out if you feel the need for the extra speed. I wonder how much performance suffers for the bits that don’t need the middleware?

Using PyFacebook without the Facebook middleware

Recently I have been experimenting with using PyFacebook to create a Facebook app. In the early stages of development I ran into a bit of a quandry: how to you use PyFacebook with Django, without using the PyFacebook middleware?

This is not an inrrelevant question, because the PyFacebook
middleware is just bloat for any views being loaded that do not
directly communicate with Facebook, and is even inappropriate for some
views that do communicate directly with Facebook (requests made with
the FBJSM Ajax object).

March 31, 2008

PyAMF flying high

Filed under: django, education, flex, pyamf, python, web2py — michaelangela @ 7:08 pm

More python/Flex goodness in the works in the future perhaps? 🙂 I really like Python… learning all the time with my web2py friends. I’d love to be able to combine this with Flex. Looks like it’s getting easier and easier. Now to combine this with Orbited

Ted On Flex: The AMF Revolution

The growth of AMF since the release of Blaze DS and the AMF spec
has been nothing short of amazing. I continue to see lots of great new
server software projects leveraging AMF data formats. This morning I
was reading the FlexCoders mailing list and an announcement of a new AMF library “PyAMF – AMF for Python” appeared leveraging Python targeting Twisted (I love twisted), Django, Pylons, TurboGears and CherryPy. Seeing a single project add this much functionality into Python web development is really great for the Flash/Flex developer community.

PyAMF Project
PyAMF Examples

March 28, 2008

Export Django database to xml

Filed under: dev, django, education, web2py — michaelangela @ 4:18 am

Looking at this helps me understand more of what is going on internally with Django‘s models. Using web2py is a bit more intuitive for getting some of this info so it’s probably not too hard to port the concept over… for a future exercise.

/home/siddhi: Export django database to an xml file

Export django database to an xml file
Here is a simple python script to export your django database to an XML file. I haven’t tested it out very thoroughly. It seems to work for the fields that I have in my model – CharField, TextField, DateField, IntegerField, PositiveIntegerField and ForeignKey. If you find any bugs, add a comment to this post.

Create a free website or blog at WordPress.com.