Flirting with Django – part 2

Just a quick post. I’ve been on holiday and I’ve been quite busy with various other things like setting myself up as full blown freelancer (dedicated website powered by Django coming soon)(anybody need a website?) and tweaking my new business directory website, so i haven’t had a chance to do too much.

In my previous post I mentioned that the one thing that drove me utterly mad was the issue of serving static files from Django. I thought that I had got it licked. My god I was wrong. Everything was working fine or so I assumed by tying in the Django FileBrowser and Django TinyMCE started to cause me problems. In the end I was left with a situation where I could browse for an image (using FileBrowseField) and select it but when I saved the page on the next page the paths were scrambled somehow and the file browser would not open. (Clearly my own newbie fault but damn hard to sort – and caused by my attempts to sort the static files issue).

The TinyMCE plugin is great, don’t get me wrong installing TinyMCE is not hard but setting up the plugins can be a chore so this is a nice little package – particularly the way it integrates with the File Browser.

The FileBrowser plugin is also great, but may one day if I get good enough at Python and Django I will rewrite it. Personally I like to have all my uploads managed in a database (as anyone who has tried out my extension to the CakePHP MeioUpload behaviour will see). My feeling is that with the FileBrowser things could get messy pretty fast. Be nice to have everything in inline popups / modal dialogues rather than new windows (haven’t tried yet).

I did solve the static file issue but I had to create a brand new clean app to test it out (integrated with TinyMCE + FileBrowser). Key parts of settings.py as follows:

# Absolute path to the directory that holds media.
# Example: "/home/media/media.lawrence.com/"
MEDIA_ROOT = 'E:/xampp/htdocs/django/s2/media/'

# URL that handles the media served from MEDIA_ROOT. Make sure to use a
# trailing slash if there is a path component (optional in other cases).
# Examples: "http://media.lawrence.com", "http://example.com/media/"
MEDIA_URL = '/media/'

# URL prefix for admin media -- CSS, JavaScript and images. Make sure to use a
# trailing slash.
# Examples: "http://foo.com/media/", "/media/".
ADMIN_MEDIA_PREFIX = '/media/admin/'

FILEBROWSER_URL_WWW = '/'

TEMPLATE_DIRS = (
    # Put strings here, like "/home/html/django_templates" or "C:/www/django/templates".
    # Always use forward slashes, even on Windows.
    # Don't forget to use absolute paths, not relative paths.
    'E:/xampp/htdocs/django/s2/templates'
)


INSTALLED_APPS = (
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.sites',
    'django.contrib.admin',
    's2.shop',
    'sorl.thumbnail',
    'filebrowser',
    'tinymce',
)

TINYMCE_DEFAULT_CONFIG = {
    'plugins': "table,spellchecker,paste,searchreplace",
    'theme': "advanced",
    'theme_advanced_toolbar_location' : "top",
    'theme_advanced_toolbar_align' : "left",
    'theme_advanced_statusbar_location' : "bottom",
    'theme_advanced_resizing' : "true"
}
TINYMCE_SPELLCHECKER = True
TINYMCE_COMPRESSOR = True

It’s funny – I really don’t feel that the whole static file thing is explained anywhere clearly enough. Should be fun when I come to deploy it on a real server. The experience so far has really got me thinking about ‘Auto Magic’ – the way frameworks can do so too much. Here I am building something fairly complex and learning both Python and the framework at the same time – at the moment I feel there is too much magic, the framework is so powerful I am completely detached and it doesn’t really help me learn Python particularly (mostly as I am having to write so little). i am constantly amazed at how terse the language is; CakePHP has a lot of magic but I still have to type a fair bit; with Django I don’t feel I’m writing at all – it makes an interesting comparison to Java which really doesn’t seem to be at all concerned with saving poor programmers fingers.

Until next time…