Geographic Targeting in Django

Posted by Ian Holsman Fri, 18 Aug 2006 06:10:00 GMT

on a similar vein to Coulix’s contribution, I have written a context_processor which allow you to see what city/region/etc your visitor is coming in from

It uses MaxMind’s GeoIP database.

warning.. this module is GPL due to MaxMind’s code being GPL. putting it into your codebase will make it catch a virus.

Posted in  | Tags ,  | 1 comment | no trackbacks

I'm a fair person

Posted by Ian Holsman Thu, 17 Aug 2006 06:45:00 GMT

So.. After doing my screencast covering how easy it was to get a django site up and running on webfaction I thought it would be only fair to show people how easy it was to get a rails app (typo) up and running on webfaction as well

Thanks to Remi for letting me do this.

Posted in  | Tags , , ,  | no comments | no trackbacks

handy django middleware for shared hosting

Posted by Ian Holsman Wed, 16 Aug 2006 10:49:00 GMT

on some shared hosting environments like webfaction they install a reverse proxy solution where you run a apache2 webserver on a custom port, and the main webserver forwards the request through to you.

This works great, and I’ve been using things like this for years, but the problem you get is that all the requests appear to be from 127.0.0.1 (or the machine which is doing the proxying for you) which isn’t the best if you want to record that stuff.

webfaction fixes this in the log files for you automatically, but that doesn’t help you if you want to do something with the IP# in django.

enter FixIP it takes the X-Forwarded-For header sent by compliant servers which holds the real IP and makes django work properly.

