/kb

personal knowledgebase

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

with one comment

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 , ,

Remove Windows line endings in VIM

with 3 comments

The Windows line endings (^M) can be easily removed with most editors. In ViM it is very easy.

To remove all windows line endings (and replace with nothing), type the following:

:%s/^V^M//g

(note that the ^V will not display).

Sometimes it is necessary to add a line break as well:

:%s/^V^M/\r/g

Partly based on a Tech Recipes article.

Written by Håvard Grimelid

November 17th, 2008 at 11:14 am

Posted in Editors

Tagged with , , , ,

Backing up my Macbook with rsync, SSH and Cron

without comments

I’ve created a simple script which syncs my documents folder with a remote computer. The files are transferred across the SSH protocol and the backup is performed daily with assistance of cron.

My rsync script is very simple:

#!/bin/bash
rsync -avzr --delete-excluded --exclude="*.log" --exclude="*.aux" --exclude=".svn" --exclude=".classpath" --exclude=".cache" --exclude=".project" --exclude="*.class" --exclude="*.swp" --exclude=".DS_Store" --exclude=".metadata" -e "ssh -c blowfish" /Users/USERNAME/Documents USER@REMOTE_COMPUTER:REMOTE_DIRECTORY

It was quite tricky to set up the cron job until i realized that cron does know very little of the user’s system. The trick was to add SSH_AUTH_SOCK=/tmp/501/SSHKeychain.socket in front of the script refrence in crontab, so that it looks like:

55 21 * * * SSH_AUTH_SOCK=/tmp/501/SSHKeychain.socket /Users/USERNAME/etc/backup.sh

This will cause the backup script to run at 21:55 each night. For more info on cron, check out unixgeeks.org. Add the following at the end if you’d like to suppress the mail from cron: >& /dev/null.

Note that for this script to run, SSH has to be set up with keypairs authentication.

Written by Håvard Grimelid

September 13th, 2008 at 11:29 pm

Posted in Scripts,Server

Tagged with , , ,

Notes on setting up Arch Linux in VirtualBox with Awesome wm

with 8 comments

For several reasons I wanted to have a GNU/Linux system with root access available. In a bright moment I suddenly realized that the simplest way of achieving this was to have a virtual installation on my Macbook.

I chose VirtualBox from Sun as virtualization environment as this is free software (GPL) and Arch Linux as the distribution because I’ve quite some experience with this distribution and it’s easy to create rather minimalistic installs with it.

This walkthrough does not explain how to install Arch Linux in Virtual Box, as this can be considered trivial. The focus is on setting up Arch Linux with the awesome window manager, correct screen resolution and getting access to shared directories in Mac OS.

After installing Arch Linux the first step is to install Xorg and Awesome together with the hwd tool which is used for configuration of Xorg.

pacman -Syu
pacman -S xorg awesome hwd

Then the VirtualBox Guest Additions must be installed in order to be able to set the correct screen resolution for Xorg. In the VirtualBox menu go to DevicesInstall Guest Additions

Perform the following commands as root/sudo:

mount /media/cdrom
bash /media/cdrom/VBoxLinuxAdditions.run

Add 98vboxadd-xclient to .xinitrc for your user and add modules to rc.conf according to Arch Linux Wiki VirtualBox Guide. Then reboot the Arch virtual machine.

In order to configure Xorg an xorg.conf file must be created:

hwd -xa

Edit xorg.conf and set the video driver to vboxvideo and add the resolution 1280×800 to the 16 color depth and remove the other resolutions. Remove all other color depths subsections.

If you want to start Xorg and Awesome and automatically log in at boot, follow the description in the Arch Wiki – Start X at boot and add exec awesome to .xinitrc.

File sharing is easy to set up. First set up the desired folder for sharing by going to DevicesShared Folders in the VirtualBox menu.

Create the folder where you want to mount the share in your Arch filesystem. This name must not be the same as the share name. Now you can mount the share with the following command (as root or sudo):

mount -t vboxsf NAME_OF_THE_SHARE /home/YOUR_USER/MOUNTPOINT

Written by Håvard Grimelid

August 17th, 2008 at 8:54 pm

Posted in General

Tagged with , , ,

Restart Apache

without comments

I always forget how to stop/start/restart Apache (on Ubuntu), so here it is:

sudo /etc/init.d/apache2 stop
sudo /etc/init.d/apache2 start
sudo /etc/init.d/apache2 restart

Written by Håvard Grimelid

August 12th, 2008 at 12:02 am

Posted in Server

Tagged with ,

Bash shell prompt colors

without comments

Here are the colors available for use in the bash shell prompt:

Black 01;30
Blue 01;34
Brown 01;33
Cyan 01;36
Green 01;32
Red 01;31
Pink 01;35
Yellow 01;33

This is my shell prompt:

PS1='\[\033[01;32m\]\u@\h \[\033[01;35m\]\W \$ \[\033[00m\]'

Which produces output on the form user@computer DIRECTORY $. I use different colors for different computers/users.

Written by Håvard Grimelid

August 8th, 2008 at 12:25 am

Posted in Shell

Tagged with , ,

php5-imagick error in Ubuntu 8.04

without comments

I got the following error message, caused by a bug in Ubuntu.:

PHP Warning:  PHP Startup: Unable to load dynamic library
'/usr/lib/php5/20060613+lfs/imagick.so' -
libWand.so.9: cannot open shared object file:
No such file or directory in Unknown on line 0 finished.

Here’s the solution.

Written by Håvard Grimelid

July 17th, 2008 at 1:42 pm

Posted in Server

Tagged with , , , ,