Entries published in November 2007
Where is Django’s blog application?
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 to recommend one of them as “the” Django blogging app (unless the person doing the recommending …
Keeping up with the Django
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 pop up on the mailing lists, or on IRC, after doing an
svn up of their Django …
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 every last bit of performance out of Django — and your Django-based applications — though, so …
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 land before Django 1.0 (the last sprint was an amazing leap forward, and it’d be …
Newforms, part 3
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_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 forms work
We’ve already taken a look at how to introspect Django models using Django’s own internal APIs, and if …
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
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 that
User; at the time it seemed like a good way to handle …
Newforms, part 2
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, building it up step-by-step.
The specific example I’ll be using here is a user-registration form; about a year ago I …
Newforms, part 1
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 it’s still quite new (and parts of it are still being developed), it’s still not fully documented, and so can be …
Auth tips and tricks
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 combination of the
User model and the built-in login/logout views and middleware will take care of your needs just …
Initial data and install-time code
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 it still seems to cause a lot of questions. So today let’s run down the …
Let’s talk about frameworks (again)
My streak ends
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 putting together four entries and post-dating them to show up at the right times, but things …
Instant web sites
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 them are extremely powerful and make it almost trivial to accomplish otherwise-complex tasks.
So today let’s look at two of …
More on admin documentation
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: a quick recap
In all Python programs, not just Django applications, you’re encouraged to provide documentation embedded directly in …
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 method of template variable resolution.
As I wrote yesterday, Django’s Unicode merge means that anything which talks to external web-based …
Unicode in the real world
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 things you’ll need to be careful about; one in particular is how to handle external APIs …
Living without projects
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 — this is a pretty good way to work, because it means that
django-admin.py and …
Moving into production
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 I miss out on something you’ve found useful, feel free to mention it in a comment. …
URLConf tips and tricks
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 people it seems to be one of the trickier parts of Django. So let’s look …
“Server startup” and its discontents
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 wanting to do at “server start”, what that means (and, just as importantly, what it doesn’t mean) …
Working with models, part 2
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 display and edit them.
- Shortcuts in both the old and new forms systems exist for generating forms automatically from a model …
Working with models, part 1
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 to do the same sorts of things, and so knowing how Django does it — and …
Handle choices the right way
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 as those for a US state field, which are — in Django’s implementation — simply two-letter postal …
Django tips: template loading and rendering
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 Django tip every day during the month of November. Kicking off the series, I’d like to focus …