Django community: Community blog posts RSS
This page, updated regularly, aggregates Community blog posts from the Django community.
-
Питонячьи библиотеки для CouchDB
По большому счету кроме urllib2 больше ничего и не надо. Но если хочется каких-то более удобных оберток над CouchDB REST API то есть из чего выбрать. Мне на глаза попадались три азличные реализации и в начале разработки проекта пришлось потратить время на то чтобы выбрать подходящую. Сразу оговорюсь, что все они очень похожи и делают одинаковые вещи - оборачивают в объекты основные сущности, которые предоставляет API -- сервер, база, документ и вьюха. Все совместимы с последним релизом самого CouchDB -- 0.10. Первая из библиотек -- couchdb-python. Самая старая из появившихся. Можно сказать референсная реализаци. Имеет все полезные абстракции и даже чуть боьлше. Это "больше" заключается в возможности задать схему документа в декларативном Django-like стиле и реализация питонячьего view-сервера. Первое нужно чтобы как-то структурировать документы и работу с ними, а второе для того чтобы использовать python как язык для map/reduce обработчиков вместо штатного JavaScript. Умеет работать как с simplejson так и с быстрой cjson библиотекой. Так же хочет чтобы балы httplib2. Следующий кандидат это couchdbkit. Умеет практически всё тоже самое, но имеет чуть больше хелперов и экстеншенов с адаптерами к популярными библиотекам вроде Django. Тянет за собой в зависимостях restkit (как видно из названия -- REST-клиент библиотека от того же автора) … -
Facebook Page Syncronisation
This is going to be a rather technical post, coupled with a smattering of rants about Facebook so those of you uninterested in such things might just wanna skip this one. As part of my work on my new company, I'm building a syncroniser for status updates between Twitter, Facebook, and our site. Eventually, it'll probably include additional services like Flickr, but for now, I'm just focusing on these two external systems. A Special Case Reading this far, you might think that this isn't really all that difficult for either Twitter or Facebook. After all, both have rather well-documented and heavily used APIs for pushing and pulling data to and from a user's stream, so why bother writing about it? Well for those with my special requirements, I found that Facebook has constructed a tiny, private hell, one in which I was trapped for four days over the Christmas break. In an effort to save others from this pain, I'm posting my experiences here. If you have questions regarding this setup, or feel that I've missed something, feel free to comment here and I'll see what I can do for you. So, lets start with my special requirements. The first … -
DjangoSki change of hotel
Over the Christmas holidays the DjangoSki changed location from one hotel in Whistler to another. This was the reason for the registration and hotel information going offline over the Christmas holidays. We've got a new hotel sorted out and the conference will be at the new location, a few minutes down the road. So we went for plan B, one of the other hotels. It's a good hotel with other benefits for example: they don't mind external food coming in, a kitchenette in the room, washer and dryer etc. In fact this hotel is directly on the Creekside chair. You can go directly from the conference room, walk to the hot tub, past the barbeque to the chair lift (or some combination of that). The reason? During the process of sorting out the contract with the original hotel the contract changed quite dramatically against us. We proceeded hoping that the pre-registration numbers would solve that problem. As it turned out it wasn't enough and the amount of money that we were putting up for the conference was so large to be a really, really big concern for us. A conference is a risk, but we are a small company and … -
Eclipse, PyDev, Django and virtualenv
How I set up Eclipse to work with an existing project using virtualenv -
Response time optimisation with Varnish
This blog post shows you how to optimize the tools chain on your server to improve its performance by an order of magnetude with out changing a single line in your django project. In order to do so I will use again django-cms [1] as guinea pig because there is a fair amount for processing to display a page but it is still easy to install. Note: django-cms example has the cache middleware activated by default.Then I will run ab testing on a particular page and compare the results. These tests are being performed on my laptop hp dv6-1030. The important information is not the figures but by them self but rather the variation of the response time.Before starting my test I have moved django-cms to be mounted under "/". In order to do this you will need to change the configuration into the file called example_uwsgi.py.import osimport django.core.handlers.wsgi# Set the django settings and define the wsgi appos.environ['DJANGO_SETTINGS_MODULE'] = 'example.settings'application = django.core.handlers.wsgi.WSGIHandler()# Mount the application to the urlapplications = {'/':application, }Then you need to change the rule behavior in cherokee admin to reflect this change. Cheorkee admin makes this task a breeze.Before diving head first into the the meat of this article here it is a diagram of the architecture … -
flup vs uWSGI with cherokee
Cherokee is one of this many web servers that supports both deployment strategies. Since I have recently blogs about uwsgi Several people asked me how do they compare performance wise. Up to now my answer was I don't know and I don't really care because this is not the most critical aspect in my opinion.However since this question keep comming I have decided to give it a more accurate answer. This blog post takes you on how to setup cherokee with both alternatives and compare their performance. The guinea pig application I have chosen to do this comparison is django-cms because I think it represents fairly well the overhead introduced by the dynamic generation of the page.Note : in this example django-cms will not be mounted under "/" this will cause all sort of issues if you try to do this but none of them impact the particular page that we try to access.One of the nice Cherokee's feature is its documentation, here it is the 2 pages related to this blog post :* flup [1]* uswgi [2]In this blog post I will assume that django-cms is installed, its example project properly configured and that you have added a page to test test against it. I will aslo assume … -
Add bpython to django's shell management command
Like many of us I am spending a lot of time in the REPL loop to develop django code and this bug in readline cost me an extra backspace each time I hit "tab" in ipython.This blog post capture my attention so I have decided to give bpython a try and so far I have been impressed by what I have seen. Here it is a management command that adds the support for bpython to django's shell management command.import osfrom django.core.management.base import NoArgsCommandfrom optparse import make_optionfrom IPython.Shell import IPShelldef start_plain_shell(): import code # Set up a dictionary to serve as the environment for the shell, so # that tab completion works on objects that are imported at runtime. # See ticket 5082. imported_objects = {} try: # Try activating rlcompleter, because it's handy. import readline except ImportError: pass else: # We don't have to wrap the following import in a 'try', because # we already know 'readline' was imported successfully. import rlcompleter readline.set_completer(rlcompleter.Completer(imported_objects).complete) readline.parse_and_bind("tab:complete") # We want to honor both $PYTHONSTARTUP and .pythonrc.py, so follow system # conventions and get $PYTHONSTARTUP first then import user. if not use_plain: pythonrc = os.environ.get("PYTHONSTARTUP") if pythonrc and os.path.isfile(pythonrc): try: execfile(pythonrc) except NameError: pass # This will import … -
django-rest or Reviving django-rest-interface
I have taken it upon myself (which is probably more than I can handle!) to revive the django-rest-interface project. The new project is called simply django-rest and you can find it on Google code here: http://code.google.com/p/django-rest/ Currently the project is a shameful clone of django-rest-interface, which unfortunately seems to have been inactive for some time. I even ripped the Wiki pages! If you never heard what django-rest-interface is, than I recommend you check it out. It provides an easy way of creating RESTful layer on top of your models, treating them as resource, but also allows you to define a non-model resources. Why clone? There are a few reasons why I chose to copy the project instead of contributing to existing one: can't contribute :-) author is non-responsive django-rest-interface project was inactive for the last year it was hosted on subversion, whereas I am using Mercurial, which allows for much easier cloning and contributions It's fun! I like REST architecture style and I think this project is worthwile working on. How to contribute It would be great to know if anyone else if interested in contributing. Currently you can do it in a few ways: Try it out! Just by trying it you … -
Vim taglist and Django
Inspired by the graphical cheat sheet here, I've recently moved over to Vim as my main development environment. After installing a whole range of plugins, I found that one of them, taglist, no longer worked with my Django code. The reason was that something was changing the filetype of Django modules to 'python.django', and taglist - unlike most other plugins - was trying to match against the whole filetype, rather than just a part of it. My solution is to hack taglist so that it does a partial match on the filetype. In the Tlist_Get_Buffer_Filetype function (line 984), change let buf_ft = getbufvar(a:bnum, '&filetype') to let buf_ft = split(getbufvar(a:bnum, '&filetype'), '\.')[0] -
Vim taglist and Django
Inspired by the graphical cheat sheet here, I've recently moved over to Vim as my main development environment. After installing a whole range of plugins, I found that one of them, taglist, no longer worked with my Django code. The reason was that something was changing the filetype of Django modules to 'python.django', and taglist - unlike most other plugins - was trying to match against the whole filetype, rather than just a part of it. My solution is to hack taglist so that it does a partial match on the filetype. In the Tlist_Get_Buffer_Filetype function (line 984), change let buf_ft = getbufvar(a:bnum, '&filetype') to let buf_ft = split(getbufvar(a:bnum, '&filetype'), '\.')[0] -
Showing queries in Haystack
At work we've been using Haystack to manage our site search, with a Solr backend. As usual, we're customising things quite a lot - using faceted queries and weighted indexes, and bypassing the built-in search forms - so I wanted to be sure, in line with my general obsession with query efficiency, that we weren't generating multiple Solr queries for every search. Haystack does log queries for every request internally, but as far as I can tell there's no way of getting to that information without writing some custom code to import and expose the relevant variable. So I've written a (very basic) panel for the Django debug toolbar which does just that. Just put this somewhere on your pythonpath or in your project, and add it to the DEBUG_TOOLBAR_PANELS list in settings.py. -
Showing queries in Haystack
At work we've been using Haystack to manage our site search, with a Solr backend. As usual, we're customising things quite a lot - using faceted queries and weighted indexes, and bypassing the built-in search forms - so I wanted to be sure, in line with my general obsession with query efficiency, that we weren't generating multiple Solr queries for every search. Haystack does log queries for every request internally, but as far as I can tell there's no way of getting to that information without writing some custom code to import and expose the relevant variable. So I've written a (very basic) panel for the Django debug toolbar which does just that. Just put this somewhere on your pythonpath or in your project, and add it to the DEBUG_TOOLBAR_PANELS list in settings.py. -
integrated project management for development
Ironically, the choices for an in-house integrated project management toolchain are, to my mind, significantly more limited than the straight up open-source market. I have been looking and watching several projects over the past months for something that I could … Continue reading → -
New localization system already in trunk
Just few hours ago, Django's new localization system has been commited to trunk.As some of you know, I did most of the work as my Google Summer of Code project, this year. Of course, together with Jannis Leidel, who also did the final steps, including the commit.Summarizing, with this change Django will format all displayed data, according to user's current locale. For example, the calendar will display Sunday as the first day for users in the States, but Monday for users from most European countries. Also it'll format numbers and dates.You can check the slides I presented at DjangoCon.http://docs.google.com/present/view?id=dfbzs3ks_16d26xjbd9Note that the setting is no longer USE_FORMAT_I18N (as in the slides), but USE_L10N.You can also check the commit at:http://code.djangoproject.com/changeset/11964 -
Django Desktop
I implemented a theme system for locidesktop.com and thought it only fitting that the first theme I made was one for Django. I present you with the Django themed Loci Desktop! Here it is, in embedded form – although you really need to click the above link to fully appreciate it. -
Django Desktop
I implemented a theme system for locidesktop.com and thought it only fitting that the first theme I made was one for Django. I present you with the Django themed Loci Desktop! Here it is, in embedded form – although you really need to click the above link to fully appreciate it. -
Django Desktop
I implemented a theme system for locidesktop.com and thought it only fitting that the first theme I made was one for Django. I present you with the Django themed Loci Desktop! Here it is, in embedded form – although you really need to click the above link to fully appreciate it. -
Django patterns: memoizing
One of the things I wanted to do with this blog was to cover some of the design patterns I've discovered/come across/stolen over the years I've been working with Django. So this is the first in what I hope will be a long-running series on Django patterns. Memoizing is the process by which a complicated or expensive function is replaced by a simpler one that returns the previously calculated value. This is a very useful thing to do in a complicated model, especially in cases where methods like get_absolute_url are calculated via a series of lookups on related models. Frequently I've found myself calling one of these methods on the same object several times within a view or template, leading to a huge amount of unnecessary database calls. It's very easy to do this manually - the method simply needs to check whether the cached value already exists, if not calculate it and store it somewhere, then return the cached value: def get_expensive_calculation(self): if not hasattr(self, '_expensive_calculation'): self._expensive_calculation = do_expensive_calculation() return self._expensive_calculation Here the cache lives within the instance itself. For the way I use it, this is useful: instances are created and destroyed within a single request/response cycle, so … -
Django patterns: memoizing
One of the things I wanted to do with this blog was to cover some of the design patterns I've discovered/come across/stolen over the years I've been working with Django. So this is the first in what I hope will be a long-running series on Django patterns. Memoizing is the process by which a complicated or expensive function is replaced by a simpler one that returns the previously calculated value. This is a very useful thing to do in a complicated model, especially in cases where methods like get_absolute_url are calculated via a series of lookups on related models. Frequently I've found myself calling one of these methods on the same object several times within a view or template, leading to a huge amount of unnecessary database calls. It's very easy to do this manually - the method simply needs to check whether the cached value already exists, if not calculate it and store it somewhere, then return the cached value: def get_expensive_calculation(self): if not hasattr(self, '_expensive_calculation'): self._expensive_calculation = do_expensive_calculation() return self._expensive_calculation Here the cache lives within the instance itself. For the way I use it, this is useful: instances are created and destroyed within a single request/response cycle, so … -
Documentation for django-lastfm with django-sphinxdoc
Yesterday I finished a first version of django-sphinxdoc that integrates Sphinx documentation into a Django powered website. Its based on Django’s documentation app, but can manage the documenation for more then one app. I’ll post more on this later. What’s more important is, that I have put the documentation for django-lastfm online with it. :-) -
Documentation for django-lastfm with django-sphinxdoc
Yesterday I finished a first version of django-sphinxdoc that integrates Sphinx documentation into a Django powered website. Its based on Django’s documentation app, but can manage the documenation for more then one app. I’ll post more on this later. What’s more important is, that I have put the documentation for django-lastfm online with it. :-) -
Django-powered Snow
During the planning of our Christmas card at work this year, a mad idea came up. Do we ignore mad ideas? No, we tackle them head-on.The idea was to build an internet-controlled snow machine - you'd hit the button on the website, and watch a member of the Torchbox team get pelted with snow. When we first came up with the idea, we dismissed it as being "too complex", but after a while, we came around. Cue three days of frantic development and phoning round to get the parts. I'll be posting a full build article, with all our source code, once we're done. The brief summary is that we have a Django app which handles rate-limiting of snow, and tracking who has clicked the button, which then communicates with our snow machine using Artnet and DMX. Ustream is used to stream the video back to the internet. Still, I imagine you want to see it in action, so head over to snow.torchbox.com, and have a go. It's only online 10 - 5 UK time, and only until Tuesday (we can't fill our office up with paper snow forever), but it's still very good fun, even if you don't know … -
Test your django-piston API (with auth)
I have to build the API for one of my web service. It is Django and django-piston powered application and it works well. Okay. I chose the TDD technique. So my problem was: how do I test the API parts which need authentication (basic HTTP, not OAuth for now)? The built-in test client of Django doesn't seem to have such a feature. So, here is my small workaround: you have to generate the HTTP_AUTHORIZATION field of your HTTP request. I wrote a small base test class for tests which need authentication: You just have to replace username and password, and write your own test suite: It should be OK. Have fun! -
Nicer dynamic forms in django
I used to make dynamic forms for Django in very bad way, I'm happy to admit that now as I've improved my process. Basically the solution is to use type() as I'm sure many of you know. If your doing that already there isn't much for you here. If your messing around with 'self.fields["name"]' in your forms then read on. Lets take a simple use case; a quiz system. You can think of it like who wants to be a millionaire. We will have a Question with four possible answers. So two models... from django.db import models class Question(models.Model): test = models.CharField(max_length=128) class Answer(models.Model): question = models.ForeignKey(Question) test = models.CharField(max_length=20) is_correct = models.BooleanField(default=False) Basically we want a form that stores both the question and the answers and checks the answer is a valid choice. You could do it this way. from django import forms class QuizForm(forms.Form): def __init__(self,question, *args, **kwargs): super(QuizForm, self).__init__(*args, **kwargs) self.fields['question'] = forms.IntegerField(widget= \ forms.HiddenInput, initial=question.id) self.fields['answers'] = forms.ModelChoiceField(queryset= \ question.answers_set) Now in this example that's actually not too bad. It's still a bit hacky as we tap into the fields dict after calling the parents constructor. I have a variant of this where I moved the … -
Nicer dynamic forms in django
I used to make dynamic forms for Django in very bad way, I'm happy to admit that now as I've improved my process. Basically the solution is to use type() as I'm sure many of you know. If your doing that already there isn't much for you here. If your messing around with 'self.fields["name"]' in your forms then read on. Lets take a simple use case; a quiz system. You can think of it like who wants to be a millionaire. We will have a Question with four possible answers. So two models... from django.db import models class Question(models.Model): test = models.CharField(max_length=128) class Answer(models.Model): question = models.ForeignKey(Question) test = models.CharField(max_length=20) is_correct = models.BooleanField(default=False) Basically we want a form that stores both the question and the answers and checks the answer is a valid choice. You could do it this way. from django import forms class QuizForm(forms.Form): def __init__(self,question, *args, **kwargs): super(QuizForm, self).__init__(*args, **kwargs) self.fields['question'] = forms.IntegerField(widget= \ forms.HiddenInput, initial=question.id) self.fields['answers'] = forms.ModelChoiceField(queryset= \ question.answers_set) Now in this example that's actually not too bad. It's still a bit hacky as we tap into the fields dict after calling the parents constructor. I have a variant of this where I moved the …