/kb

personal knowledgebase

Translation of Django Apps

without comments

I recently created a Django app that needed translation of the Django administration. It took some googling and messing around to make it work, so this quick and dirty walkthrough will hopefully be helpful for others.

Here’s how I did it:

Add translations for all model fields:

class Stuff(models.Model):
    title = models.CharField(_('title'), max_length=255)
    the_other_model = model.ForeignKey('othermodel', verbose_name=_('the other model'), verbose_name_plural=_('the other models'))
 
    class Meta:
      verbose_name = _('stuff')
      verbose_name_plural = _('stuffs')

Then the language (.po) files must be created:

mkdir locale
python manage.py makemessages -l no

Then edit your language files in locale/_LANGUAGE_/LC_MESSAGES/django.po. When you’ve finished editing the language file must be compiled:

python manage.py compilemessages

If you want to translate app names as well, you can add something like this to your project __init__.py:

from django.utils.translation import ugettext_lazy as _
 
_(u'auth')
_(u'configuration')
_(u'sites')
_(u'appname')
_(u'Auth')
_(u'Configuration')
_(u'Sites')
_(u'Appname')

This feels like a dirty hack but I couldn’t find any other way to do it.

Source: Django documentation – Internationalization and localization, and some Google Groups thread that I can’t find right now, for the app name translation.

Written by hgrimelid

March 4th, 2010 at 3:50 pm

Posted in Programming

Install PIL (Python Imaging Library) in Leopard

without comments

Installing modules when using pip, virtualenv and virtualenvwrapper is a breeze, but it took some time before I realized what was the easiest way to install PIL.

I do it this way:

pip -E PATH_TO_VIRTUALENV install http://dist.repoze.org/PIL-1.1.6.tar.gz

This installs a slightly repackaged version of PIL. The latest PIL version is 1.1.7, but as far as I know it’s not available in this form.

Written by hgrimelid

February 7th, 2010 at 1:09 pm

Posted in Programming

Tagged with , , , , ,

Exclude several files or directories from tar

without comments

It’s very useful to be able to exclude certain files or directories when using tar. Here’s how I did in Leopard:

tar czvf FILENAME.tgz --exclude={.svn,wiki*,static} FILES_OR_DIRS_TO_TAR

Written by hgrimelid

August 9th, 2009 at 4:08 pm

Posted in Shell

Tagged with , , ,

Passwordless login with MacFusion

without comments

Put a reference to the private key file in ~/.ssh/config, for example:

IdentityFile ~/.ssh/id_rsa

Source: FAQS.org

Written by hgrimelid

August 6th, 2009 at 9:28 am

Posted in Server

Tagged with , , , ,

Make SSHKeychain work in Leopard

without comments

Here’s how I set up SSHKeychain on my Mac:

  1. Modify the package content as described
  2. Add stuff to .bashrc/.profile
  3. Add keys to SSHKeychain: Preferences > SSH Keys
  4. Check Manage (and modify) global environment variables (this probably has no effect, ref 2.)

Written by hgrimelid

June 22nd, 2009 at 10:43 am

Posted in Shell

Tagged with , , , ,

Removing info bubble from Google Map, and keeping placemark

without comments

Default behavious is to show the information bubble when embedding a Google Map in your web page. Some times this is not a desired behaviour, and you only want to show the placemark. After a little digging this Google Groups thread provided a very simple solution to the problem.

Add the iwloc-parameter with value near. Here’s an example:


View Larger Map


View Larger Map

So the string to insert is

&iwloc=near

somewhere in between. Take a look at my source code if you’re unsure.

Written by hgrimelid

March 26th, 2009 at 11:05 am

Posted in General

Tagged with , , ,

Chapter 7 in “Python Web Development with Django” – some notes

without comments

I am currently in the process of learning Django and have just finished chapter 7 in Python Web Development with Django. Here are some notes on how I got it working (except for the index page which still isn’t completely as expected) on an ubuntu hardy installation.

The photo gallery example assumes an apache2 + mod_python setup. I didn’t really find it straightforward to get this running from the description in the appendix, so here is how I got it to work (after installation of apache2 and mod_python, of course):

