Django community: Community blog posts RSS
This page, updated regularly, aggregates Community blog posts from the Django community.
-
PlushCMS - simple CMS system
Quick introduction to PlushCMS - Polish simple CMS system written in Python and Django -
Django Form Snippets
Django forms are very convenient tools for Django development, but sometimes I find myself wrangling with them to get them to do what I want them to do. I’ve compiled a few simple form examples that I find useful for various form-related tasks.Say you have a model that ... -
Updated Django-powered website
Two years ago, we launched chicagodjango.com as a place where we could unapologetically geek out about Django and Python. We didn't want to be constrained to a small tech section on our main company site (aka the Mother Ship) imagescape.com.It was campy, it was fun and ... -
Chicago Djangonauts, reanimated
Having scheduled the first meeting of the reanimated Chicago Djangonauts, I didn't know what to expect. There were a number of RSVPs on Facebook and a couple through the chicagodjango.com site, but you never know.We ordered three cases of beer from our favorite brewer and 13 pizzas ... -
PyCon, Django in Chicago and general musings
For the fourth year in a row, we have just completed our sponsorship of PyCon. Each time I return to the office after the conference ends, I am filled with a particular energy. This year that energy is more pronounced. In many ways, our day-to-day work is done in relative ... -
Security for Mobile Applications
Imaginary Landscape has been putting significant effort into developing usable and secure mobile websites. Understanding the context of a mobile user is the first step in developing security protocols to protect mobile access and information. A new blog posting on the Imaginary Landscape main website describes how we approach mobile ... -
Mobile Web Development with Django
If you've ever looked into creating your first Django project targeted at mobile devices, you were probably quick to realize that there is no be all, end all solution. Mobile development decisions have to be made with regards to handheld device detection, redirects, how to deal with desktop vs ... -
Magic Links
The first time I used the new link attachment tool found on sites like Facebook, I thought I had experienced something magical. After pasting an external webpage URL into my wall-post form, an image, page title, and page description of the linked webpage appeared beneath my post as though out ... -
Crazy about DjangoCMS
The more we dig into DjangoCMS, the more we like it. Already, we have adopted it for a number of client sites as well as internal sites. We are extending it to handle more complex presentations and it is working great.Its simple interface belies its sophistication, when configured well ... -
Our Django Server Setup: How and Why
One of the most important decisions you make in the process of building a new Django application is what software stack you use to serve it to the world. You're not lacking for options: people run Django on Apache, lighty, nginx, and Cherokee. You also need to decide how ... -
Top 5 Favorite Django Modules
Of all the Django modules that are out there, several stand out as ones that I keep coming back to. These are some of my favorites: 1) Django Debug Toolbar - http://robhudson.github.com/django-debug-toolbar/ Django Debug Toolbar is an extremely useful Django application for debugging a site from “Django ... -
Django Permission TemplateTag
In a previous post, I wrote about a way to keep track of user permissions on a model instance. For example, I suggested that each model have a permissions subclass that could be instantiated with a user instance passed as a constructor argument. Methods on that permissions class could then be called to determine if that user has permission to perform various actions. I also suggested that the threadlocals module could then be used to pass in the user instance to the permissions object in the Django template. However, from various readings, I get the impression that threadlocals may not be the best thing for passing arguments in a template function. Therefore, I decided to use a more traditional route of creating a template tag to do something similar. I created a template tag that lets you surround a block of HTML code to hide or show the contents based on the return value of the permission function. The tag below basically says, "if the logged in user has 'can_edit_group' permission on the given 'group' object instance, then display the Edit link". Reference the original post for details.In the Django template{% load permission_tags %}{% permission request.user can_edit_group on group … -
Like QuerySets, but crazier
I have a crazy QuerySet idea. Imagine we have this: class Task(models.Model): foo = models.CharField() class TaskStatus(models.Model): task = models.ForeignKey(Task) user = models.ForeignKey('auth.User') status = models.CharField(default='not-done') class Meta: unique_together = ('task', 'user') Now imagine we're listing Task.objects.all() on a page. We need to show the user's status for each task, so we do something like this: b_list = []for a in A.objects.all(): b_list.append(a.b_set.get_or_create(user=request.user)) That sucks because it's doing an extra db query for each object. We can work around that problem, but this also sucks because we're sort of needlessly creating new records when we don't need them. If the TaskStatus is being created with a default status, and we know that's what it will be because we're creating it, couldn't we just assume that status if there's no TaskStatus object and skip the creation? Yes, we could probably do that too. But now our code's going to look a little messier. I won't both writing it out, but you can imagine, right? Here's my crazy idea: Write a manager method that returns a custom QuerySet subclass that will do something like this internally: def get_for_user(self, user); tasks = Task.objects.all() task_status = dict([(ts.pk, ts) for ts in self.get_query_set().filter(user=user)]) for task in … -
Quick update on django-relationships
With the 0.3.0 release of django-relationships, I've made a couple backwards-incompatible changes which I thought I'd mention. -
Benchmarking Celery
Before you even go there, I’ll preface this with YMMV. This little post is to document a benchmark that I did for an internal use case, in the hopes that it’ll be helpful for others. As a benchmark, I wasn’t … Continue reading → -
F() objects and QuerySet.update() support in djangoappengine
As some of you already noticed, we added supported for QuerySet.update() and F() objects as part of some features we currently need for our startup pentotype. Finally, it's possible to use simple transactions on App Engine via Django-nonrel :) Let's see how it works. Django's beauty In order to show what we gain by using Django's QuerySet.update() method and F() objects let me illustrate the code differences between the old djangoappengine version and the new one using a simple transaction which increments the value of a counter by a specific amount. In both cases we have the following model definition: # models.py from django.db import models class Accumulator(models.Model): counter = models.IntegerField() name = models.CharField(max_length=500) The old version looks like this: from google.appengine.ext import db def increment_counter(pk, amount): obj = Accumulator.objects.get(pk=pk) obj.counter += amount obj.save() db.run_in_transaction(increment_counter, 1, 5) whereas the new one looks like this: from django.db.models import F Accumulator.objects.filter(pk=1).update(counter=F('counter') + 5) Obviously, it's more elegant to update a counter using QuerySet.update() and F() objects because it's much clearer and we have to write less code, thus resulting in more productivity. Additionally, it's possible to do more complicated transactions, for example, updating multiple entities using Django's elegant syntax: Accumulator.objects.filter(name__startswith='simple').update(counter=F('counter') + 1) … -
New kung-fu in django-uni-form 0.8.0
This won’t be a new official release announcement as Daniel already did the honors. I’m now the co-lead developer of django-uni-form and in this post I will try to go over some examples that teach you new kung-fu techniques. But before doing so, let me emphasize how cool this project is and how many new nice features are packed in the new version 0.8.0. If you like DRY principles and don’t think your Django forms are DRY enough using built-in filters, you should check it! if you haven’t yet. To explain some of the new features, let’s use a real form and a real example, so it’s easier to follow how django-uni-form works and what has been enhanced. The following form will be used from now onwards to exemplify the new features in django-uni-form. I’m using different types of form fields to demonstrate how django-uni-form can work with any built-in widget or form field or any other custom field that you have defined. That is, django-uni-form plugs well with other form apps. from django import forms class ArticleFeedbackForm(forms.Form): like_example = forms.TypedChoiceField( label = _("¿Do you like this article?"), choices = ((1, "Yes"), (0, "No")), coerce = lambda x: bool(int(x)), widget … -
New kung-fu in django-uni-form 0.8.0
This won’t be a new official release announcement as Daniel already did the honors. I’m now the co-lead developer of django-uni-form and in this post I will try to go over some examples that teach you new kung-fu techniques. But before doing so, let me emphasize how cool this project is and how many new nice features are packed in the new version 0.8.0. If you like DRY principles and don’t think your Django forms are DRY enough using built-in filters, you should check it! if you haven’t yet. To explain some of the new features, let’s use a real form and a real example, so it’s easier to follow how django-uni-form works and what has been enhanced. The following form will be used from now onwards to exemplify the new features in django-uni-form. I’m using different types of form fields to demonstrate how django-uni-form can work with any built-in widget or form field or any other custom field that you have defined. That is, django-uni-form plugs well with other form apps. from django import forms class ArticleFeedbackForm(forms.Form): like_example = forms.TypedChoiceField( label = _("¿Do you like this article?"), choices = ((1, "Yes"), (0, "No")), coerce = lambda x: bool(int(x)), widget … -
Shiv The Third - Return of Tutorial
Continuing from my previous posts on Shiv, Shiv - A wrapper over Django and Shiv - Part Deux, I will now try to explain about various components involved in Shiv. In this post we will talk about Media and Element. In later parts, we will explore Tabs, Boxes, Widgets, Pages, and Caching. An app using Shiv should have the following files 1. page.py: This will contain the definition for all Page container classes 2. box.py: This will contain the definition for all Box container classes 3. widget.py: This will contain the definition for all Widget container classes 4. tab.py: This will contain the definition for all Tab container classes 5. element.py: This will contain the definition for all Element container classes 6. media.py: This will contain the definition for all Media classes Media Media classes define the various media used by all the containers of the Project. Shiv uses a naming convention for media classes. The media class of a container class named “ABC” should be “ABCMedia” and it must be derived from shiv.media.Media. Media class has following class variables a. template: This is the path of the template file. b. css: A list of css file paths which this … -
Extending templates from a specific Django app
A while ago (well, actually a long time ago since I had this blog post laying around as a draft, until I stumbled across a stack overflow question that this post would answer) I wanted to customize Django's admin app, by adding a link to a statistics page that I had built, on the index.html page of the admin app. I could have copied the index.html from the templates directory in the admin app, to my project's templates directory and added my link, but this would have given me a maintenance task to keep Django's admin templates up to date in future versions. So what I really wanted to do was to define my own admin/index.html file in the project's templates directory, and then in my index template extend the index template from the admin app. However this isn't supported by Django be default. When I searched the web I found all sort of solutions to the problem, some involving symlinking the admin's index.html to another path and then extending the symlink, but that wasn't an option for me since I develop on both Windows and Unix, and besides, it didn't feel like a very clean solution. Finally I found … -
P.J. Eby on PyPI
P.J. Eby: The cardinal rule of successful groupware is that those who put information into the system must receive a proportional benefit for their efforts, or those who want to get information out of the system will not see anything coming out. -
Custom 404 Not Found page with Django CMS…
… or how to make user editable 404 page that stays in the pages tree of the CMS Basics: Yes you need it! You need 404 page cause you never know what may happen to a link: bad link paste, obsolete or deleted article, someone just playing with your URLs etc. It is better for [...] -
Shiv - Part Deux
After debating whether to release early or release well, I have settled for the former. I hope that once the code is out in the open and I get ridiculed for the lousy code quality, it will motivate me to find time more aggressively to correct and refine the framework. I will try to find some time to continue my little tutorial of Shiv with sample applications in the next installment. In the meantime, here is the link to the git repository. Have a look and feel free to ask me any question that you might have. https://github.com/lzdev/Shiv Part 1 | Part 3 -
Measure your code
I found a cool tool today to measure code: metrics. It measures SLOC, comments and cyclomatic complexity. It’s easy to install: pip install pygments metrics Run this in your project’s root: metrics -v `find . -type f \( -iname "*.css" -or -iname "*.py" -or -iname "*.js" -or -iname "*.html" -or -iname "*.txt" \) \! -path "*/migrations/*.py" -print` I have a django project so I added \! -path "*/migrations/*.py" to skip any files that are in a migrations dir (I’d skip the automatically generated south migrations). You probably bundle other libraries or apps in your source tree (eg: jquery or that nice django app the author didn’t bother to make a setup.py script for) so you want to measure only some specific paths. Eg, to collect stats only for files in src/foobar, lib/tools and src/otherbar: metrics -v `find src/foobar lib/tools src/otherbar -type f \( -iname "*.css" -or -iname "*.py" -or -iname "*.js" -or -iname "*.html" -or -iname "*.txt" \) \! -path "*/migrations/*.py" -print` If you work on multiple projects you can make a script or alias for this: metrics -v `find \`cat METRICS\` -type f \( -iname "*.css" -or -iname "*.py" -or -iname "*.js" -or -iname "*.html" -or -iname "*.txt" \) … -
Measure your code
I found a cool tool today to measure code: metrics. It measures SLOC, comments and cyclomatic complexity. It's easy to install: pip install pygments metrics Run this in your project's root: metrics -v `find . -type f \( -iname "*.css" -or -iname "*.py" -or -iname "*.js" -or -iname "*.html" -or -iname "*.txt" \) \! -path "*/migrations/*.py" -print` I have a django project so I added \! -path "*/migrations/*.py" to skip any files that are in a migrations dir (I'd skip the automatically generated south migrations). You probably bundle other libraries or apps in your source tree (eg: jquery or that nice django app the author didn't bother to make a setup.py script for) so you want to measure only some specific paths. Eg, to collect stats only for files in src/foobar, lib/tools and src/otherbar: metrics -v `find src/foobar lib/tools src/otherbar -type f \( -iname "*.css" -or -iname "*.py" -or -iname "*.js" -or -iname "*.html" -or -iname "*.txt" \) \! -path "*/migrations/*.py" -print` If you work on multiple projects you can make a script or alias for this: metrics -v `find \`cat METRICS\` -type f \( -iname "*.css" -or -iname "*.py" -or -iname "*.js" -or -iname "*.html" -or -iname "*.txt" \) …