on 24 Feb 2012
I’ve spent the last week or two working on a new Django site, and learning a lot at the same time. I thought I’d post a few small little tips on how to do a few things that people still getting into Django may not work out so easily. The documentation is generally very good, but sometimes it’s not clear on which method or combination of methods to use to achieve something.
(Note: I’m stuck back on Django 1.2 so some of these things have changed in 1.3 and dev)
Getting current URL
To get the current URL you need to reference the request object, but you’ll find this isn’t available by default in your templates. To have access to this, be sure to adddjango.core.context_processors.request to your TEMPLATE_CONTEXT_PROCESSORS setting.
You can get the current URL with request.build_absolute_uri
Moderate comments by default
The built-in comments framework is great, and even better when I found it had the capability for moderation. However I found it strange that once enabled, comments weren’t hidden by default. You’re supposed to be able to specify auto_moderate_field and moderate_after settings to make them require moderation after a set time since the post was written, but it had no effect. So the solution, override the moderate() method as below (moderate() in docs)
email_notification = True
enable_field = 'can_comment'
def moderate(self, comment, content_object, request):
I’ll add a few more later and hopefully they’ll help some Django newbies out there.
I’ve not had much time to work on anything with the HTML5 game lately, but I still intend to – right now I’m in the middle of getting NPC movement and map scrolling working well, somehow between all the ideas of loot and inventory screens and baddies, you forget about the less-fun maths part of game programming.
Here’s a screenshot as of a week ago. You can see the random map generation, using Binary Space Partitioning to split into sections (indicated with the shades of grey) and then rooms placed inside each section. The plan is to make the rooms varied shapes rather than just 4 sided boxes. Each room then gets joined together with its sibling and so on until there’s a playable map. More updates soon!