In /etc/apache2/sites-available/default, change lines:

        <Directory /var/www/>
                Options Indexes FollowSymLinks MultiViews
                AllowOverride None
                Order allow,deny
                allow from all
        </Directory>

To:

        <Directory /var/www/>
                Options Indexes FollowSymLinks MultiViews
                AllowOverride AuthConfig
                Order allow,deny
                allow from all

                AddHandler mod_python .py
                PythonHandler mod_python.publisher
                PythonDebug On
        </Directory>

Then I did the changes in /etc/apache2/httpd.conf, but slightly different from the book by adding a PythonOption django.root (didn’t work for me with the setup in the book):

<Location "/gallery/">
      SetHandler python-program
      PythonHandler django.core.handlers.modpython
      SetEnv DJANGO_SETTINGS_MODULE gallery.settings
      PythonOption django.root /gallery
      PythonDebug On
      PythonPath "['/path/to/django/root', '/home/user/django-stuff'] + sys.path"
</Location>

With an additional entry for media files:

<Location "/gallery/media">
      SetHandler none
</Location>

Now, I created a directory /var/www/gallery and a /var/www/gallery/media like explained in “Preparing for File Uploads” and edited settings.py:

MEDIA_ROOT = '/var/www/gallery/media/'
MEDIA_URL = 'http://localhost/gallery/media/'
ADMIN_MEDIA_PREFIX = 'http://localhost/gallery/media/admin/'

Where admin is a symbolic link (/var/www/gallery/media/admin) to the admin media directory in the Django installation. The two last entries where not mentioned in the book, but they made it work…

With the settings described above and the PythonOption django.root in httpd.conf, I didn’t need the section about DRY URLs. And it worked – as opposed to the stuff in the book.

With all the configuration stuff in place I typed the code outlined in the chapter and to begin with I couldn’t see any photos, except for the thumb_url in items_list.html (typo: items_listing.html in the last paragraph page 175). I managed to get it working by changing some lines in items_detail.html and photos_detail.html though:

22                 <img src="{{ photo.image.thumb_url }}" />

instead of

22                 <img src="{{ photo.get_image_thumb_url }}" />

and

8 <img src="{{ object.image.url }}" />

instead of:

8 <img src="{{ object.get_image_url }}" />

The alternatives that worked seems more intuitive to me.

I also imported list_detail from django.views.generic in urls.py, but am not sure if this was needed. The photo gallery seems to work fine except for the thumbnails in index.html. I’ve noticed that the code in urls.py for that template is different from the others, using simple.direct_to_template, but I haven’t managed getting that part to work.

Thanks to this thread for getting mod_python up and running: Python and Apache2

Written by Morten Wergeland Hansen

March 8th, 2009 at 8:08 pm

Posted in Programming

Tagged with , , , ,

#1064 – You have an error in your SQL syntax

without comments

When exporting and importing from and to different MySQL databases with diferent version numbers this error message might show up:

#1064 – You have an error in your SQL syntax. Check the manual that corresponds to your MySQL server version for the right syntax to use near ‘ENGINE=MyISAM DEFAULT CHARSET=utf8′

I got the message when exporting from a MySQL 5.x database and importing to a MySQL 4.x database. The solution came to me from the following blog post comment and is very easy. Just add the --compatible option:

mysqldump -u username -ppassword –compatible=mysql40 database_name > FILENAME.sql

Written by Håvard Grimelid

March 4th, 2009 at 11:28 am

Posted in Server

Tagged with ,

Open Finder for current directory from command line

without comments

I just learned how to open a Finder window directly from the command line.

open .

Yes, it’s actually that easy, and the open command is quite handy some times. This is from the open man page.

The open command opens a file (or a directory or URL), just as if you had double-clicked the
file’s icon. If no application name is specified, the default application as determined via
LaunchServices is used to open the specified files.

This means that you can actually open anything from the command line with the open command. Go ahead and try it out!

Written by Håvard Grimelid

January 9th, 2009 at 9:41 am

Posted in Shell

Tagged with , ,

Importing a MySQL dump file

with one comment

mysql -h SERVER -uUSER -p DATABASE < DUMPFILE

Written by Håvard Grimelid

January 6th, 2009 at 7:58 pm

Posted in Server

Tagged with , ,