Archive for the 'Django' Category

Pinax Developer Quickstart

At PyCon I helped a group of people get a Pinax development environment set up. Many People were not yet familiar with the whole virtualenv and pip workflow, so I thought I would put up the steps I use to get Pinax up.

Technorati Tags:

A simple Django life stream

After reading Ryan Bergs' "The basics of creating a tumblelog with Django" (part1) and (part2), I realized that I wanted a similar tumblog/life stream but I didn't want to have to go through all the work of saving those objects to the database. After all, isn't all that data already stored in their respective systems. And since I am using FriendFeed, which is again duplicating all that data, I don't want to be duplicating it yet another time.

That's when the light bulb went off. Using the FriendFeed API and a bit of caching I can pull in my lifestream from FriendFeed using only a custom tag. So I came up with what I call lifefeed

Just add "lifefeed" to your installed apps and you can now do this from any template

{% load lifefeed %}{% load cache %}
{% cache 900 friendfeed %}
	{% lifefeed "YOUR FRIEND FEED USERNAME" %}
{% endcache %}
You can use the default friendfeed.html template that comes with lifefeed or use your own. You will most definitely want to use your own CSS to style the items since I am no CSS guru.

lifefeed is very simple right now but I would like to add more to it. Like

  • Consolidate duplicate twitter/pownce/brightkite posts
  • Display FriendFeed comments

To see an example of lifefeed in action check the sidebar on this page, you may have to scroll down a bit.

So what do you think?

Technorati Tags:

Intense Debate Import Hack

Recently I wanted to try out the Intense Debate commenting system on this blog. I didn't want to lose all the comments that I had already and Intense Debate only supports importing comments from Blogger, etc but not custom blog software. Since I wrote this blog using the Django framework using django.contrib.comments I needed to come up with my own solution.

It only took one Tamper Data request to see how an anonymous comment was posted into the Intense Debate system. A few minutes later I had a script to import my old comments into Intense Debate.

intenseDebate.py

import intenseDebate
intenseDebate.postComment(blogpostid="", acctid="", anonName="", anonEmail="", \
    anonURL="", comment="")

  • blogpostid is the unique Intense Debate postId. You can find this easily on any Intense Debate powered page using the firebug dom tab. The key to look for is "IDCommentScript".
  • acctid is your Intense Debate accountId. This can also be found using the firebug dom tab. The key to look for is "IDWUserWidget.acctid"
  • anonName is the name of the commenter
  • anonEmail is the commenters email address
  • anonURL is the commenters web address
  • comment is the text of the comment

This method of importing comments does have it's problems.

  1. Manual process to get the blogpostid.
  2. No way of getting the Date/Time into Intense Debate.
But it's better than no comments at all.

Footnote: I don't have a problem with Django comments, especially when paired with django-comment-utils, I just wanted to give Intense Debate a try.

Technorati Tags:

Django in June

This is going to be one busy weekend. Friday after work I am going to Coors Field to watch the Rockies vs. the Devil Rays. I am driving straight from the game to DIA to catch the red eye into Logan where I will pick up my rental car and go directly to the Django in June un-conference.

I am really looking forward to this. It will be great to meet some of the great people in the Django community. I also hope to meet some people in Boston that might be interested in helping me populate boston.bardiver.com with some more great bars. To ensure that this happens I will also be bringing with me some BarDiver.com Schwag to give out. ;)

Once the conference is done with I will be driving up to New Hampshire to play golf with my Dad on Fathers Day. This should be a lot of fun. This will be the 3rd time I have played this year. After that and a quick visit with my Grandmother, I will be driving back into Boston to catch my flight back to Denver. Sunday night I might actually get 8 hours of sleep before I have to be at work again on Monday morning.

Technorati Tags:

BarDiver.com Launches

Back in September I said that I was working on a surprise project. Well today I am happy to announce that BarDiver.com has officially launched. BarDiver.com is a community driven search site to help you find local bars that have the features and atmosphere you like.

For this first release I am keeping BarDiver.com focused on Colorado only. If that is a success and I get interest to open BarDiver.com for another city/state I will absolutely do that. So please, check out the site and if you think your area could use BarDiver.com, let me know.

BarDiver.com is built using the Django web framework. Django was instrumental in getting the site up fast. I cannot get over how great Django is to work with. I was able to use many of the goodies built into Django instead of writing them (again) myself. Some of these things are the authentication, comments, sites, flatpages and of course the admin user interface. I am really looking forward to the many cool new things that are being worked on in the seperate Django branches, such as Multiple Database Support, and Generic Authorization.

