Solutions Log

So I only have to figure things out once.

Create a JavaScript Object With Overwrite-able Settings

1
2
3
4
5
6
7
8
9
var Fancy = {
  settings: {
      something: 'one',
      somethingElse: 'two'
  },
  init: function(options){
      var s = $.extend({}, this.settings, options);
  }
}

Call it like so:

1
Fancy.init({something: 'Luke', somethingElse: 'Wroblewski'});

That’s one way to do it anyway.

Installing MySQL on Snow Leopard Using Homebrew

If you already have a /usr/local folder and it’s not owned by your user:

sudo chown -R `whoami` /usr/local

Install Homebrew:

cd /usr/local
git init
git remote add origin git://github.com/mxcl/homebrew.git
git pull origin master

This is kind of odd–you install Homebrew right into the base of your /usr/local folder. It nicely ignores other folders that already exists there. Just do it.

Install MySQL:

brew install mysql

Yeah, it’s really that easy. This will take a while.

Now warm it up:

mysql_install_db

And make sure it automatically starts again on login:

launchctl load -w /usr/local/Cellar/mysql/5.1.43/com.mysql.mysqld.plist

Using Dropbox to Share Git Repositories

First, create a Git subfolder inside your Dropbox folder. Then you can share the individual projects inside that folder with whomever you want (or just use it for instant offsite backups).

From inside a Git project:

git clone --bare . ~/Dropbox/Git/gitproject.git
git remote add dropbox ~/Dropbox/Git/gitproject.git

When you’re ready to push:

git push dropbox master

Your Collaborator’s View

Your collaborator would work on the project like so (inside the folder where they want their project to live):

git clone ~/Dropbox/Git/gitproject.git

If they want to have a dropbox remote instead of the default origin:

git remote add dropbox ~/Dropbox/Git/gitproject.git

They would then push the same way:

git push dropbox master

To get the other person’s changes, it’s the standard deal:

git pull dropbox master

Source

Create Your Own Local Copy of the Django Documentation

sudo easy_install Sphinx

Inside your local SVN checkout of Django:

cd docs
make html

Now you’ll have a beautiful local copy of the documentation to browse for those rare moments when you’re away from the internet (perhaps you’re in a fort?). Just point your browser to:

file:///path/to/your/django/docs/_build/html/index.html

Source

Getting Django + MySQL Running Again on Snow Leopard

Previously…

  • Install the latest Xcode Tools from your Snow Leopard installation DVD
  • Re symlink things to /Library/Python/2.6/site-packages (Leopard used 2.5)
    • Django
    • Any other thing you had symlink’d in 2.5

MySQL + Python

After all that Sequel Pro is still showing the version of MySQL as 5.1.33, but it seems to be working…

PIL

This was by far the biggest headache. I finally found a solution:

Install like this, but before running sudo python setup.py install, do this:

LDFLAGS="-arch ppc -arch i386 -arch x86_64" CFLAGS="-arch ppc -arch i386 -arch x86_64" python setup.py build

If you already had PIL installed and had the source files you compiled from before, be sure to delete them and start fresh from a new Imaging-1.1.6.tar.gz.

Using the jQuery Autocomplete Plugin With Django

Here’s another look into the development of ComicBinder.

There’s already a good tutorial on how to use an autocomplete plugin with Django, but I wanted to use this much snazzier plugin.

The Process

Load both jquery.autocomplete.min.js and jquery.autocomplete.css in your page.

In your form object, create a CharField to hold your autocomplete. Something like:

1
title = forms.CharField(label='', widget=forms.TextInput(attrs={'placeholder': 'The name of a comic'}))

Create a hidden field to hold the primary key of the item you’re selecting (so you don’t have to depend on searching against a ‘name’ field or something else equally brittle):

1
title_pk = forms.IntegerField(widget=forms.HiddenInput())

Create a view to populate the autocomplete list:

1
2
3
4
5
6
7
8
9
10
11
def title_lookup(request):
    results = []
    if request.method == "GET":
        if request.GET.has_key(u'q'):
            value = request.GET[u'q']
            # Ignore queries shorter than length 3
            if len(value) > 2:
                model_results = Title.objects.filter(name__icontains=value)
                results = [ (x.__unicode__(), x.id) for x in model_results ]
    json = simplejson.dumps(results)
    return HttpResponse(json, mimetype='application/json')

This will, of course, need a URLconf:

1
url(r'^title_lookup/$', view=title_lookup, name='title_lookup'),

And to finish it off, a bit of JavaScript in your template to call the plugin:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
<script>
    $(function() {
        $('#id_title').autocomplete('{ % url title_lookup % }', {
            dataType: 'json',
            width: 500,
            parse: function(data) {
                return $.map(data, function(row) {
                    return { data:row, value:row[1], result:row[0] };
                });
            }
            }).result(
                function(e, data, value) {
                    $("#id_title_pk").val(value);
                }
            );
        }
    );
</script>

Sources

Smart URL Redirects in Django

While building ComicBinder’s URLs, I wanted a way to differentiate a volume of a title other than one, and a printing of an issue other than one. So, for example, a URL to the second printing of the second volume of Amazing Spider-Man #1 would look like:

/marvel/amazing-spider-man_2/1_2/

That’s easy enough with some URLconf wrangling. What I’m talking about today is automatically redirecting a request for _1 in any of those places to the same URL without the _1. While technically correct, I want the lack of underscore + number to mean one, and for there to be only one URL for a resource (someone send me a link to a clever article that talks about this).

django.views.generic.simple.redirect_to to the rescue. Try something like this:

(r'^(?P<publisher>[-\w]+)/(?P<title>[-\w]+)_1/(?P<number>\d+)_1/$', 'django.views.generic.simple.redirect_to', {'url': '/%(publisher)s/%(title)s/%(number)s/'}),

I made a few of these to account for situations where it was the first volume, but nothing specified for printing, and vise versa.

Not too bulky, and since I have that sitting near the normal rule, it shouldn’t put me out too much to update it if I make any changes.

Source

How to Log Something

With The Django Debug Toolbar

Somewhere in your Python code (not a template):

import logging
logging.debug(something_you_want_to_log)

With Firebug

Somewhere in your JavaScript or the Firebug Console:

console.log(something_you_want_to_log);

That was easy.

Sources