03 Feb 2009
A regular occurance in writing webapps is the user will submit a form, and on the results page you’ll want to
include a link which the user can click to resubmit the form. This lets users bookmark the page or you can add
an extra parameter such as ‘format’ so they can download the results.
While Django forms contain several functions for converting the form to HTML, it doesn’t contain one to
convert a bound form to url arguments.
Fortunately Python’s standard library module urllib
contains a module which converts a dictionary to a
properly formatted url argument string. We simply add a function, as_url_args
, which passes the form’s
cleaned data to this function and we get back a nice string we can add to a link.
Read More...
28 Jan 2009
I recently had cause to create a form with two date time fields which had the default values of midnight seven
days ago, and midnight this morning. Initially I thought this would be easy and created the following form.
from datetime import datetime, date, timedelta
class MyForm(forms.Form):
date_from = forms.DateTimeField(label="From",
initial=(date.today() - timedelta(days=7)))
date_to = forms.DateTimeField(label="To", initial=date.today())
This works fine except that when a process has been running across more than one day the initial values are no
longer correct as they refer to the day the process started. Fortunately it appears that there is an
undocumented feature where the initial value can be a function rather than an absolute value. This function is
called each time the unbound form is displayed, so they are always correct.
Read More...
20 Jan 2009
Chris Petrilli has made a very useful post on placing Django models into separate
files.
The first thing I do when starting a Django project is to delete the standard views.py file and replace it
with a directory. It won’t take you long before you’ve written enough views that a single file becomes huge.
The same is true of models.py. If you have ten or more models then the file can quickly become a thousand line
behemoth. I’d tried to split the file into a directory before, but it never worked, and the error messages
were never helpful.
Read More...
15 Jan 2009
You might be a smart developer. You might be someone who gets things done.
Unless you’re a lone programmer working in your bedroom though, that’s not enough. Collaborating and cooperating with your teammates
is vital to keeping your project moving forwards and to ensure it’s extendable and maintainable.
In this series I’m going to talk about how small changes to your coding style, small changes to your communication and small changes
to your working practices can make a huge difference to those you’re working with. A lot of these tips are common sense, some of them
you’ll already know, but hopefully some of them will make you think and might make your life easier.
If you’re fresh out of university and joining a team of programmers with 30 years experience then you will probably get there and think
“WTF!” about some aspect of their working practices. Most of what you read about programming is of
the “here’s a shiny new toy” variety. If you immediately started using everything you read about not only would you probably go insane
but you’d also annoy your coworkers immensely.
Read More...
14 Jan 2009
For the past day and a half I have been building a website in Django rather than the C++ that is the bulk of
my day job.
It’s so easy, it makes me sad that I have to go back to C++ this afternoon.
I’ve also discovered that aggregation, the main feature I want from django, will be committed to trunk
tomorrow. Hurrah!
Read More...