I hope you enjoy BarDiver.com

Dive In.

Technorati Tags:

Copper Conferencing Launches

I am proud to release yet another Django powered web site. Copper Conferencing went live last night. (twentystar) and I turned this site out in record time.

We have at least two more sites that are very close to being released and one really BIG surprise coming in the near future.

Technorati Tags:

I am going to The Future of Web Apps

I am going to The Future of Web Apps conference this September and I was wondering if any other Django peeps were going to be there.

I hope to hear some stories and advice from people who have had success in this business. I also hope to pimp my super secret new web site. Built with Django of course.

I will be in San Francisco for the 2 days of the conference and I will be sticking around till Sunday to explore the city and eat a ton of sea food. So if there are any like minded Django fans that are going to be in town, maybe we can meet up for a drink one night.

Technorati Tags:

django.contrib.auth has changed

Error was: cannot import name SESSION_KEY
. A quick tip to anyone who got this error after a recent django/trunk update. You no longer need to
from django.contrib.auth.models import SESSION_KEY
and there are some new django.contrib.auth methods like "login()" and "authenticate()". The Authentication docs have been updated to reflect this, but I found it easier to just read the changes. It looks like there was also a lot of work done on multiple authentication backends.

Hopefully this will help anyone who's site broke after the latest svn update.

Technorati Tags:

Impact Sports site launches

This post is a bit late but I am proud to announce that the Impact Sports Performance website was launched last week.

Impact Sports was a collaboration between (twentystar) and myself. This site completely powered by the Django web framework, unlike the last site we launched, Colorado Ridge, which was backed by Django but used php on the front end.

For this project, Django was a complete joy to use. The form processing in Django alone saved me days of work. We even threw in a bit of AJAX (form submiting on the home page) and Scriptaculous (on the facilities page).

I can't wait to start on our next project. :)

Technorati Tags:

Apology for the community spam

I sincerely apologize for the Django Community spam. I recently converted Socialist Software to be Django powered and there were some problems with Dreamhost. All seems to be fixed now.

Again, please accept my apologies.

Technorati Tags:

Socialist Software now powered by Django

Don't let the look and feel fool you. Socialist Software is now powered by Django. I modified the new Wordpress K2 theme to work as Django templates and what you see is the result. Some notable things:

  1. The comments on this site are powered by a modified django/contrib/comments app. The stock app did not support commenter's url's, so I copied the comments app out of django/contrib and into the local projects app dir to make the necessary modifications.
  2. I wanted to use the django/contrib/redirects app to handle redirects from the old Wordpress site. However since Wordpress uses the query string to pass page, rss feed and tag data Django never caught the 404 necessary to perform the redirect. Instead I had to use mod_rewrite to catch and redirect any links. (This is more of a Wordpress gripe than a Django problem)
  3. I think it took me longer to migrate the data from the Wordpress database into the Django one than to do anything else. And Django even made that super easy because I didn't have to write sql to insert in the the Django database. The Django database api took care of it all.
All in all, I am very pleased to have transistioned from Wordpress to a Django based solution.   Things I would like to add in the future:
  1. xml rpc capability so I can use desktop blogging tools to make posts.
  2. a tag cloud
  3. Upcoming.org integration
  4. Technorati integration

Technorati Tags:

Using Django with a small team

Up until recently I have been working on Django based projects all by myself. In last few weeks I have been working with with a designer who does the templates and graphics while I handle the modeling, python and other admin stuff (svn, mysql).

At first things were a little tricky because of our different environments and our constantly having to change the settings.py and urls.py according to each individual machine. You can imagine that this didn't work well with svn because changing the DATABASE_USER param was not really a change that needed to be commited, yet a change to the INSTALLED_APPS does need to be commited.

Because the urls and settings files are just python files I can create individual files for each machine that imports all the settings from the 'global' file. For example if Tom has his database names 'coolProject' and Harry has his database set as Cool_Project then we create 2 new files:

settingsTom.py

from settings import *
DATABASE_NAME = 'coolProject'
DATABASE_USER = 'Tom'
DATABASE_PASSWORD = 'password'
DATABASE_HOST = ''
DATABASE_PORT = ''
ROOT_URLCONF = 'myproject.urlsTom'
TEMPLATE_DIRS = (
    # Put strings here, like "/home/html/django_templates".
    "/Users/tom/python/myproject/templates",
)

MEDIA_ROOT = '/Users/tom/python/myproject/site_media/'
MEDIA_URL = 'http://127.0.0.1:8000/site_media/'

settingsHarry.py

