Entries in category “Django”
More slides
Published March 16, 2008
I gave a quick talk at the Django open session last night, and I’ll be giving it again here in a few minutes as a proper lightning talk, briefly walking through the process of some of the database-backed journalism projects we do at the Journal-World (specifically this feature on crime reports at the local university campus).
Slides are already online. If you’re at PyCon and want to hear the talk, come to the ...
Slides
Published March 15, 2008
I just finished my talk, which apparently included a bonus live transcript on IRC by Justin Lilly, and I think it went pretty well considering how much ground it had to cover and how nervous I was up until the moment when I started talking.
If you aren’t at PyCon, or if you are but didn’t come to the talk, or if you just want to download them and mash them up, my ...
Managers versus class methods
Published February 25, 2008
In the triumphant return of “James answers questions from the django-users list”, today I’d like to take a few moments to talk something that’s recently become something of a hot topic, spawning not one but two threads, as well as a couple off-list email discussions: what, exactly, is appropriate to put into a method on a custom manager as opposed to, say, a class method, and when and how can you tell?
This ...
On “conceptual integrity”
Published February 11, 2008
Mark Ramm has a nice post this morning talking about Adam Gomaa’s discussion of “conceptual integrity” in Python web frameworks; I think it’s worth a read.
The key point Mark touches on is good documentation: ensuring that people see and understand the overarching structure of the framework is, ultimately, the key to helping people understand how it can help them get their work done. In the case of Django there is an official ...
Miscellany
Published February 10, 2008
Between normal work, working on the book, working on my upcoming PyCon talk and working on a couple little things on the side, I haven’t had much time for blogging lately, especially about Django. But I’ve built up a collection of little things that need to get posted, so I’m just going to start dumping them out here and then get back to not having any free time :)
A django-registration update
I ...
Media moves
Published February 7, 2008
As of sometime this weekend when the DNS change propagated, all static/media files for this site migrated over to Amazon’s S3 storage service; when I relaunched back in September, media was coming off a Joyent BingoDisk account. While a change in media serving technology really isn’t all that sexy or exciting compared to some aspects of web development (or as popular as writing about whatever Microsoft is doing today), I’d like ...
Web frameworks and web hosts
Published January 10, 2008
Today John Gruber linked to a post on the official Dreamhost blog lamenting the state of web-framework deployment on consumer-level shared hosting; while the post itself is largely concerned with Ruby on Rails, the current state of deploying popular (non-PHP-based) web frameworks on hosts like Dreamhost is, well, abysmal. A search for “dreamhost” in the archives of the django-users mailing list turns up nearly five hundred results, many of them people struggling to ...
The why and wherefore
Published December 31, 2007
So, let me straighten a few things out.
First off, this is my personal blog. What you see here is me speaking my own personal mind, and I neither represent nor want to represent anything larger: not Django, not Python web development, not my employer, not anything except me and whatever I feel like writing about. This is an important point to keep in mind, because I don’t have any claim to represent anything ...
With apologies to John Gruber
Published December 31, 2007
For a little while now I’ve been trying to stay out of Python framework debates, partly because I just haven’t had the time and partly because things seem to be taking a turn for the nasty again. But recently it’s been harder and harder to keep away, especially with the hard work of a new entrant into the arena: Noah Gift, whose blog posts at O’Reilly have, in the last month ...
Speaking and writing
Published December 12, 2007
First off, if you’re going to PyCon (Chicago, March 14-16), make some room on your schedule; there are several interesting talks on Django scheduled, including one by yours truly.
The description of my talk on the PyCon site is somewhat pithy on account of space restrictions, but conveys the general idea of what I’ll be talking about; flexible, reusable Django applications are one of my pet topics, and I’ll be covering some ...
Requiring HTTP methods
Published December 11, 2007
Just a quick tip today: someone on IRC tonight was asking for an easy way to write a Django view which restricts itself to only allowing a specific HTTP method or methods. For example, a web-based API might want to only allow POST to specific views.
This is actually pretty easy to do with a set of decorators built in to Django, in the module django.views.decorators.http. Specifically, the fix for ticket #703 ...
A couple updates
Published December 9, 2007
Today marks two releases, both numbered 0.4, of django-registration and template_utils.
The new release of django-registration is largely a matter of policy; there’s no new functionality or features, but there is one backwards-incompatible change: the validation of passwords (verifying that the same password is entered in both fields) has been moved to the clean() method of RegistrationForm, which means that the error message from a password mismatch is now accessed via form.non_field_errors ...
The magic of template tags
Published December 4, 2007
Over the last couple days I’ve spent some time discussing the word “magic” and exploring just what it really means, with an emphasis on the fact that a lot of “magic” in programming — though initially counterintuitive and not at all what you’d expect to have happen (and it’s precisely this reason which usualy makes “magic” a bad idea) — boils down to applications of fairly simple principles. As a real-world demonstration of that ...
Making magic
Published December 3, 2007
In yesterday’s article I spent a fair amount of time talking about the word “magic”, specifically in the context of Clarke’s Third Law, which states that
Any sufficiently advanced technology is indistinguishable from magic.
A big part of what I was getting at was that a lot of things which seem to be explicable only by appealing to “magic” are really just cases of technology — sometimes extremely simple technology — being used in a ...
Clarke’s Third Law
Published December 3, 2007
Every so often, a TV producer who wants to get ratings will air a “documentary” about some wonder of the ancient world. Say, the great pyramids at Giza. The formula for this “documentary” is pretty simple: you get a bunch of people from modern, industrialized nations to go crawl over these huge ancient monuments in Egypt and speculate on how those ancient Egyptians managed to build them. And, inevitably, a lot of these people will ...
Where is Django’s blog application?
Published November 29, 2007
In terms of people looking for sample code or example applications to use/learn from, “where can I find a good Django-powered blogging application” is probably at the top of the frequently-asked questions list both on django-users and in the IRC; part of this is simply that, right now, there is no “definitive” Django blogging application; there are a bunch of them available if you go looking, but you’re not likely to get anyone ...
Keeping up with the Django
Published November 29, 2007
Thanks to a couple big projects at work that I’m trying to finish up in time to devote my full weekend to the sprint, I don’t have anything directly code-related to throw out today. Fortunately, there are plenty of things people ask about Django which have nothing whatsoever to do with code, and I can deal with one of them today: how do you keep up with Django development?
Every so often, people ...
Performance tips
Published November 27, 2007
Nearly two years ago, Jacob posted a round-up of useful tips for Django performance, geared mainly at the non-Django portions of your stack; suggestions like having dedicated media and database servers, memcached, plenty of RAM and database tuning really aren’t Django-specific. Two years later, all of his tips are still relevant and will still have an impact on the performance of your Django-based stack.
This leaves wide open the question of how to squeeze ...
Django sprint
Published November 26, 2007
I’ve got a couple articles brewing in the back of my head to finish out the month (and not a bad month, even with the unexpected break last weekend), but rather than write one of them tonight I’d like to pause and remind everyone that this Saturday — December 1 — is a major sprint day for Django. We’re hoping to make quite a bit of progress on some as-yet-unfinished features which need to ...
Newforms, part 3
Published November 25, 2007
Now that I’ve got the user-profiles application out in the wild, let’s take a look at one last important feature of Django’s newforms library: the ability to dynamically generate forms “on the fly” from model classes, via the helper functions form_for_model and form_for_instance. And, just as importantly, let’s look at why they shouldn’t always be the first thing you reach for when you need to build a form.
How model-generated ...
User profiles
Published November 24, 2007
Django, as you hopefully already know, provides a way to store additional information related to a User via the mechanism of setting up a site-specific user profile model, and supports this directly via the get_profile() method on every User object.
By way of supporting this, back in version 0.2 of django-registration I added the ability to specify a callback function which, upon creating a new User, could also create a default site-specific profile for ...
Newforms, part 2
Published November 23, 2007
Yesterday we took a look at how Django’s newforms library works and explored the different components, how they fit together and how the overall process of form validation takes place inside a newforms form. And while that’s all useful knowledge, it’s helpful to have some practical examples to see all of the various bits in action, so today we’ll look at a simple example which shows off some of the features ...
Newforms, part 1
Published November 22, 2007
If you’ve ever had the pleasure (and I use that term somewhat loosely) of working with Django’s original form-processing system (which now lives in django.oldforms and is in the process of being deprecated), you’ll know that the replacement currently under development (which currently lives in django.newforms and will eventually become simply django.forms) represents an immense simplification and adds quite a few capabilities which weren’t present before. But because ...
Auth tips and tricks
Published November 21, 2007
Django’s bundled authentication framework is stupendously useful as an out-of-the-box solution for common user-authentication needs, and provides a ton of things basically for free: users, permissions, groups, login/logout functionality, middleware for automatically detecting the user’s identity, the ability to restrict views based on various attributes (including staff members, superusers, permissions and arbitrary tests), the list goes on and on. For probably 90% or more of the applications you’ll develop, the simple ...
Initial data and install-time code
Published November 21, 2007
A fairly frequently-asked question is something along the lines of “how do I provide some data which gets installed along with my application?”, or some variation on that, often including questions about how to ensure a particular bit of code is run when the application is installed via syncdb. Django provides several different ways of approaching this, depending on the exact situation and exactly what you need to do, and while they’re mostly documented ...
My streak ends
Published November 14, 2007
First up, the bad news: my streak of one Django-related post per day in November is going to come to an end tomorrow. Some Thanksgiving plans (technically pre-Thanksgiving by a week, but nothing wrong with celebrating a little early) I’d had which were first on, then off, and are now on again, will be taking me out of town and probably offline from tomorrow morning through Sunday night. I toyed with the idea of ...
Instant web sites
Published November 13, 2007
As much fun as it is to write about features of Django which aren’t that well documented, or which aren’t documented (_yet_; one of my goals with each sprint we do is to add to Django’s documentation and bring it closer to being truly comprehensive), there are a ton of features in Django that, while documented, don’t seem to get a lot of use. Which is a shame, because some of ...
More on admin documentation
Published November 12, 2007
Since the last entry on admin documentation tricks seems to have been popular, and showed off a couple lesser-known things you can do, let’s take another look at the admin documentation system, and a couple more things you might not know about; in fact, until today I didn’t know about them, so if nothing else this entry will remind me that they exist and that I need to use them more.
Admin docs ...
comment_utils 0.3
Published November 11, 2007
Since it’s Sunday and nobody’s reading right now, I’ll sneak in a release announcement instead of a pure “Django tip”: I’ve just released version 0.3 of comment_utils.
I’ve been running it for a little while, and it seems to be fine; if you run into any problems, please file a bug report.
Fixes and tweaks
Two big changes in this version are Unicode support and accommodations for a new ...
Unicode in the real world
Published November 10, 2007
Django’s Unicode branch merged back over the summer, but I imagine there are a lot of people who are still using pre-Unicode versions of Django and so haven’t started updating their code yet (it was several months before I had fully updated this site, for example). For the most part, updating to work with post-Unicode Django is pretty easy (and made even easier by Malcolm’s handy checklist), but there are still some ...
Living without projects
Published November 9, 2007
The official Django tutorial walks you through the process of developing a Django application inside a Django project; in this view of the world, the “project” is seemingly the major focus, and the “application” just happens to be something you stuff inside it. For the purposes of learning and exploring Django — especially if you’re new to Python and unfamiliar with things like how to create your own modules and set up your Python path ...
Moving into production
Published November 8, 2007
At some point, every Django-based project (hopefully) moves out of the development phase and into production deployment, and this can be a traumatic experience. As a result, there are a few things you’ll nearly always want to do during this process in order to transition smoothly and ensure you go to production with all your code humming along; this time around we’ll look at a few of the more common items, and if ...
Documentation bookmarklets
Published November 7, 2007
Django’s admin application is capable of generating and displaying a variety of useful documentation (though once newforms-admin lands, that will be broken out into a separate documentation-only application), including lists of installed models and registered template tags, and a listing of valid URL patterns and the view functions they map to, along with the docstrings of those functions. But it also contains a page of bookmarklets — bits of JavaScript to be dragged into your ...
URLConf tips and tricks
Published November 6, 2007
I’ve written a couple of things recently talking about useful things to be aware of when you’re putting together the URL configuration for a Django application — one covering a pitfall you should watch out for with regular expressions and one touching on the utility of the “url” tag and the “permalink” decorator, and the “reverse” utility — but you can never have too much useful information about URL configuration, because for a lot of ...
“Server startup” and its discontents
Published November 5, 2007
A fairly common question people ask on the Django mailing lists and in the IRC channel is “how do I have something run when the server starts up?” This is a somewhat tricky question to answer, because in many ways there’s no concept of “server start” in Django, and because the reasons behind the question vary from one use case to the next. So let’s take a look at just what people are ...
Working with models, part 2
Published November 4, 2007
Now that we’ve got a handle on how to generically load models without necessarily knowing in advance which models, it’s time to start looking at interesting things we can do with them once we’ve got them. There are lots of uses for code which can introspect any model and extract information from it, some of which you’ve probably already seen:
- The Django admin interface introspects model classes to determine how to ...
Working with models, part 1
Published November 3, 2007
In order to do all the things it does, Django has to contain quite a bit of generic code that’s capable of working with pretty much any model or combination of models you want to throw at it, and has to be able to introspect those models to get information about their fields, their relationships and other useful tidbits. There are also plenty of real-world use cases where your code needs to be able ...
Handle choices the right way
Published November 2, 2007
A lot of common use cases involve a model field which needs to accept values from a restricted set of choices; for example, a field for selecting a US state should, logically, only allow values which correspond to actual US states. In Django’s ORM, this is represented by using the “choices” argument when defining the field, and generally this provides a fairly easy solution.
But it’s not always perfect: while string-based values (such ...
Django tips: template loading and rendering
Published November 1, 2007
I’ve been reminded today by Maura that November is National Blog Posting Month, when — in theory — bloggers the world over try to keep up a pace of one entry every day. I don’t know how well this is going to go, but I’d like to give it a try. And, inspired by Drew McLellan’s excellent 24 ways “advent calendars” of web-design tips, I’m going to give it a theme: one ...
Be careful with your URL patterns
Published October 14, 2007
Tonight in the Django IRC channel, someone stumbled across a seemingly-odd error when trying to use a generic view:
TypeError: object_list() got multiple values for keyword argument 'queryset'
The problem turned out to be the URL pattern which was routing to the generic view. Consider a simple example, as might be found in a weblog application:
from django.conf.urls.defaults import * from weblog.models import Entry info_dict = { 'queryset': Entry.objects.all() } urlpatterns = ('', (r'^(index ...
Another django-registration update
Published October 7, 2007
This one’s just a quick heads-up, but if you’re following django-registration you’ll want to take note: I’ve been doing a series of minor releases since 0.3, and it’s now up to 0.3p5.
There have been a few bugfixes since 0.3 that you’ll want, and also some important changes:
- The example templates have been removed. They were simply causing too much hassle from expectations that they’d ...
Standalone Django scripts
Published September 22, 2007
In the grand tradition of providing answers to frequently-asked questions from the django-users mailing list and the #django IRC channel, I’d like to tackle something that’s fast becoming the most frequently-asked question: how do you write standalone scripts which make use of Django components?
At first glance, this isn’t a terribly hard thing to do: Django’s just plain Python, and all of its components can — in theory — be imported and used ...
django-registration update
Published September 19, 2007
For those of you who are using/following it, I’ve just pushed out django-registration 0.3. The basic workflow of the application is still the same:
- User signs up for account.
- User receives activation email.
- User clicks link in email to activate account.
But under the hood quite a few things have been rearranged to make the application cleaner and more extensible, and there have been a couple of backwards-incompatible changes for anyone using ...
Django sprint wrap (for me)
Published September 16, 2007
I don’t know whether anyone else is going to continue on for the rest of the night, but I’m exhausted and I’ve got to be at work in the morning.
Today, for me, consisted of reading every open ticket in Trac and doing as much triage as I could; a lot of duplicates got closed, a lot of similar issues got consolidated, a lot of ancient tickets which have either been fixed ...
Sprinting all the day long
Published September 15, 2007
Wow.
So the “official” Django sprint day is over, though (in my time zone, at least) work actually started yesterday and looks like it’ll keep on going into tomorrow. Already the amount of activity (as measured by the Trac timeline) is simply astounding, and that’s likely to continue; there have been about 150 commits so far, and I couldn’t even begin to count the number of patches that have been filed and ...
Tips and tricks
Published September 6, 2007
In a few days it’ll be the weekend and I’ll (hopefully) have a bit of news regarding updates to the various applications I spun out during the process of rewriting all the code I’m using here, as well as the first official release of the blogging application I’ve developed (you can probably already find it if you know where to look, but for now it’s neither documented nor supported). In ...
ORM Wars
Published September 4, 2007
Last week while I was still on blog hiatus, Adam Gomaa wrote up a pretty constructive summary of why he prefers SQLAlchemy over the default Django ORM, and documented how he made SQLAlchemy a little less painful to use by writing a set of helper functions before moving on to announcing that he’s writing his own declarative layer — borrowing somewhat from Django’s model syntax — on top of SQLAlchemy.
I went back and read ...
Django, Accelerated
Published September 4, 2007
As of yesterday, this site is served from a Joyent Accelerator, running Django via Apache/mod_python backed by a PostgreSQL database. This setup probably isn’t for everybody, but if it’s right for you (and only you can make that call), here’s how I got everything running.
First things first
Make sure you’ve taken preliminary steps to secure your Accelerator before you do anything with it; this means things like setting up ...
Honey, I’m home
Published September 3, 2007
If you’re seeing this, congratulations: the DNS changes have propagated and you’re seeing this site, in all its redesigned glory, at its new home: a Joyent Accelerator.
This took quite a bit longer than I’d expected, largely because — while ostensibly working on redesigning the site and re-writing the blog application which powers it — I kept feeling the urge to spin off various bits of functionality into their own generic, standalone applications. Now ...
A quick survey
Published July 5, 2007
As you’ve hopefully noticed if you’re following things in the world of Django, the Unicode branch merged today, which means that Django is now safe for Unicode at every level (pending any bugs which need to be fixed). That’s great news for users of Django.
But it also means that developers of Django-based applications need to be aware of how Django handles Unicode and watch out for boundaries between Django and any ...
Hacking comments without hacking comments
Published June 25, 2007
Way back when I first set up this site, and threw together a minimal blog application to power it, I didn’t bother much with enhancing Django’s comment system. But later on, when I started getting a lot of comment spam (trivia: as of this moment, 15,153 comments have been posted here, and 14,406 of them — roughly 95% — have been spam) I realized I needed to do something, and quickly hacked Django ...
Hypothetical framework choices
Published June 2, 2007
I subscribe to feeds of items tagged with “django” on a couple of sites, and tonight I noticed a link which posed a hypothetical situation of wanting to port a large J2EE application to either Rails or Django, and asking about making the choice between them.
I wrote up a reply, but attempts to post it there netted me “The page you were looking for was not found. Moron.” So here it is in all ...
django-registration update
Published May 29, 2007
I’ve just packaged up version 0.2 of django-registration; there are two big changes since the initial release which are worth mentioning (and which are, of course, covered in the documentation supplied with the download):
-
The
key_generatedfield was removed from theRegistrationProfilemodel, so you’ll want to drop the corresponding column in your database if you upgrade. - A mechanism for automatically creating custom user profiles during signup was added; check the documentation ...
Software Update
Published May 28, 2007
Back when I first launched this site (about a year ago now), I mentioned that I’d someday release the blog application I’d written to power it. So far I’ve been held up by two large problems:
- The application itself was written using an SVN checkout of Django from April of last year, back when the codebase that became the Django 0.95 release was still known as “the magic-removal branch”.
- Some features ...
Django template utilities
Published April 12, 2007
In the course of working on a couple personal projects (which will be released eventually), I found myself factoring out a lot of common code and making it more and more generic, until one day I realized I had a whole separate application just waiting to be documented and bundled.
So here it is: template_utils, a collection of generic and useful tags, filters and other tools related to Django templates. Full documentation is on the ...
Reusable Django apps
Published March 27, 2007
A little while back I released a couple of Django apps to the world, and I’ve got a couple more in the pipeline (whenever some more of my copious free time rolls around, I’ll finish them up and get them out the door as well), so I’ve been thinking a bit about best practices for making them as easily reusable as possible. And James Tauber has started up a mailing list for ...
Django snippets
Published February 25, 2007
Just in case you’re not subscribed to the Django mailing lists and/or aren’t at PyCon, today I launched a little side project I’ve been working on: djangosnippets.org, a site for Django users to share useful bits of code.
There are still a few things I’m working out (like feeds), but on the whole I’m pretty happy with it :)
PyCon 2007: Web frameworks panel
Published February 23, 2007
(this was liveblogged during the panel, and the only later edits were to correct a couple of typos and add this notice — the content has not been changed)
Titus, the moderator, has welcomed everyone. No blood shed so far. Probably not taking audience questions.
Introduction of the panelists:
- Spyce: Jonathan Ellis
- CherryPy: Robert Brewer
- TurboGears: Kevin Dangoor
- twisted/Nevow: Duncan McGreggor
- Zope: Jim Fulton
- Django: Adrian Holovaty
Titus pauses: “The most interesting thing about Django ...
PyCon 2007: the prelude
Published February 22, 2007
So, I’m in Dallas, eagerly awaiting the beginning of the non-tutorial bits of PyCon 2007; I got here around 4:15PM (after a lovely flight where I was inadvertently bumped to first-class), and already things are rocking; I just got back from dinner and drinks with some folks who are doing really cool stuff with and to Django. Already got some interesting ideas and things to continue fleshing out, and I’ve got another ...
About model subclassing…
Published February 20, 2007
In Django 0.90 and 0.91 we offered the ability to subcless models, and a nasty hack called replaces_module which would let you tell Django to use a subclass in place of the original model it was inheriting from. The magic-removal changes broke that ability, and we’ve been slowly working toward getting it back (well, actually Malcolm has been rolling the Sysiphean rock up the hill and the rest of us have mostly ...
Python framework design
Published February 19, 2007
Lately I’ve found myself being baited into the same old debate over and over and over again, and I’m getting tired of making the same arguments each time. Usually it begins with someone lamenting how Django is anti-community or too inflexible or generally suffering from a raging case of NIH. From there it progresses into people proclaiming how TurboGears or (more often) Pylons is objectively “better” because of how they’re designed, and ...
OpenID delegation under Django and lighttpd
Published January 8, 2007
There’s been lots and lots and lots of buzz around OpenID in the last couple weeks, which makes me happy because OpenID is a pretty darned cool system. Simon is doing all sorts of cool things, and Sam Ruby wrote up wonderful step-by-step instructions for OpenID delegation, which lets you use your own domain name to sign in with OpenID even if your OpenID is hosted somewhere else.
I sat down tonight and worked ...
My site is smarter than I am
Published November 22, 2006
Looking through my stats today, I found an incoming link from a blog written entirely in Chinese. Now, I don’t read Chinese (I know about half a dozen words of Mandarin, but couldn’t begin to pronounce them with the right tones), so I ran it through Babelfish and found that this person was — apparently — commenting on how “comforting” my site was to him.
This left me somewhat befuddled, until I went back and ...
Django tips: get the most out of generic views
Published November 16, 2006
Recently at work I had the chance to revisit an application I’d written fairly early on in my tenure at World Online; I was still getting used to doing real Django development and to some of the quirks of our development environment, and ended up writing a lot more code than I needed to, so I was happy to be able to take a couple days and rewrite large chunks of the application to ...
Django tips: auto-populated fields
Published November 2, 2006
One of these days I’m going to run out of frequently-asked Django questions to answer. But today isn’t that day, so let’s look at one of the most common questions people ask: how do you set up a model with one or more fields that never show up in add/edit forms, and get automatically populated with some specific value?
A simple example
Let’s say you’re writing a to-do list ...
Django and NIH
Published October 21, 2006
Just so you know, Django is a smug, arrogant framework that doesn’t play nice with others. The developers are smug, arrogant assholes who don’t care what anyone else does or what other tools are available, and who always insist on doing things their way. Django absolutely refuses to support cool “standard” Python tools and libraries unless they were implemented first for Django. Just stay the hell away from it, and teach those goddamned ...
How I got here
Published October 16, 2006
I’m not a formally-trained programmer. I wasn’t a computer science major in college (my degree is in philosophy), and my first job after graduation didn’t involve programming (it was phone-based customer service at a health-insurance company). But here I am, developing software for a living.
I’ve never written a compiler. I’ve never hand-tuned something by dropping in bits of assembly, or even by writing C extensions for an interpreted language ...
Heads up
Published October 11, 2006
If you’re a regular reader of my blog it will be purely remedial reading, but an article I wrote about Django is up at Sitepoint as of this morning.
Go forth and spread the good word.
Django tips: laying out an application
Published September 10, 2006
Continuing the theme of dealing with common questions from the Django mailing lists and IRC channel, today we’ll look at how to organize the various bits of a Django-based project or application.
Projects versus applications
This is really more of a separate (though related) question, but understanding the distinction Django draws between a “project” and an “application” is a big part of good code layout. Roughly speaking, this is what the two terms mean ...
Django tips: documentation and resources
Published September 4, 2006
Django’s official documentation often draws praise for being well above average for an open-source project, but it’s far from being the only source of useful information for a developer using the framework. Also, articles and tutorials specific to Django aren’t the only useful documentation out there; the Python language in general tends to have tons of great resources. So let’s take a look at some resources you might not know about ...
Django tips: user registration
Published September 2, 2006
One of the most common and, generally, most successful models for letting people sign up for user accounts requires the user to go through some form of “activation”; usually it looks something like this:
- User fills out a form with a username, password and email address.
- User gets an email with a “confirmation link” which must be clicked to activate the account.
- User clicks the link and the account becomes active; then they log in ...
Friday fun
Published September 1, 2006
It’s the end of the week and, while I’ve got plenty of things I could write about, I’m sort of tired and burned out. So let’s just look at a couple things that went on this week.
Django performance with foreign keys
There’s been a bit of a to-do the last few days over a supposed performance problem in Django. I have a hard time classifying it as a “bug ...
The best of all worlds
Published August 29, 2006
Every once in a while, someone will complain on the Django users mailing list that they feel our ORM doesn’t offer all the features they need, and it’s true that there are a few exotic and/or advanced things that it can’t handle. Usually the standard people compare to is SQLAlchemy which can be, unfortunately, extremely complex to learn and use. A related — and inaccurate — complaint is that it’s possible to ...
Django tips: using properties on models and managers
Published August 18, 2006
While working on a little side project this week, I ran into a couple of very common use cases that often result in a lot of extra typing:
-
Defining a
BooleanField, or anIntegerFieldorCharFieldwithchoiceswhich will, logically, break up instances of the model into certain groups which need to be accessed often. - Repeatedly wanting to calculate a value based on the values of several fields of a model.
Let’s look ...
Django tips: A simple AJAX example, part 2
Published August 5, 2006
Last time around we looked at how to write a simple view which processes a form and either returns errors or returns success, and then tweaked it slightly so that the same view could handle either a “regular” form submission (in which case it operates normally), or an XMLHttpRequest (in which case it returns JSON).
Today we’ll look at writing the JavaScript side of it; for reference, here’s the live example we’re ...
Django tips: A simple AJAX example, part 1
Published July 31, 2006
One thing that’s come up over and over again in the Django IRC channel and on the mailing lists is the need for good examples of “how to do AJAX with Django”. Now, one of my goals in life at the moment is to try to fill in the gaps in Django’s documentation, so…
Over the next couple of entries we’re going to walk through a very simple form, which will submit ...
Helpers, scaffolding, tradeoffs and other stuff
Published July 17, 2006
In one of the very, very few coherent things I’ve seen him say in comments posted here and elsewhere, one Lucas Carlson brought up the other perceived advantage of JavaScript helpers: they save time:
Sure it is possible to add javascript helper functions to Django, and yes that would speed up initial development times and reduce bugs since Python is usually more terse than JS… even for expert JS programmers.
I’m going to ...
Django tips: Hacking FreeComment
Published July 16, 2006
Django’s bundled comments application (found in django.contrib.comments) is incredibly useful; it gives you a nice, out-of-the-box system for adding comments to any site. But, if you look at it closely, really feels more like two applications:
-
The
Commentmodel and its helpers, which only allows registered user accounts to post comments. -
The
FreeCommentmodel and its helpers, which allows anyone at all to post comments.
The first one — based around Comment — is ...
JavaScript, ORM and “hiding SQL”
Published July 4, 2006
So my little rant on AJAX support in Django apparently touched a nerve with a couple people; that means it’s time to write more about it.
One of the common points people have been raising, in comments and elsewhere, is that I shouldn’t rail against “hiding JavaScript from the developer” when Django’s ORM already hides SQL from the developer; from the perspective of a server-side developer, SQL is just as important, right ...
Django tips: scaling an application
Published July 3, 2006
In today’s ripped-from-the-mailing-list Django tip, we’ll be looking at a common scaling pattern: an application which starts out with one user, then has to gain separate “instances” for each of multiple users. And for bonus goodness, we’ll scale it even further to work on multiple sites simultaneously.
Let’s build a blog
For purposes of this example, let’s say that you’re building a blog with Django; that was the example ...
Django and AJAX
Published July 2, 2006
One hot topic that keeps coming up over and over again on the Django mailing lists and in IRC has to do with when Django will get “AJAX support”. There are two answers to that question; one can be stated with authority, and the other consists entirely of my own unofficial and non-binding opinion. Let’s start with the first:
We’ve already got it, and more is on the way
Doing AJAX with Django ...
Django tips: the difference between ‘blank’ and ‘null’
Published June 28, 2006
New users of Django, even people who have lots of experience writing database-driven applications, often run into a seemingly simple problem: how do you set up a model with “optional” fields that don’t always have to be filled in? Django’s validation system assumes by default that all fields are required, so obviously you have to tell it which fields it’s OK to leave blank.
But therein lies the problem: there are two ...
Django tips: Template context processors
Published June 14, 2006
Last time around we looked at how to write an effective template tag, with the focus on writing a flexible template tag that would make it easy to pull in various types of recent content in any page; I use a tag similar to the one in that entry to pull out the recent entries, links and comments in the footer of every page on this site.
For situations where you want to get content ...
How Django processes a request
Published June 13, 2006
In a comment he left yesterday, Jonathan Snook posed an excellent challenge: document the chain of how Django processes a request, from start to finish, with plenty of detail on the various things being called internally and links to the appropriate documentation.
Simon Willison once wrote such a document, but it was a fairly high-level view and a fair number of things have changed since then, so I’m going to take a stab at ...
Django tips: Write better template tags
Published June 7, 2006
Django‘s template tags are a great way to handle things that don’t always make sense being in a view. If you want to have, say, a list of recently-added content which appears in a sidebar or footer on every page of a site, it’d be crazy to manually change every view to fetch that content and add it to the template context; a template tag is definitely the way to go.
For ...
Django tips: extending the User model
Published June 6, 2006
One of Django‘s great strengths is its built-in user and authentication system; the bundled application django.contrib.auth includes models for users, user groups and permissions and views for logging users in and out and changing and resetting their passwords. This is enough to cover the needs of a huge number of sites and, after the admin, the auth system is probably the most popular bundled application Django ships (or maybe the other way ...
Templating languages redux
Published June 5, 2006
So, I thought I’d explained why templating languages are pretty useful things. Markup is, after all, just about the fastest and simplest way to, well, mark up content for presentation and, when coupled with a small amount of logic, is by far the best way to present the output of a dynamic database-driven web application. I kind of hoped that I wouldn’t have to write more on the topic, because there are so ...
Why templating languages aren’t a bad idea
Published May 24, 2006
Before I get into any meaty details, a quick question. You have a dynamic, database-driven web application and you’re ready to sit down and bang out its HTML output. One part of one page will be an ordered list containing comments left by visitors. You have the following choices for doing this:
Option A:
<ol class="comments"> {% get_comment_list %} {% for comment in comment_list %} <li>{{ comment }}</li> {% endfor %} </ol>
Option B:
item = page.new_content_element('ol') item ...
Django, gzip and WSGI
Published May 21, 2006
One of the many things I like about Django is the range of available middleware you can use to do all sorts of interesting stuff. But one in particular has got me a little bit stumped.
One of the available middleware components for Django allows content to be gzipped for output when the client specifies ‘gzip’ in its Accept-Encoding header; this is handy because it both conserves bandwidth and allows pages to be downloaded more ...