Django community: Community blog posts RSS
This page, updated regularly, aggregates Community blog posts from the Django community.
-
Django navigation bar (active link highlighting)
Every web application needs a navigation bar. Common practice is to indicate to the user where he or she is, and is usually implemented by using a visual aid such as a bold type-face, different color or an icon. I wanted an elegant, generic, extendable solution to "highlight" a link on the navigation bar without hardcoding URLs, using ifequals, or using template block inheritance by specifying a navbar block on each and every template (you'd be surprised, but the above mentioned are recommend often). The solution I came up with is quite simple. No need to hardcode URLs (using urlconf names). Navbar is only specified in the base template (actually a separate template loaded by the base template). By using a simple template tag and the request context processor, "active" will be returned if the "link" should be active. Supports multiple URLs for each link. CSS is used to highlight the active link. You can see the above in action on the TurnKey Hub. On to the code First up, we need to enable the request context processor. settings.py TEMPLATE_CONTEXT_PROCESSORS = ( ... 'django.core.context_processors.request', ) Next, create the template tag navactive. Note: the navigation sitemap I'm using is flat, but you can … -
Django navigation bar (active link highlighting)
Every web application needs a navigation bar. Common practice is to indicate to the user where he or she is, and is usually implemented by using a visual aid such as a bold type-face, different color or an icon. I wanted an elegant, generic, extendable solution to "highlight" a link on the navigation bar without hardcoding URLs, using ifequals, or using template block inheritance by specifying a navbar block on each and every template (you'd be surprised, but the above mentioned are recommend often). The solution I came up with is quite simple. No need to hardcode URLs (using urlconf names). Navbar is only specified in the base template (actually a separate template loaded by the base template). By using a simple template tag and the request context processor, "active" will be returned if the "link" should be active. Supports multiple URLs for each link. CSS is used to highlight the active link. You can see the above in action on the TurnKey Hub. On to the code First up, we need to enable the request context processor. settings.py TEMPLATE_CONTEXT_PROCESSORS = ( ... 'django.core.context_processors.request', ) Next, create the template tag navactive. Note: the navigation sitemap I'm using is flat, but you can … -
Django navigation bar (active link highlighting)
Every web application needs a navigation bar. Common practice is to indicate to the user where he or she is, and is usually implemented by using a visual aid such as a bold type-face, different color or an icon. I wanted an elegant, generic, extendable solution to "highlight" a link on the navigation bar without hardcoding URLs, using ifequals, or using template block inheritance by specifying a navbar block on each and every template (you'd be surprised, but the above mentioned are recommend often). The solution I came up with is quite simple. No need to hardcode URLs (using urlconf names). Navbar is only specified in the base template (actually a separate template loaded by the base template). By using a simple template tag and the request context processor, "active" will be returned if the "link" should be active. Supports multiple URLs for each link. CSS is used to highlight the active link. You can see the above in action on the TurnKey Hub. On to the code First up, we need to enable the request context processor. settings.py TEMPLATE_CONTEXT_PROCESSORS = ( ... 'django.core.context_processors.request', ) Next, create the template tag navactive. Note: the navigation sitemap I'm using is flat, but you can … -
Django 1.1 Testing and Debugging
I borrowed a copy of Django 1.1 Testing and Debugging from a friend today. Spent the bus ride home from the office flipping through the pages, and I’ve got to say – it’s a pretty darned good book! If you’re … Continue reading → -
Investing in Yourself - A review of Django 1.1 Testing and Debugging by Karen M. Tracey
Packt Publishing recently asked me to read and review Django 1.1 Testing and Debugging and I have to admit I really enjoyed reading this book. Often I find myself debating whether or not to purchase a new development book. I'm usually thinking "If I spend $XX.XX on this book, will I really learn anything worth that much?". Especially considering most answers are a few Google searches away. I can happily attest this book is definitely worth the cost. The book starts off with the usual introduction to testing, discussing both Doctests and Unittest, which is obviously required for a book on this subject. However, this book differs greatly from many in that it walks you through testing and debugging your application as you would when building a real application. Many tech books strive to be a great tutorial, but often fall short of the mark and end up just being some verbiage around a rehashing of the available documentation. They end up being more reference than tutorial. Django 1.1 Testing and Debugging however does a great job of walking you through real world scenarios. For example, it covers topics (and in the proper order in my opinion) like: Test coverage … -
Adding Geolocation Support to GeoDjango Admin
GeoDjango is awesome. It’s one of those frameworks that does an amazing amount of heavy lifting for you behind the scenes so you can just focus on your implementation. There was one thing that has been bugging me about it since day one though: the map picker tool used in the administration area doesn’t focus on your current location automatically. It simply gets initialized somewhere around Cameroon, and you have to pan and zoom your way around the world every time you want to add a single point, or polygon, most likely near where you are. Adding geolocation support to this map has been surprisingly easy. Google Chrome, Firefox, and now Safari 5 all support geolocation, through W3C Geolocation Standard. Google Maps API documentation includes a very handy JavaScript snippet that you can use to activate this feature, and it even has fallback support for browsers that have Google Gears installed, which has its own geolocation support. Without further ado, let’s get into the code. First, I switched the GeoDjango admin to use a Google Maps layer instead of the default Open Street Maps one. It’s simply better in terms of visuals and readability, and people are much more used … -
Ubuntu 10.04 (Desktop)
I’ve been using Ubuntu as my distribution of choice for VM’s and server instances, and on a lark I took a swag at installing Ubuntu desktop onto a VM yesterday. I’ve got to say, it’s a pretty usable setup. I … Continue reading → -
Django caché invalidation
Un dels problemes més importants del desenvolupament d'aplicacions web que necessiten suportar una gran quantitat de visités és el de decidir com es farà l'arquitectura de caché i quan i com s'invalida el contingut de la mateixa. Al respecte he trobat dues presentacions realment excel·lents de la Djangocon: Scaling Django Web Applications Static Generator Les dues són molt bones, però especialment us recoman la segona si voleu passar una estona divertida a més d'aprendre com funciona el tema de les cachés. Jared Kuolt broda una presentació plena d'ocurrències, dobles sentits i acudits freaks al mateix temps que aconsegueix introduir-nos en la problemàtica de les cachés. La primera presentació té moltíssima informació, Michel Malone de Pownce explica els problemes que s'han trobat a Pownce i com els han anat solucionant a Django. El problema fonamental de les cachés a Django és el tema de la invalidació. Django oculta bastant el nom de les claus que genera i sense aquestes claus un no pot anar a Memecached per invalidar-les. La sol·lució passa invariablement per generar les nostres pròpies claus de manera repetible i fer un us dels signals de Django per a realitzar la invalidació de les cachés. Això vol dir ser … -
Using virtualenv, pip and django-site-gen to quickly start new Django projects
Last week, after several false starts, I moved all the sites I maintain into virtualenvs, with their own pip requirements files. My reasons for doing so are pretty simple: Experimenting with new/different versions of software is a pain in the ass without isolation A pip requirements file for each site is a very nice thing to behold I symlink 3rd party apps into a custom directory on my PYTHONPATH and it was getting huge Profit There are quite a few great tutorials out there for getting started with these tools. I will only discuss how I got over some of the hurdles involved in using these tools, as well as a tool for automating the creation of "skeleton" django sites. -
What’s the Deal With: Django Template Indentation
I like my source code to look good (if at all possible). Django definitely helps with this but you are essentially in a 0 sum game. You make your template code look good or your output HTML source look good, not both it seems. OK, so in the docs you will first of all come [...] -
update script for unittest2 integration
Django is moving to use unittest2 as part of the default test suite. Unfortunately, it needs to be bundled, which means that it will live in django.utils.unittest rather than in the pythonpath where it expects to be. This means that we need to patch the import statements. I’ve written a little script to download the [...] -
What to do with Locidesktop?
So what to do with locidesktop.com? It's a desktop-like website bookmarking tool – if you haven't seen it, take a quick look at this example desktop. I built Loci Desktop a few months ago and promoted it on a few geek sites. It's been running ever since, with no maintenance from myself, happily serving up start pages to a small number of regular users. There was a buzz when I promoted it, people were largely impressed, some were indifferent, but few ended up using it regularly. So now I'm left with a quandary. I could try and promote it. But to what end? It's not like I need a certain number of visitors to cover the hosting. I'm using the same VPS as I am for my blog, and I designed Locidesktop to be ultra-low bandwidth anyway – so it effectively costs me nothing to run. One option would be to sell the entire site outright, as the domain and technology rights. But there is currently no way of monetizing it and I doubt anyone would be interested as a commercial venture. I could try and license it as a b2b service. A few people have commented that it would … -
What to do with Locidesktop?
So what to do with locidesktop.com? It's a desktop-like website bookmarking tool – if you haven't seen it, take a quick look at this example desktop. I built Loci Desktop a few months ago and promoted it on a few geek sites. It's been running ever since, with no maintenance from myself, happily serving up start pages to a small number of regular users. There was a buzz when I promoted it, people were largely impressed, some were indifferent, but few ended up using it regularly. So now I'm left with a quandary. I could try and promote it. But to what end? It's not like I need a certain number of visitors to cover the hosting. I'm using the same VPS as I am for my blog, and I designed Locidesktop to be ultra-low bandwidth anyway – so it effectively costs me nothing to run. One option would be to sell the entire site outright, as the domain and technology rights. But there is currently no way of monetizing it and I doubt anyone would be interested as a commercial venture. I could try and license it as a b2b service. A few people have commented that it would … -
What to do with Locidesktop?
So what to do with locidesktop.com? It's a desktop-like website bookmarking tool – if you haven't seen it, take a quick look at this example desktop. I built Loci Desktop a few months ago and promoted it on a few geek sites. It's been running ever since, with no maintenance from myself, happily serving up start pages to a small number of regular users. There was a buzz when I promoted it, people were largely impressed, some were indifferent, but few ended up using it regularly. So now I'm left with a quandary. I could try and promote it. But to what end? It's not like I need a certain number of visitors to cover the hosting. I'm using the same VPS as I am for my blog, and I designed Locidesktop to be ultra-low bandwidth anyway – so it effectively costs me nothing to run. One option would be to sell the entire site outright, as the domain and technology rights. But there is currently no way of monetizing it and I doubt anyone would be interested as a commercial venture. I could try and license it as a b2b service. A few people have commented that it would … -
Continuous Integration Testing
At work we recently set up Buildbot to replace an in-house continuous integration tool that never really took off. I’ve used Buildbot before, in a hobby capacity, but using it my day job has really brought home to me how important not only testing is, but testing continuously. Buildbot is pretty simple to explain. Every [...] -
RSS feeds with Django syndication feed framework
Implemented the working of feeds with this site. Now, you can subscribe to live RSS feed from this site, which have been implemented using the Django Syndication Feed Framework 1.2 and later. Currently it supports RSS feeds following the specifications stated at RSS v2. Atom feeds will be added later on in future. To Subscribe to live feeds, Click Here. -
RSS feeds with Django syndication feed framework
Implemented the working of feeds with this site. Now, you can subscribe to live RSS feed from this site, which have been implemented using the Django Syndication Feed Framework 1.2 and later. Currently it supports RSS feeds following the specifications stated at RSS v2. Atom feeds will be added later on in future. To Subscribe to live feeds, Click Here. -
Uploads to Blobstore and GridFS with Django
We've finished our App Engine Blobstore and MongoDB GridFS storage backends for Django. With these backends you can directly use FileField and ModelForm to easily handle file uploads in a portable way. You can see a demo in action on App Engine at http://django-filetransfers.appspot.com/ and also have a look at the demo's source code. The App Engine Blobstore backend is already integrated and pre-configured in djangoappengine. The GridFS backend is now part of django-storages. However, that's only the first half of the solution. In addition to the storage backends we've also created a reusable Django app called django-filetransfers which provides a simple API for upload and download handling in your views. This API is an abstraction over the little details in the different file hosting services. For example, in a traditional Apache/Lighttpd/nginx setup you might want to efficiently serve files via your web server instead of Django by using the "X-Sendfile" extension. App Engine requires that you use a modified upload URL. Asynchronous uploads to Amazon S3 (i.e., the browser sends the file directly to S3 instead of piping it through your Django instance) require that you generate a custom upload URL and use additional POST data to authorize the … -
I can haz a question (or five)?
So... people have noticed that I have this habit at conferences. It's been lampooned and questioned online; I've also been asked about it in person. I ask questions. A lot of questions. More often than not, I ask the first question at every talk I attend. And people think it's weird. However, there is method in my madness. Back in the day, I was a Physics undergrad student who migrated to the Computer Science department to do my Honours degree. To make some use of my undergrad qualifications, my Honours thesis was revolved around a computational physics problem. Although my thesis topic was strictly computer science, it wasn't one of the research foci of the department. Half way through the year, Honours students were required to give a presentation to the faculty describing their work and their progress. The presentation was assessed, and formed a major part of the mid-term mark for the course. A component of the mark for that assessment (I think it was 10%) was allocated to how well the student answered questions. So, I diligently gave my presentation, describing the problem I was addressing, and how I was going to address it. My slides all worked, … -
"La guía definitiva de Django"
Hace unos días recibimos dos ejemplares de La guía definitiva de Django que nos enviaron nuestros amigos de Anaya Multimedia. Se trata del primer libro publicado en español sobre Django y es la traducción al español de "The Definitive Guide to Django"... La guía definitiva de Django" contiene todos los capítulos de "The Definitive Guide to Django" y resulta más amena que ésta para aquellos que no dominan tanto el inglés o prefieren leerla en castellano... -
Getting Started with virtualenv (Isolated Python Environments)
Like South, virtualenv is a helper utility that I put off using for too long. Looking back, it is so easy to get up and running (just like South, see below) that there is no reason for you to hold off like I did. In a nutshell, virtualenv is a tool for creating isolated Python [...] -
Provisioning a new Ubuntu server for Django
I've been a long-time satisfied user of Webfaction, but recently I've had a strong urge to move to VPS hosting so that I can have greater control over the environment. After some research, I went with Rackspace Cloud because of the incredibly cheap low-end options. My site doesn't use a huge amount of bandwidth, so Rackspace looks to be the most feature-packed and still cost-effective option. A friend of mine, Kevin Whitaker, recently posted a great article about getting up and running with Django in a server environment for testing or production. He used Ubuntu, Postgres, Nginx, and FastCGI to make up his stack. I've never set up Nginx before, so his post was a great help in getting Nginx configured. My stack is slightly different, however, since I prefer to use Gunicorn instead of FastCGI and I use supervisord to manage my processes. I also use virtualenv to manage dependencies like Django itself and psycopg2. I'm including my install notes below so that I can provide a complete picture of one way of provisioning a new server from build to deploy. My site is relatively low-traffic, so I'm not going to go into postgres tweaking, connection pooling, caching, etc. … -
Selenium Unit Test Reuse
Yesterday, one of the QA guys at work approached me with a question that turned out to be much more interesting to me than I think he had planned. He's been doing some unit testing using Selenium, exporting his test cases to Python. His question was this: how can I run the same unit tests using multiple browsers and multiple target servers? I'm pretty sure he expected a simple 3-step answer or something like that. Instead, he got my crazy wide-eyed "ohhh... that's something I want to experiment with!" look. I started rambling on about inheritance, dynamic class creation, and nested for loops. His eyes started to look a little worried. He didn't really appreciate the nerdy lingo that much. I told him to pull up a chair and get comfortable. Since I already had some other work I needed to pay attention to, I didn't want to spend too much time trying to figure out a good way to solve his problem. After about 20 minutes of devilish chuckles and frantic rustling through Python documentation, I came up with the following code: from types import ClassType from selenium import selenium import unittest IPS = ['192.168.0.1', '192.168.0.2'] BROWSERS = ['safari', … -
Selenium Unit Test Reuse
Yesterday, one of the QA guys at work approached me with a question that turned out to be much more interesting to me than I think he had planned. He's been doing some unit testing using Selenium, exporting his test cases to Python. His question was this: how can I run the same unit tests using multiple browsers and multiple target servers? I'm pretty sure he expected a simple 3-step answer or something like that. Instead, he got my crazy wide-eyed "ohhh... that's something I want to experiment with!" look. I started rambling on about inheritance, dynamic class creation, and nested for loops. His eyes started to look a little worried. He didn't really appreciate the nerdy lingo that much. I told him to pull up a chair and get comfortable. Since I already had some other work I needed to pay attention to, I didn't want to spend too much time trying to figure out a good way to solve his problem. After about 20 minutes of devilish chuckles and frantic rustling through Python documentation, I came up with the following code: from types import ClassType from selenium import selenium import unittest IPS = ['192.168.0.1', '192.168.0.2'] BROWSERS = ['safari', … -
Beginning Django Development
Oddly, I have had four different people ask me about the best way to begin aDjangoproject or setup their system to make it easy to develop with Django.Since I've now given my response via email so many times, I figure I might as well broadcast it and hopefully this will help others as wellMy assumptions:Using Linux (or a Unix machine)Single developer with root access (quite possibly a laptop)Knowledge of DVCSes (e.g. git / hg / bzr )What we will do is create a private binary directory and anotherPythonmodules. Then take a checkout of the two most important Django projects, and make them work.First: create your user$ sudo adduser newbie $ ssh localhost -l newbie $ whoami newbieThen we want to create some local bin and lib directories. First the binary directory$ mkdir ~/bin# OnUbuntu/Debian, this will automatically be in your PATH the next time you loginAnd now a local directory for our various libraries$ mkdir -p ~/lib/python $ echo "PYTHONPATH=~/lib/python:" >> ~/.bashrc $ echo "export PYTHONPATH" >> ~/.bashrcThis will add that directory to our Python path. If you happen to also use another language you can put things into ~/lib/ruby, ~/lib/perl as appropriate$ mkdir ~/Projects $ cd ~/ProjectsHere is where …