from settings import *

DATABASE_NAME = 'cool_project'
DATABASE_USER = 'Harry'
DATABASE_PASSWORD = 'password'
DATABASE_HOST = ''
DATABASE_PORT = ''
ROOT_URLCONF = 'myproject.urlsHarry'

TEMPLATE_DIRS = (
    # Put strings here, like "/home/html/django_templates".
    "/Users/harry/workspace/myproject/templates",
)

MEDIA_ROOT = '/Users/harry/python/myproject/site_media/'
MEDIA_URL = 'http://192.168.0.1:8000/site_media/'

Now Tom will use DJANGO_SETTINGS_MODULE=myproject.settingsTom.py and Harry will use DJANGO_SETTINGS_MODULE=myproject.seetingsHarry.py.

Notice that each user has there own ROOT_URLCONF. This allows each user to configure how they use Django to server static files. An example of urlsTom.py would be something like this.

urlsTom.py

from django.conf.urls.defaults import *
from impactsports.urls import *

oldpatterns = urlpatterns
urlpatterns = patterns('',
    (r'^impact_media/(?P.*)$', 'django.views.static.serve', 
    {'document_root': '/home/tom/python/myproject/site_media', 'show_indexes': True}), 
)
for pattern in oldpatterns:
    urlpatterns.append(pattern)

This has saved lots of time now that I don't have to tweak the settings.py and urls.py file to work with my environment every time there is a change in one of them.

Technorati Tags:

Django mentioned in OnJava.com article about Rails

Django was recently mentioned in an article at OnJava.com. It is great to see that Django is on the radar of some people in the industry, but it still does not seem to garnish the same amount of attention as Ruby on Rails. I sometimes wonder why this is, and I have come up with the following.

  1. Django's lack of a release version. Although this is not true anymore as Django has released 0.9
  2. Ruby on Rails was first on the scene. Event though Django was being developed at the same time as Ruby on Rails, it was not made public until a while after RoR was already gaining popularity.
  3. Django's lack of Ajax. RoR is almost synonymous with Ajax, which brings with it a lot of flash and hype and Web 2.0 buzzwords. Don't get me wrong. I am in favor of Ajax but Django lacks the same one to one relationship that RoR and Ajax has. Ajax support in Django is coming but it is still vague as to what it will be.
You should notice that none of these things are of any detriment to Django at all. I just wonder what else we can do to push Django more into the spotlight. As a Java developer by profession who does work with Django on the side, I have really come to appreciate the benefits of the agile programming framework that Django and Python provide. With books like Beyond Java hitting the shelves I think more and more people are starting to agree.

Technorati Tags:

Django server side file selector

Note: This is no longer valid. You can now use the meta.FilePathField that is built into Django like this

fieldName = meta.FilePathField(path='/path/to/files', match='re_string', recursive=True)
I keep coming up with things that I think Django should have built in, but then end up being super easy to implement with just a few lines of code in the model. The TinyMCE editor was a good example of this, and it happened again with what I call a server side file selector. A project I am working on requires the admin user to select a video file to be associated with a message. I don't want users trying to upload very large video files over a HTTP POST, so they are manually uploaded to a directory via FTP. Initially the admin users had to manually type in the file location to a charField, but that just sucks. So I thought "Wouldn't it be great if I could do something like
videoUrl = meta.ServerFileField('/home/user/video')
and Django would automatically create a select field with all the files from that dir as options." After an hour of looking through core.py and formfields.py it dawned on me that I could just do this in the model. So here it is.
from django.core import meta
import os

videoDir = '/home/jay/video'
files = os.listdir(videoDir)
theList = []
for f in files:
	if os.path.isfile(os.path.join(videoDir,f)):
		theList.append(tuple([os.path.join(videoDir,f), f]))
videoChoices = tuple(theList)

class Message(meta.Model):
    def __repr__(self):
        return self.name
    
    title = meta.CharField(maxlength=200)
    video = meta.CharField(maxlength=200, help_text='Select the video', choices=videoChoices ,blank=True)
...
I had forgotten that Django can produce a select field if you just pass the meta.CharField a tuple of choices. No screenshot of this one since Gimp seems to have a hard time taking a screenshot of a HTML Select box

Technorati Tags:

Django and Xinha

After I posted about Django and TinyMce someone on IRC mentioned Xinha. I had not heard of or used Xinha before, so I thought I would give it a shot. Xinha has some really nice features that are not found in TinyMce, however I was a little bit disappointed at the extra configuration that Xinha required. TinyMce also just seemed a bit more polished.

