Varud

Socially Proximate Predictions

Flower

Posts Tagged ‘django’

Django 1.2 Alpha – Template Threading

Django 1.2 Alpha 1 was recently released to developers worldwide.  I haven’t been able to play around with it yet but I am reading through the announced changes and plan to write a series of articles for people making the leap from 1.1 to 1.2 – since that’s what I’ll be doing this Spring.

First of all, this is a giant release.  I don’t expect it to go smoothly and I can pretty much guarantee that some major third-party packages will be broken even when Django 1.2 is released as stable.  One of the major changes is that template node bytecode will now be cached in memory (I think – at least that’s how I understand it).  Most people will say, ‘cached is faster than not cached … this is great’.

Unfortunately, what this really means is that the web server will cache the code and run it across all the threads that share that process memory pool.  Now, imagine you are on a default Apache installation on a modern OS.  These days, that Apache will be running in a multithreaded configuration.  That means that each thread (end user) will hit that bytecode in a shared fashion.  If you (or a third party developer) have written any custom template tags, this can be a problem.

Thankfully, the fantastic Django docs point this out and explain why this matters.  For the lazy, I’ll reproduce version 1.1 compatible template tag code that could drive the cycle tag:

{% cycle 'row1' 'row2' %}
And the Python code behind it:
class CycleNode(Node):
    def __init__(self, cyclevars):
        self.cycle_iter = itertools.cycle(cyclevars)
    def render(self, context):
        return self.cycle_iter.next()

To take their example, if you write a tag that cycles different styles for list items, and two threads hit that tag node, you might get cycling that crosses the thread boundaries.  Typically, one client request is getting one request, and another the other.  One client in that example would get two odd styles, and the other would get two even styles – even though in Django 1.1, since the template tag node was not cached, each user would get an odd, even cycle of their styles – the expected behavior.

What this means for people with custom tags?  In English, this is only really an issue if context can’t be global.  Keep in mind that the variable passed will still be thread-safe (they are stored in the thread, not the template node code).  If you are using a template tag that depends on the context of the template at a given moment, then you need to worry and follow their advice of testing render_context, if not, it’s ok.

Keep posted for a discussion on the Messaging API next.

Django 1.1 Released

Wow!  Django 1.1 has finally been released.  This has been a great effort by the authors and should be a huge boon to the community.

http://www.djangoproject.com/weblog/2009/jul/29/1-point-1/

I’m already doing everything with 1.1 so when this anti-fraud product is ready, it will be on there.  Key features (for me) include grouping and advanced admin customization methods.

Install Eclipse Galileo for Django and Pinax – Part 2

Now, onto getting Pinax installed for development purposes:

I really don’t like how things have gone with Pinax installation in the past few months. I’m going to give my own take on things. Pinax has moved to GitHub and so should you.

First, create an account at GitHub. Then, fork Pinax so you have your own copy to work with. For me, I have my development version here. If you want this repository to be private, you can pay GitHub $7/month – well worth it if this is a corporate gig.

Now you can follow the Pinax directions from your own fork.

$ curl -O http://github.com/AdamN/pinax/raw/master/scripts/pinax-boot.py
$ python pinax-boot.py --development ../Documents/workspace/WhateverYouNamedProject/src/pinax
$ source ../Documents/workspace/WhateverYouNamedProject/src/pinax/bin/activate
(pinax)$ cd ../Documents/workspace/WhateverYouNamedProject/src/pinax
(pinax)$ pip install --requirement src/pinax/requirements/external_apps.txt

At this point, if you have an existing installation of Pinax on your machine, you may get an error about django-wikiapp being the wrong version. In a new command line tab, run:

pip install django-wikiapp==0.1.2

If it says it’s already installed, you may need to delete the existing django-wikiapp in the directory shown by the error in the pip install command above. Now it’s time to start a project:

(pinax)$ pinax-admin clone_project basic_project myproject

You now have a Pinax site in myproject/ directory

(pinax)$ cd myproject
(pinax)$ ./manage.py syncdb
(pinax)$ ./manage.py runserver

Now, go to your browser and navigate to http://127.0.0.1:8000

Hopefully, you now see a Pinax website in all its glory.

Install Eclipse Galileo for Django and Pinax – Part 1

Today I decided to see if I could do a proof of concept Django/Pinax deployment for an anti-fraud advertising service. Since they already use PHP for their legacy codebase and since the existing developer uses Eclipse and the Zend Plugin for debugging, I decided to use Eclipse for the proof of concept (since I have to convince that person to switch to Django).

First things first. Update your Mac to the latest and greatest software and JVM. A new JVM literally came out today so that’s what I did.

Now, go download the bleeding edge release candidate Eclipse for Java EE Cocoa Version – Galileo. Some people will probably want something else, but I say that Cocoa apps are way nicer than Carbon ones and anyway, I went with Galileo. The Java EE version has web development tools built in there which I thought would be important. Feel free to comment below with a leaner way to do this – I’m not using Java.

Now, grab the new version of Subversion (if you’re using Subversion). I got, and highly recommend, version 1.6.2. I usually don’t get the latest SVN packages but when trying 1.4.x it did not work.

