display the value of a django form field in a template Jul
06
1
2

Sometimes you need to get at the raw value of a field in a django form. This is easily done in 1.3 using the following:

{{ form.field_name.value }}

If you're running a version lower than 1.3 you can get at the value with:

{% if form.instance.field_name %}
{{ form.instance.field_name }}
{% else %}
{{ form.data.field_name }}
{% endif %}

comments

accessing the object inside a django admin change_form.html template Jul
06
1
2

When overriding the change_form.html template sometimes I need access to the object that is being edited. The change_view method provides the original object in the 'original' context variable.

You can access the object in the template using the original variable:

{{ original }}

comments

manually/force eject a stuck cd/dvd on and apple mac Feb
01
1
2

It doesn't happen a lot, but sometimes a CD or DVD gets stuck in my mac and it won't eject using the keyboard or dragging it into the trashcan.

When this happens I just open the terminal and type the following command, which usually overrides whatever is blocking the disc to eject:

drutil tray eject

Although I'm not sure when I'd ever use it there's also a close command.

drutil tray close

comments

including site packages in virtualenv 1.7 Jan
13
1
2

So if you're using virtualenv 1.7 and are wondering why you can no loger see your global Python site-packages directory it's because the default behavior has changed.

Now the default install for virtualenv is to hide site packages; equivalent to passing the old --no-site-packages command:

virtualenv --no-site-packages <env>

Since this option is now default it's no longer needed, and the new command to see the global site packages is --system-site-packages:

vitualenv --system-site-packages <env>

Hopefully this saves you some confusion as it's not clear what's happening when your new install now suddenly is breaking.

comments

random integer with a range in python Dec
14
1
1

To create a random integer within a range you can use the following command:

import random

# random integer between 1 and 100
RANDOM_INTEGER = random.randint(1, 100)

comments

remove domain dns key entry in known_hosts for ssh Nov
22
1
1

Sometimes I re-key or move the domain of a server and need to reset the key so RSA doesn't complain. Technically you can just open ~/.ssh/known_hosts file and remove the entry for the dns, but the names are hashed which makes the entry difficult to find.

I used to just reset the entire known_hosts file, but that's not necessary. You can remove the domain entry with the simple command:

ssh-keygen -R example.com

Next time you try to ssh you'll be prompted to add the key as if you're visiting the site the first time. I hope this helps.

comments

showing a foreign key value in a django admin list display Nov
07
1
1

say you have two models like so:

# models.py

class Book(models.Model):
    title = models.CharField(max_length=100)
    author = models.ForeignKey("Author")

class Author(models.Model):
    name = models.CharField(max_length=100)

In this case it would be handy if we could list the author in the list_display of the book admin, but Django gives an error if we list foreign keys like so:

# admin.py

class BookAdmin(admin.ModelAdmin):
    list_display = ('title', 'author__name')

admin.register(Book, BookAdmin)

Luckily we can easily fix this by adjusting how we link into the author foreign key. We just need to add a method to the Book ModelAdmin and return the desired string.

# admin.py

class BookAdmin(admin.ModelAdmin):
    list_display = ('title', 'author_name')

    def author_name(self, instance):
        return instance.author.name

Be careful though as gratuitous additions of foreign key field can greatly increase the load time of the admin page for the list view of the model: use this judiciously.

comments

ignoring files generated by eclipse for git Oct
18
1
1

If you're developing in Eclipse, like I am for Android, and you use version control you're going to want to ignore the default project files that are generated by Eclipse. You should update your .gitignore with the following list:

.project
.classpath
.settings/
bin/

comments

create/generate qr code with qrencode in ubuntu Oct
14
1
1

You can create qr codes easily in Ubuntu using qrencode. You can install it like so:

sudo apt-get install qrencode

and then create the qr code with the following command:

qrencode -o /path/to/qrcode.png -s 10 'http://example.com'

the `-s` option flag is the size of dot (pixel). The default is 3.

comments

how to rename a database in postgresql Oct
12
1
1

http://www.postgresonline.com/journal/archives/40-How-do-you-rename-a-database.html

First kick the users out of the database, you can find the users with the following command.

SELECT * 
    FROM pg_stat_activity 
    WHERE datname = 'myolddbname_goes_here'

now run the following:

ALTER DATABASE myolddbname_here RENAME TO mynewdbname_here

comments