installation is easy.. just add the routine to your middleware settings ala
MIDDLEWARE_CLASSES = (
    "django.middleware.common.CommonMiddleware",
    "zilbo.common.utils.middleware.fixip.FixIP",
   ...

Posted in  | Tags  | 4 comments

Talk about a fast response

Posted by Ian Holsman Tue, 15 Aug 2006 08:59:00 GMT

on the Django user mailing list a new user (Gloria) asked a question about how to start django.

she got 10 helpful answers in the spate of 10 minutes.

the question is a common one, so I’ll re-post it here for google to find.

[...] I have decided to give Django a serious look, and I have a simple question. After running this command:
> python manage.py runserver 1111
Validating models...
0 errors found.

Django version 0.95, using settings 'wi_2.settings'
Development server is running at http://127.0.0.1:1111/
Quit the server with CONTROL-C.
I expect to get to this URL from a remote machine, like this:

http://dev.blah_server.com:1111

I’m sure I’m missing something simple. Please let me know what url parameter to set to access my page from a remote machine.

the answer of course is django by default only fires up the webserver on the localhost port… to override this you need to add the ip# to the command line.

> python manage.py runserver 0.0.0.0:1111

Posted in  | Tags  | no comments | no trackbacks

My Django Screencast has been updated

Posted by Ian Holsman Thu, 10 Aug 2006 17:18:00 GMT

The screencast about installing django at webfaction has been updated and the voice quality which plagued the original has been fixed.

I’m still not 100% sure on why it happened. it was either due to me having a USB mouse installed in the other USB socket of my macbook pro, or the right-side USB adaptor on my laptop is flaky.

If anyone has any requests for screencasts (on using django) feel free to ping me, and if I have some spare time I’ll create it. (I actually enjoy doing them)

Posted in  | Tags  | 2 comments | no trackbacks

Nasty Django gotcha in my code

Posted by Ian Holsman Thu, 10 Aug 2006 03:33:00 GMT

So while trying to debug a small problem with bad urls appearing in my code and other things which looked like django or mod_python had some kind of memory leak, I stumbled onto a bug in my code.

The problem I was seeing was that on forms where I create a ‘new’ object details from the previous screen were appearing, and sometimes links were using the previous’s page url were being used instead of the current one.

So after talking to One of the Python Experts, Malcom we narrowed it down to how I was using the ‘extra_context’ parameter being passed into the request.

taking a step back for a second the ‘standard’ way most people set up their urls.py is something like


context= {
        'app':'forum',
}
and then define the URL patterns like this example…
urlpatterns += patterns('zilbo.common.tag.views.tag', 
        (r'^$', 'object_list', { 'queryset': Forum.forum_objects.all(), 
           'allow_empty':True, 
          'extra_context': context }),

)

my bug was that I didn’t realize that python passed ‘extra_context’ by reference. and I was blindly assigning things to it in the view itself. (updating the main copy). eg.. my code had something like

extra_context[‘forum’] = forum_obj

so when the next request came on the same thread it would have got a modified context dictionary (instead of the original one). and if my view didn’t actually set that variable (as was the case for the create view) it would take the old version. and would show the details as if it was an edit, not a create.

So the quick-fix in the code at the moment is to just copy() the extra_context where I modify it. and longer term is to treat it as a read-only variable.

hopefully the next djangoite with this problem will find this blog post and save themselves a few grey hairs

Posted in  | Tags ,  | 2 comments | no trackbacks

Two new chatters

Posted by Ian Holsman Fri, 04 Aug 2006 07:59:00 GMT

I’ve just launched 2 new ’’chatters’.

Med Chatter

and

Car Chatter

they follow the release of VC Chat which was soft-launched a week or two ago.

all are written in Django, and use zyons as their base.

I’ve got a couple of ideas on the boil which I will launch on those sites in about a week.. stay tuned!

Posted in  | Tags ,  | no comments | no trackbacks

Django: Alive and well in Melbourne

Posted by Ian Holsman Fri, 04 Aug 2006 07:34:00 GMT

I got a email recently from a local software house, to act as a mentor to them for a small project they are starting to write with Django.

In the week since I recieved the email, and actually visiting their site, they had figured out most of their issues and had their own little a skeleton of their application up and running.

The issues they were having after 1 week were:

  • Ajax: These guys were Python/PHP/Oracle developers, while they were excellent coders in that language, they had little to no experience in using ajax. They didn’t even know about dojo or prototype.. (or JSON for that matter). My thought was “why should they have to know about it.. this kind of BS should just be invisible to them”.
  • Common Login: They were over the moon when I told them this was easy using multi-auth and they could use the apache module (mod_sso from oracle) to do the heavy lifting and just grab the REMOTE_USER apache passes through. They thought this was a big selling point to their customers.
  • The Standard operating environment of their clients: Their clients are still on apache 1.3 (as oracle ships with 1.3). so I had to break it to them that 1.3’s version of mod_python is not compatible with django. Luckily fastcgi works
  • Oracle integration: They are a oracle shop. and while they can use postgres or mysql, all their base data was in oracle.

One interesting comment they made was that they felt that django was a “niche” product, and it was mainly meant for a large publishing company. By this they meant the form handling that django offers isn’t as well developed as it could be. Adrian recently let it slip that something ‘awesome’ was in the works and well be made available soon in this area..

Posted in  | Tags ,  | 5 comments | no trackbacks

i18n tip for django

Posted by Ian Holsman Tue, 01 Aug 2006 03:01:18 GMT

Django’s Internationalization support is freaking awesome. and it is so easy to use it isn’t funny.

Django’s i18n support is based on the brower sending a accept-language, or having the server set a cookie to handle the user preference.

I was in the middle of porting a legacy app for a client which didn’t work the way the designers of i18n had thought of.. they set the language in the URL itself for the request. (ie /challenge/spanish/signup.html was the spanish version of the page and /challenge/english/signup.html was the english version) .

now..this presented 2 problems. first ‘english/spanish’ are not language codes, and more importantly I wanted the server to render the page it was asked for in the language specified.. (it couldn’t trust the cookie in this instance).

so.. an easy solution at the top of each view I added a call to ‘setlang’

def setlang(request, language ):
    if language == 'english':
        translation.activate('en')
        request.LANGUAGE_CODE = translation.get_language()
        request.session['django_language'] = 'en'
    elif language == 'spanish':
        translation.activate('es')
        request.LANGUAGE_CODE = translation.get_language()
        request.session['django_language'] = 'es'
this also had the effect of not requiring every page to have the language code in the URL (we forgot some), as we can assume the user will be entering into the site via a language-encoded landing page first.

now this code isn’t ‘beautiful’ and is a bit hackish. but it met the needs of my client, and like most projects i18n was left to the end when we were had zero time..

It also has the benefit of keeping the english and spanish separate in the URL space, so google and other bots will find (and index) both languages.

other tips for i18n work (coming from a first-timer after reading the docs):
  • fix up the content-type in the .po file when you initially generate it. by default it puts in ‘CHARSET’. I just copied what the default django locale had and it seems ok.
  • remember to restart your server after you recompile the languages (thanks FunkyBob from IRC)
  • MS word is evil. it puts in funny UTF characters instead of a regular ”’”. gettext doesn’t like them, and you will need to find all of them and replace them with ascii characters. (this took the longest time for me)
  • DOS-format files don’t work with gettext.. gettext expects lines to be ended with a simple ’\n’ not ’\r\n’
* Don’t put ALL the text in the blocktrans section in the template. just a keyword will suffice, instead of the 5-block english copy. I wish I knew this before hand. as the templates have large english blurbs in them which have to be taken out and put into the .po file. eg.

{% blocktrans %}welcometext{% endblocktrans %} 
instead of

{% blocktrans %}Welcome to the new and improved version of FOO BAR.... {% endblocktrans %}
and the stuff inside the blocktrans is the actual key inside the .po file. and this will confusing as HTML editors fix up typos in the HTML and forget to update the .po files and so on.

FWIW.. if you have got all your ‘trans’ and ‘blocktrans’ stuff done in the templates, it takes about 30 minutes for a complete i18n newbie to create a multi-lingual site.

FANTASTIC.

Posted in  | Tags ,  | 4 comments | 1 trackback

My Startup .. some figures

Posted by Ian Holsman Mon, 24 Jul 2006 06:32:02 GMT

So as some of you might know, I’m doing a startup written in django in between consulting gigs.

So Far the breakdown is as follows for the month:

  • Ad-based revenue: $36 on about 30,000 page impressions, compared to last month thats 150% up on page views and 50% less in revenue.. go figure.
  • Product Sales: $ 60
  • Subscribtion Sales (jobs): $0

I’ve spent about ~10-20 hours of my time doing it, and about $30 in buying domains/hosting. At the moment I’m after growth in page views, so I’m happy(ish).

Posted in  | Tags  | 4 comments | no trackbacks

Older posts: 1 2 3 4 ... 7