Spin up Eclipse and grab some plugins. You’ll need the PyDev and Subclipse plugins. Go to Help > Install New Software, and install these:

Get the 1.6 series of Subclipse here:

http://subclipse.tigris.org/update_1.6.x

PyDev for debugging and highlighting:

http://pydev.sourceforge.net/updates/

For Subclipse, you’ll get alot of options. Just install everything (again, I’m usually lean but I tried not installing everything and had issues – I would just do it all).

Now, run through the fantastic Getting Started guide on the PyDev website. It’s written for Windows and a previous version, but you should be able to get through everything.

When you’ve gone through that, it’s time to get your existing SVN project into the project you created for PyDev above. Just right-click on the project and select ‘Import’. From there, choose SVN and import from your svn repository (if you have one – otherwise, skip this).

I will get to Pinax on the next post, when I figure it out :-)

Events in Django

Being that I am creating a site tightly coupled with events and calendars, I need some event functionality.  There are three options:

  1. The event subsystem from Django Basic Apps
  2. Somebody’s self-maintained event app
  3. And … the event system that’s in Pinax

Option 3 really is great.  What a great app written with some really good methods including Periods and Relations.  I’m very excited to use it.

Site Hijacked

I’m not sure if anybody noticed, but the site was taken down for a few days because somebody defaced it.  I kind of liked the music that they put on my homepage, but after listening to the song, I had to go about fixing it.  What did I learn?

  1. Don’t use shared hosts.  In this day and age, unless you are super poor, do not use a shared host.  If you are not technically savvy, use services for specific tasks (i.e. blogger.com for a blog) so you don’t have to think about the technology/maintenance.  If you are technically savvy and you have needs that are not basic, get a server where you have root access.  AWS ec2 is awesome for this.  The cost is $75 a month though so it may be too expensive for some people (although if you can set one up – I can probably find a gig for you to make enough money to pay that $75/month – just drop me a line).
  2. Dreamhost.com, while pretty good and having good customer service, just isn’t what I need.  People have recommended gandi.net for name services and hosting if you need the cheap option for hosting.  I haven’t used it personally but knowledgeable people love them.

Oh, I don’t think I’ve done an update on platform choices either.  Here is where I stand now:

  1. MySQL 5.0.45 (I’m still contemplating PostgreSQL)
  2. Python 2.5.1
  3. Apache 2.2.9
  4. Fedora 8 (I like the Fedora/Red Hat series – Debian/Ubuntu was giving me grief).
  5. Django 1.0
  6. Pinax trunk

That was fun!  I feel very excited about the Python community.  It’s a smaller group but it’s growing fast and it has solid support out there.  In addition, it’s a rock solid language with full unicode support – I love that.

Frameworks and languages, oh my

Recently, I’ve begun the painful process of actually deciding what core languages and frameworks will be used for the application.  Some things are kind of obvious for everybody, and leads to my favorite phrase from my days of studying for the SAT – PoE (Process of Elimination).

One dimension to start eliminating on is the breadth of the network supporting that technology over the coming 2-3 years.  Perl is a language that is great, rock solid, and also drifting into obsolesence.  I feel bad for Perl but what are you going to do?  This also leaves out old versions of languages like PHP 4.

Another issue is licensing and money.  That cuts out Microsoft products right off the bat as well as some of the supported platforms like WebSphere.  I haven’t been a Microsoft guy for years although they’re still the only game in town for word processing and spreadsheets.

At this point, there are some things that just go along with what I’ve done before.  I’ll most certainly use a Linux flavor for the OS – either Ubuntu (which I’ve never used in production) or CentOS (which is the same as Red Hat but is free).  After having some frustrating days trying to deal with a font installation problem (of all things) on an Ubuntu server, I’m starting to consider CentOS.  Either way, it’s not much of a difference, either will do.

This operating system needs to be on a machine of some sort.  I don’t want to buy a machine and grow out of it though – I’d prefer to rent.  That leads to a slew of hosting options.  One of the big guys is Dreamhost.  I’ve worked with them on some minor projects and they’re great in alot of ways.  However, I need full control, root access, the ability to create an entirely new instance, etc…  Dreamhost won’t do.  It’s time to take advantage of the cloud so I’ve been experimenting with Amazon Web Services Elastic Compute Cloud (AWS ec2) and Simple Storage Service (S3).  For about $72/month, I can have a dedicated ’server’ on a premium network (Amazon’s) with access to S3 which allows for such things as snapshots (backups) and getting data into a solid storage space.

This post will have to be continued – I can’t nearly finish this work so quickly.  Does anybody have thoughts?  Here’s the current plan:

  • Amazon Web Services ec2,S3, etc…
  • Linux (Ubuntu or CentOS)
  • Apache (I hadn’t really thought about anything else, possibly lighttpd)
  • Python or Ruby (PHP feels passe and there’s really no other option)
  • If Python, Django or Turbogears (probably Django because Google App Engine is using that)
  • If Ruby, Rails or Merb

I could still be convinced on alot of fronts – this is hard :-)