django user variables and login forms
I’ve been working on Pub Gateway this afternoon, and I ended up with a slight problem with django’s authentication module. I’d managed to build a working login form which sent the user back to the home page if they entered a valid username and password. The only other thing I wanted was to change the base template to display a link to the login page for users who hadn’t logged in, and a link to the logout page for users who were logged in. I was using the following template code to decide which link to display:
{% if user.is_authenticated %}
<a href="/accounts/logout/">Logout</a>
{% else %}
<a href="/accounts/login/">Login</a>
{% endif %}
However, the template was only showing the logout link if I was on the login page—on all the other pages it was showing ‘login’ instead, even if I’d already logged in. It turns out that the user variable isn’t passed to the template unless you supply what django calls a ‘context processor’, which is added by default to the login view but not on views which I’d created myself. After a bit of searching through the documentation, I had to modify my views like so:
Original view:
return render_to_response('core/about/index.html')
New view:
return render_to_response('core/about/index.html', context_instance=RequestContext(request))
Possibly obvious to anyone who knows django already, but it took me about half an hour of reading through the documentation to figure this out. It seems a bit clumsy to have to do this on every view, and I’m surprised that there’s no obvious way to say ‘do this on every view unless I tell you not to’, but at least everything is working now.
posted by Paul at 6:25pm on Saturday 24th April 2010 | No Comments »
A pint with the party leader
Everyone has been talking for the last week about how well Clegg did in the recent leaders debate on ITV. The polls have him on anything up to a 14 point lead on Cameron and Brown, and everyone seems to be joining the 101 Facebook groups and buying the t-shirts. Yes, you too can be the proud owner of an ‘I agree with Nick’ or ‘I voted Lib Dem before it was cool’ t-shirt, though I’m tempted to buy a ‘Dave the Chameleon’ one instead.
Why is this? Well, Clegg is probably the first leader of the Lib Dems who people can look at and think ‘yes, I can see him as Prime Minister’. Paddy Ashdown was too early, when the Lib Dems had absolutely no chance of getting elected, and Charles Kennedy was a jolly nice chap but didn’t really have that statesman feel. You don’t see Nick Clegg appearing on Have I Got News for You either, which is just as well because I don’t think he’d be able to shrug off the jokes about the Lib Dems not being a serious party.
The only thing is, I don’t particularly like or dislike Nick Clegg, whereas I did (and still do) like Charles Kennedy. I’d go for a pint with Kennedy, but not with Clegg, Cameron or Brown. If you believe their characitures in the press, Brown would glass someone, Cameron would have a dry white wine poured for him by a butler (although he’s on record as being a fan of real ale) and Clegg would sit at the bar for hours trying to decide what to drink, or whether to even have a drink at all…
posted by Paul at 8:21pm on Wednesday 21st April 2010 | 3 Comments »
Register to vote
Have you registered to vote? There’s still time, it’s a really simple process, and it gives you the power to help decide who will represent your area in Westminster for the next five years. You may also have local elections, depending on your area.
Alas, I haven’t got round to forming the Currybeer Party in time for this election—perhaps next year. ![]()
posted by Paul at 5:30pm on Tuesday 13th April 2010 | No Comments »
Thoughts on frameworks
I’ve been building dynamic web sites for over ten years now, and one of the things which has bugged me enormously is the huge amount of repetitive code I have to write, especially for form handling (including validation) and access control. At least half of my coding time is spent on this sort of work, and the lack of creativity involved leads to boredom, which inevitably leads to dissatisfaction with the work and mistakes. Apparently though, there is this silver bullet called a framework, which will solve this problem for me…
I heard about frameworks a few years ago, when the various web development groups were getting all excited over the memory-eating monstrosity which is Ruby on Rails, under the reasoning of ‘37signals use it, so it must be the best thing since sliced bread!’ I went as far as reviewing a book which was aimed at PHP programmers who wanted to get to grips with Rails, but I wasn’t convinced that it was any good. The initial investment, in terms of the amount of time required to learn the framework and the Model View Controller way of doing things, was also too much of a hurdle. Sure, I might save some time in the long run, but there was no guarantee that this would warrant spending an undefined amount of time getting to grips with the framework’s particular way of doing things.
However, I’ve been playing around with Django this weekend, and I must admit that it has impressed me (this is not a simple achievement, for I am terribly fussy about anything to do with programming, whether it’s a library, framework, IDE or coding style). Like any framework, it has the ability to turn models into database structures and forms, a built-in admin interface and scaffolding which you can gradually replace with your own templates. Its major selling point though is that it is incredibly easy to pick up. I put this down to two reasons:
- It has good documentation, including a self-contained tutorial.
- It has good documentation, including a self-contained tutorial.
I realise that’s actually just one reason, but it’s so important I feel it’s worth stating twice. As a result of the documentation, someone who has never programmed in Python (apart from a one day tutorial more than a year ago) has been able to create a clone of a site written in PHP in about half the time. As a result, I’ll be using Django to rebuild Pub Gateway over the coming weeks, and then moving on to kick start Rogue Students, which I’ve neglected for some time.
posted by Paul at 5:21pm on Sunday 11th April 2010 | No Comments »
Announcing… Pub Gateway!
Pub Gateway is now available for your viewing pleasure.
A few things to note:
- The design is simple, because I’m not an artist and can’t create amazing logos. I’ll probably pay someone to come up with a decent design and logo at a later date.
- There are only a few pubs available as I haven’t written the interface to submit them.
- There are no photos of pubs because I need to buy myself a new digital camera.
- The ‘nearby information’ feature (e.g. ‘railway stations with X miles of the pub) isn’t ready yet – mainly because I found that the metadata I need is not marked up properly on Wikipedia. It will probably take me a few weekends to fix all the 483 pages so that the useful information can be extracted.
However, the main structure of the site is in place (mod_rewrite rules, user authentication, templates to display basic details about a pub etc.) so it’s now a case of gradually building up all the features necessary to make a complete site.
posted by Paul at 1:00am on Tuesday 6th April 2010 | 2 Comments »
Thought for the day
Thought for the day (which probably wouldn’t get onto Radio 4):
- Any sane mail server logs the IP address of any remote server which connects to it and attempts to deliver spam.
- It’s possible to map an IP address to a physical location, with a reasonable degree of accuracy (if you pay for a good database).
- Long range missiles can be launched by computer and targeted to a precise location (unless the targeting system has been programmed by someone who gets latitude and longitude mixed up, or the missiles are American).
Combine these three facts together and you have a simple (albeit rather extreme) method of dealing with spammers.
posted by Paul at 5:05pm on Wednesday 31st March 2010 | No Comments »
UKUUG Spring conference
Three days of this week were taken up by the UKUUG Spring 2010 conference, which I seemed to volunteer to lead the organisation on once again (and for next year as well by the looks of it!). I’ve still not fully recovered, probably as a result of going down with a cold the morning after the last day of the conference, but have nearly finished emailing all the speakers and sponsors to say thank you, and uploading the slides to the conference wiki.
Tuesday involved a tutorial on the SCons build system, by Russel Winder. I struggled a bit with some of the exercises as I’m not a Python programmer (it’s on my ‘things to learn’ list), but I was sufficiently convinced to use SCons for any new projects where I need a build system, as it’s vastly superior in terms of usability to both make and ant. Russel is also an excellent tutor who gets the blend of teaching and humour spot on, which made the day an enjoyable experience.
Wednesday and Thursday were the main conference days, with talks ranging from FreeBSD’s virtual network stacks to a live demo of setting up Ubuntu Enterprise Cloud in 30 minutes (I’m still amazed that nothing went wrong with the multi-projector and network setup!). There were also some lightning talks which involved a brave but ultimately unsuccessful defence of PHP, some Perl madness from Matt S Trout, a rant about multicore processors and a quick introduction to Access Space. Simon also managed a plug for Currybeer, amongst other geek groups in Manchester.
We also had a social programme which started on the Monday night with a few drinks, led into free beer (courtesy of antibodyMX) and curry on the Tuesday, a conference dinner on the Wednesday and more drinks on Thursday evening. This meant that I effectively spent 72 hours running a conference with no rest beyond three lots of 6 hours sleep, for which my immune system promptly punished me for the next three days.
Anyway, that’s Spring 2010 over, now to start planning for 2011…
posted by Paul at 11:12pm on Sunday 28th March 2010 | No Comments »
Debt Relief Bill
Apparently I’m responsible for it not going through! ![]()
posted by Paul at 8:55am on Tuesday 16th March 2010 | 1 Comment »
Pub site progress II
Work on my pub site is coming along faster than I had expected, slowed down only by my need to concentrate on my Open University course. So far I have all the data required to add a handful of pubs, though I still need to buy a new digital camera so that I can go out and take photos of them for uploading.
At the moment I’m writing a simple scraper to pull in the station name, code and latitude/longitude from Wikipedia for all current railway stations in the UK, plus the same data for London Underground and Metrolink stations. Once this is done, and I’ve figured out how to do spatial queries in MySQL (e.g. return all recorded items within a given radius of a specified point), I’ll be able to show the nearest stations to pubs and vice versa. I hope this will be useful, but if not it’s provided me with a refresher of how to parse text and use regular expressions in Perl. The same code will be put to use for creating a database of UK counties and cities, so finding all pubs in Manchester or Lancashire will be a doddle.
I’m aiming to launch the site on 6 April, for no other reason than it’s a close deadline to aim for and I’ll remember because it’s the start of the financial year.
posted by Paul at 10:02pm on Monday 15th March 2010 | 3 Comments »
Computer books clear out
Continuing my general spring clean clear out, I have the following computer books (all in excellent condition) which I no longer need and are free to a good home:
- DHTML Utopia: Modern Web Design Using JavaScript & Dom (SitePoint)
- Professional Search Engine Optimization with PHP (Wrox)
Texinfo (Free Software Foundation)- SEO Warrior (O’Reilly)
- The Art of SEO (O’Reilly)
- Open Source Game Development – Qt Games for KDE, PDAs and Windows (Thomson Delmar Learning)
- Beyond IBM (Penguin)
Beautiful Code (O’Reilly)- C++ GUI Programming with Qt 4 (Prentice Hall)
As with the previous batch of stuff, I’d prefer to give them to local people, but I can probably post one or two of the books in exchange for beer at a future date. Feel free to drop me an email if you want further info about any of the books – I will put links up to their Amazon pages at some point tomorrow.
posted by Paul at 9:47pm on Tuesday 9th March 2010 | 1 Comment »
