Django community: RSS
This page, updated regularly, aggregates Community blog posts from the Django community.
-
Django-Compact Updates
Getting back into the swing of things on django-compact today: Today's Commits Got concatenation working for both js and css Added minification support via YUI Compressor This second item in the list leads me to a question about best practices when creating a re-distributable Django app. Minification support is an optional flag passed at run time to build your css/js: ./manage.py compact --minify If specified, it will run the concatenated files through the YUI Compressor minifier. Since this tool is java based, and not python :(, I decided to include the jar file in a lib folder under the application folder. I then created a module called yui.py and encapsulated the shell out via Popen in a python class called Compressor. This does assume that the java executable is in your path and is of a version greater than 1.4 (YUI Compressor requires at least 1.4). The question I have is, is there a best practice of where and when to include binaries such as this? I feel that this is pretty clean in that I construct the path to the jar dynamically at run time so i don't assume any particular install location. I could do more up front … -
Django-Compact Updates
Getting back into the swing of things on django-compact today: Today's Commits Got concatenation working for both js and css Added minification support via YUI Compressor This second item in the list leads me to a question about best practices when creating a re-distributable Django app. Minification support is an optional flag passed at run time to build your css/js: ./manage.py compact --minify If specified, it will run the concatenated files through the YUI Compressor minifier. Since this tool is java based, and not python :(, I decided to include the jar file in a lib folder under the application folder. I then created a module called yui.py and encapsulated the shell out via Popen in a python class called Compressor. This does assume that the java executable is in your path and is of a version greater than 1.4 (YUI Compressor requires at least 1.4). The question I have is, is there a best practice of where and when to include binaries such as this? I feel that this is pretty clean in that I construct the path to the jar dynamically at run time so i don't assume any particular install location. I could do more up front … -
My Latest Project: BackTrac Backup System
My life has been pretty busy as of late, mostly with a new project I am working on called BackTrac. I am developing a network backup solution, written entirely in Python - using the Django framework as a front-end web interface.This came at quite a good time, as I've been asked to give a presentation [...] -
Numeric IP field for Django
Some time ago I needed to add an IP field to my model with more records (some hundred thousands). I was going to just add Django's IPAddressField, but I realized that it stores the data as text on the database, and I didn't like the idea.Basically, and IP address is just 4 bytes of data, but it's text representation can use between 7 and 15 bytes. That's not a big different when your model will have few rows, but it's a different when you'll have a huge set of IP addresses, and specially if you want to join tables by it.The only inconvenient of storing the IPs as numbers is that are not human readable if you want to check them directly to database.So, here you have my code that can be used as a replacement of IPAddressField:import IPy from django.db import modelsfrom django import formsfrom django.utils.translation import ugettext as _def _ip_to_int(ip): return IPy.IP(ip).ipdef _int_to_ip(numeric_ip): return IPy.IP(numeric_ip).strNormal()class IPFormField(forms.fields.Field): def clean(self, value): try: _ip_to_int(value) except ValueError: raise forms.ValidationError, \ _('You must provide a valid IP address') return super(IPFormField, self).clean(value)class IPField(models.fields.PositiveIntegerField): ''' IP field for django for storing IPs as integers on database (Django's field IPAddressField stores them as text) ''' __metaclass__ … -
Creating Django Management Commands
Creating a website with Django is great fun, but eventually you’ll need to write a tool to clean up you data, expire old users or one of the myriad of other administration tasks that are involved with running a website. You’ll be very used to using manage.py to create your database and to run your [...] -
Welcome Jacob Kaplan-Moss
I'm very pleased to announce that Jacob Kaplan-Moss has joined Revolution Systems to head up a new line of services around the ever growing Django web development framework. First up are commercial Django Support Plans, but look for more Django related offerings in the near future. Jacob has been a good friend of mine since before Django was even released.  It was a pleasure to work with him at our previous day jobs and I'm very excited for the future ahead.  Not only is he obviously an authority on Django, he's an amazing developer and generally an expert on all things tech. Jacob and Adrian are both great examples of how to lead an Open Source project and grow a real community around it.By offering Django Support packages we hope to help adoption of Django in the business world, which helps grow the community at large.   -
Easier field translation with django-transmeta
django-transmeta is a new project that provides django field translations in a simpler way than existing ones like django-multilingual and transdb.The basis of that simplicity is creating a field in the database table for every translation, so internally we'll have something like:CREATE TABLE app_model ( [...] myfield_en varchar, myfield_ca varchar, [...]);where "en" and "ca" are the languages in our application (English and Catalan in this case).For the developer, translating a model is as simple as adding a metaclass to the model, and specify the fields to translate in its Meta class:from transmeta import TransMetaclass MyModel(models.Model): __metaclass__ = TransMeta name = models.CharField(max_length=64) description = models.TextField() price = models.FloatField() class Meta: translate = ('name', 'description', )Even with this project is still as tricky as transdb and multilingual, its main goal is being really really simple, for its design, for developers, and its code (that mainly it's about 120 lines of code). It also breaks some limitations of transdb (its most simple predecessor IMHO) like translating non-text fields.I also want to mention that I just discovered a new project for translating model fields, named django-modeltranslation, that looks cool, but I don't like the (admin like) registering way to set translatable fields (too much complicated). -
It's time for a change
It’s the era of change, I suppose… Starting today I’m joining Frank Wiles – a good friend and fellow Lawrencian – at his consulting firm, Revolution Systems, LLC. I’m really excited to be working with Frank: he’s a great guy, and a crazy-smart developer and sysadmin. Together, we’re going to be offering professional Django support to companies who need a Django expert on staff, but can’t afford someone full-time. The Django community is terrific; those who are comfortable with the IRC/mailing list support venue can get great assistance that way. -
Django Brasil no Twitter
Agora os artigos coletados pelo agregador de feeds da comunidade serão publicados no Twitter, isso graças à colaboração do djangonauta Arthur Furlan (valeu Arthur, a comunidade agradece!). Então, quem deseja acompanhar a comunidade brasileira de Django pelo Twitter, inscreva-se no @djangobrasil. E se você ainda não possui seu blog cadastrado em nosso agregador de feeds, envie para nós o link para o feed da tag (ou categoria) "Django". Colaborações com código para o site são muito apreciadas e estamos precisamos de mais. Se interessar, acesse a página do projeto. -
Revisiting long running processes in Django
So in my last post on this topic I discussed different ways to handle the problem of running code that wold otherwise cripple your web server, by pushing them off to another process. It was mentioned that there may be other ways to attack the problem, either through Python threads or Django’s signals. This post is a review of those two suggestions. Django’s signals I’ll attack the simpler option first, since the response to whether this solution is viable takes all of one word: no. Signals are not asynchronous by nature and therefore they do not work as a solution to the problem. It is true that you can setup signals to work asynchronously, but you’d have to employ one of the methods discussed to handle that. So if you were to use signals to handle some asynchronous work then all they would really be doing is changing the layout of that code. You’d still need something else to actually handle the asynchronous work. I’ll offer up a simple example of how signal code looks just for completeness. There are three pieces to signals. 1.The signal itself 2.The sending of the signal 3.The listeners of the signal 1. Setting up … -
Full Django Serializers - Part II
In the first part of this article I covered the excludes and extras options to the Wad of Stuff serializer. In this article I introduce the relations option.RelationsThe Wad of Stuff serializers allow you to follow and serialize related fields of a model to any depth you wish. This is why it is considered a "full serializer" as opposed to Django's built-in serializers that only return the related -
Fast Caching with Django and Nginx
I've been toying with optimizing the caching on my blog recently – for my own interest (this humble blog doesn't get all that much traffic). All the same, any speed improvements will only mean snappier page-loads and greater capacity to handle a slashdotting, or similar. I discovered that Nginx has a memcached module that can serve pages directly from memcached without touching the file-system, or a downstream web-app. Which makes for very fast response times. To get it working, you need to set a cache key named with the url of the page, and the value to the HTML. Alas, this means that it would not work with Django's caching mechanism due to the way Django caches unique pages based on the contents of the request header. -
Fast Caching with Django and Nginx
I've been toying with optimizing the caching on my blog recently – for my own interest (this humble blog doesn't get all that much traffic). All the same, any speed improvements will only mean snappier page-loads and greater capacity to handle a slashdotting, or similar. I discovered that Nginx has a memcached module that can serve pages directly from memcached without touching the file-system, or a downstream web-app. Which makes for very fast response times. To get it working, you need to set a cache key named with the url of the page, and the value to the HTML. Alas, this means that it would not work with Django's caching mechanism due to the way Django caches unique pages based on the contents of the request header. Still, the promise of serving up cached pages without even touching Django was most tempting. So I took the cache middleware from Django and butchered it so that it created a simple enough cache key for Nginx to handle. Here's the code that generates a cache key, given a request: def get_cache_key(request, key_prefix=None): if key_prefix is None: key_prefix = settings.CACHE_MIDDLEWARE_KEY_PREFIX cache_key = '%s.%s' % (key_prefix, request.path) return cache_key The following snippet, taken from … -
Django, MySQL & UTF-8
Last week I finally decided to switch my hosting service from MediaTemple to SliceHost. I have been postponing this move for almost a year, since I simply didn’t have the time to redeploy ten separate domains and move all their e-mail accounts. But, in a moment of foolishness, I cancelled my MediaTemple account and opened a brand new SliceHost one with Ubuntu 8.04 - little did I know what I was getting myself into… Actually, the move was pretty painless. MediaTemple’s insistence on using CentOS has been putting me off for the past couple of years, and redeploying my Django sites basically meant changing a few lines in my Fabric deployment scripts. I had been forwarding all my mail to separate Gmail accounts for a while now, so I ended up opening a Google for Domains account and that was ready as well, after some DNS setup. The only problem I faced was with MySQL, as I had expected. I never use MySQL for new projects anymore, Postgres provides a much more robust and consistent solution. However, my MediaTemple service didn’t have a PostgreSQL server, so all my Django projects had to run on MySQL. I did mysqldumps on the … -
Eldarion Logo
Those of you who follow my good friend James Tauber on twitter, facebook or through the Pinax Project have already heard that James has resigned from mValent. -
Ordering related objects in Django
Django models have a meta option order_with_respect_to that allows you to order objects within the scope of a related ForeignKey object.This option adds an _order column to the model's database table to keep track of this ordering.That's all very well, but if you need to change the sequence order you may well feel at a loss. The official documentation makes no mention of this option beyond a basic explanation of its purpose , and editing your objects in the built in admin app reveals no user interface for changing the ordering either. [1]Update Feb 2009: There's now a ticket for this functionality to be added to the docs.I managed to uncover a Python interface by delving into Django's API internals [2]. An object with related models that order with respect to it is given two handy methods:get_RELATED_order()set_RELATED_order()where RELATED is the lowercased model name of the ordered objectsclass Entry(model.Model): # ... fieldsclass Comment(model.Model): entry = model.ForeignKey(Entry, related_name='comments') # ... more fields class Meta: order_with_respect_to = 'entry'Given the above example models, you can retrieve the order of an entry's comments:>>> entry = Entry.objects.latest()>>> entry.get_comment_order()[1, 2, 3, 4]And change the ordering by passing a list of comment ids back in.>>> entry.set_comment_order([2, 1, 4, … -
Serving Django via CherryPy behind cherokee
Almost one year ago "Peter Baumgartner" wrote about Serving Django via cherrypy I am going to explain in this post how to take this approach one step further and to add load and balancing of the requests to several instances of cherrypy. This sounds like a lot of "*.conf" editing, isn't it ? In fact the nice thing about this approach is that the only file you will have to edit is your settings.py to add one line. To do so I am going to use cherokee mainly because it has a user friendly interface called cherokee-admin which provides a very easy way to configure your server. For this article I have used cherokee Version 0.98.1.I will assume in this article that you have a working django project in a virtualenv. First you will need to install cherrypy and django-cpserver. You have several way to do this the easier is probably to use pippip install cherrypypip install -e git://github.com/lincolnloop/django-cpserver.git#egg=django-cpserverThen you need to edit you settings.py to add "django_cpserver" in the list of your INSTALLED_APPS. This will give you a convenient django management command to start cherrypy server../manage.py runcpserver port=8089Believe it or not this was the hardest part of the recipe … -
Django: DRY Custom Model Forms and Fields
With the release of Django 1.0, the ability to add a list of validators to a model field was removed. This was replaced with the clean*() methods in the new forms classes. Unfortunately when you write your own form subclasses you may end up repeating a lot of the parameters in your form field definitions that you had already declared in your model fields such as max_length, required, help_text, -
Error común en upload_to de ImageFields y FileFields
Hoy me he topado con un artículo sobre un error muy común al utilizar ImageField ó FileField. Al usar cualquiera de estos dos tipos de campo podemos especificar la ruta relativa a nuestro setting MEDIA_ROOT en la que queremos que se guarde el archivo subido mediante el argumento upload_to... -
More on extended-markup
In my last post I introduced extended-markup, which is the light-weight markup system used to generate posts in Django-Techblog. I'll cover a few other things it can do in this post. When the Post model is saved to the database, the extended markup is parsed in to a structure that is basically a dictionary containing a list of chunks, and associated variables. The order that the chunks appear in each section is the same as the order they appear in the markup, unless a chunk variable called priority is defined. This causes the chunks to be sorted in descending order of priority, chunks without a priority value are assigned a default of 100. -
Django Techblog markup system
Django Tech Blog is now running my blog. It is only fitting that my first post on the new system is about the technology behind it. I never intended to compete with Wordpress on number of features, and Techblog was never intended to be an all-things-to-everyone type of web application, but I can boast a few features that set it apart. I'll cover some of those features in future posts, for now I would like to go over the light-weight markup language I use for posts. -
Descubriendo objetos similares por sus etiquetas 2
Ya hablamos anteriormente sobre cómo descubrir objetos similares por sus etiquetas con la versión 1.0 de Django. Sin embargo la versión de desarrollo de Django incluye funciones de agregación que nos permiten realizar este tipo de tareas de una manera más cómoda sin tener que utilizar SQL puro como hacíamos antes. Partiremos exactamente del mismo código que usamos la vez anterior: Dos modelos definidos en el archivo models.py de nuestra aplicación... -
Nashvegas: A Simple Django Migration Tool
Introduction Back in July I discussed how I built a tool to help manage database changes across a team and through deployment for our Django based project. And yes, I am aware of other approaches/projects tackling this same problem. None of them seemed to work how I wanted them to as they all seemed to focus on trying to keep you from writing the raw sql yourself. I am comfortable with writing SQL and for something that will execute automatically against production databases, I prefer to have that control in knowing exactly what will get executed. Yes, many of these tools have options to allow me to preview the generated SQL, but it still gave me the feeling of being boxed in. For your reference, here are the ones I reviewed: South dmigrations django-evolution deseb yadsel schema-evolution So I abstracted out the script and it's assumptions about our environment to function as a management command in a stand alone reusable app. My Approach It's extremely simple and is based purely on executing SQL statements in order based on it's file naming scheme. Basically, you create a folder somewhere in your project -- I call mine "db". Within this folder you … -
Nashvegas: A Simple Django Migration Tool
Introduction Back in July I discussed how I built a tool to help manage database changes across a team and through deployment for our Django based project. And yes, I am aware of other approaches/projects tackling this same problem. None of them seemed to work how I wanted them to as they all seemed to focus on trying to keep you from writing the raw sql yourself. I am comfortable with writing SQL and for something that will execute automatically against production databases, I prefer to have that control in knowing exactly what will get executed. Yes, many of these tools have options to allow me to preview the generated SQL, but it still gave me the feeling of being boxed in. For your reference, here are the ones I reviewed: South dmigrations django-evolution deseb yadsel schema-evolution So I abstracted out the script and it's assumptions about our environment to function as a management command in a stand alone reusable app. My Approach It's extremely simple and is based purely on executing SQL statements in order based on it's file naming scheme. Basically, you create a folder somewhere in your project -- I call mine "db". Within this folder you … -
FAQ: Untrusted users and HTML
An input form that takes raw HTML. It’s a pretty common thing to see in web apps these days: many comment forms allow HTML, or some subset thereof; many social-network-style applications allow end-users to enter HTML in their profiles; etc. Unfortunately, allowing untrusted users to enter raw HTML is incredibly dangerous; read up on XSS if you don’t know why. So a common question that comes up in web developer circles deals with how best to “escape” user-entered HTML so that is safe for presentation.