Note that in order to get this to work I had to specify to Xinha the id's of the textarea's that I wanted to be wysiwyg editors. TinyMce was much more flexible in that it can convert every textarea on the page to a wysiwyg editro

I installed Xinha into my /home/skabber/public_html/javascript/xinha directory and then made symlink into the django/conf/admin_media/js directory.

Inside the xinha directory you will need to create 2 .js files

init.js will hold the follwoing

_editor_url  = "/media/js/xinha/";
_editor_lang = "en";
Note that _editor_url should be the location where Xinha is installed

description.js should contain the follwoing

 xinha_editors = null;
    xinha_init    = null;
    xinha_config  = null;
    xinha_plugins = null;

    // This contains the names of textareas we will make into Xinha editors
    xinha_init = xinha_init ? xinha_init : function()
    {
      xinha_plugins = xinha_plugins ? xinha_plugins :
      [
       'CharacterMap',
       'ContextMenu',
       'FullScreen',
       'ListType',
       'SpellChecker',
       'Stylist',
       'SuperClean',
       'TableOperations'
      ];
             // THIS BIT OF JAVASCRIPT LOADS THE PLUGINS, NO TOUCHING  :)
             if(!HTMLArea.loadPlugins(xinha_plugins, xinha_init)) return;

      xinha_editors = xinha_editors ? xinha_editors :
      [
        'id_description'
      ];

       xinha_config = xinha_config ? xinha_config() : new HTMLArea.Config();

      xinha_editors   = HTMLArea.makeEditors(xinha_editors, xinha_config, xinha_plugins);

      HTMLArea.startEditors(xinha_editors);
    }

    window.onload = xinha_init;

Now we are ready to edit our Django model. Just like in the TinyMce example use the js param of the meta.Admin to pass in 3 javascript files.

js = ('/media/js/xinha/init.js','/media/js/xinha/htmlarea.js','/media/js/xinha/description.js'),

Once that is done, log into the Django admin and add/edit one of your objects to see the Xinha editor.

These instructions only convert meta.TextField's with the name 'description' aka meta.TextField('description'), To change a field of a different id, modify the xinha_editors in the description.js and probably change that files name.

Technorati Tags:

Django on Dreamhost

After lots of googleing and some late night IRC chats I have Django installed and fully working on Dreamhost. The full how to is posted on the Dreamhost Wiki

Technorati Tags:

Django and TinyMce

I have been experimenting with the Django web framework for the past few days and it is great. It is easy to learn, and what it produces from the amount of code you input is staggaring.

One thing I did find lacking was lack of a wysisyg input field. meta.TextField works great but sometimes users need a little more. At first I wanted to create a meta.HtmlField but after some more thought you can achieve the same results with a meta.TextField and the admin.js param.

Before I get into the code here is a screenshot of what we will achieve. Django with TinyMce

Here's how:

First you will need to download the TinyMce code from http://tinymce.moxiecode.com/download.php Extract that to somewhere on your system. I used /Users/jay/Sites/javascript

Next you will need to link the TinyMce code into the Django media directory

ln -s /Users/jay/javascript/tinymce/jscripts/tiny_mce /Users/jay/python/django/trunk/django/conf/admin_media/js/tiny_mce

Your django may be in another dir.

To test that the tiny_mce is in the correct place, fire up the django server

djang-admin.py runserver --settings=myproject.settings.admin
and browse to http://127.0.0.1/media/js/tiny_mce/tiny_mce.js If you see a javascript file the you are on the right track.

Now create a textareas.js file in the tiny_mce dir and fill it with this.

tinyMCE.init({
	mode : "textareas",
        theme : "default"
});

Now we need to edit the model that we want to use TinyMce.

fields = (
        meta.CharField('title','title',maxlength=200),
        meta.TextField('description','description'),
        meta.CharField('location','location',maxlength=200)
    )
    
    admin = meta.Admin(
    		fields = (
    			(None, {'fields': ('title','description','location')}),
    		),
            js = ('/media/js/tiny_mce/tiny_mce.js','/media/js/tiny_mce/textareas.js'),
    )

Notice the "js" param in the admin section. This tells Django to load those javascript files on the add/edit page for this model.

If you have done all this correctly you should see the TinyMce editor for all the meta.TextField's in your model.

For more info on TinyMce, including how to tailor it to your needs go to http://tinymce.moxiecode.com/

Some people may not want to use TinyMce, but the same principals should apply to many wysiwyg editors. I may do another howto using the xinha editor, since someone has aked for that.

Technorati Tags:



Powered by Django.