Django community: Community blog posts RSS
This page, updated regularly, aggregates Community blog posts from the Django community.
-
Serving static files for Django on Webfaction
In my last post I wrote about the setup of Django with virtualenv on the Webfaction hosts. The post did not cover the serving of static files and will be covered here. It's always better to serve static media through an extra HTTP server/container doing nothing but serving static media files for better scaling. Normally I use nginx for this as it is small and fast. For Webfaction hosting you have two possibilities: You can add a "Static only" application that is an nginx, configure it for a separate (sub)domain and then configure your Django project to use this domain for serving the files. Wasn't that easy! Or you can use the Apache for serving the files. Create the "media" folder somewhere on your server (e.g. within the virtualenv folder: /home/<username>/webapps/<wsgi-app>/<virtualenv-dir>/media). Now adjust the apache.conf to load the modules required for serving the media and set some expiration time if you like (I used 12 hours as seen below). I'm only writing the updates to the apache.conf of the last post: ... # put after other LoadModule lines LoadModule alias_module modules/mod_alias.so LoadModule authz_host_module modules/mod_authz_host.so LoadModule expires_module modules/mod_expires.so ... <VirtualHost *:1234> ... # after WSGIScriptAlias Alias /media /home/your_user_name/webapps/your_wsgi_app_name/virtualenv_directory/media/ <Directory /home/your_user_name/webapps/your_wsgi_app_name/virtualenv_directory/media> Order … -
Serving static files for Django on Webfaction
In my last post I wrote about the setup of Django with virtualenv on the Webfaction hosts. The post did not cover the serving of static files and will be covered here. It's always better to serve static media through an extra HTTP server/container doing nothing but serving static media files for better scaling. Normally I use nginx for this as it is small and fast. For Webfaction hosting you have two possibilities: You can add a "Static only" application that is an nginx, configure it for a separate (sub)domain and then configure your Django project to use this domain for serving the files. Wasn't that easy! Or you can use the Apache for serving the files. Create the "media" folder somewhere on your server (e.g. within the virtualenv folder: /home/<username>/webapps/<wsgi-app>/<virtualenv-dir>/media). Now adjust the apache.conf to load the modules required for serving the media and set some expiration time if you like (I used 12 hours as seen below). I'm only writing the updates to the apache.conf of the last post: ... # put after other LoadModule lines LoadModule alias_module modules/mod_alias.so LoadModule authz_host_module modules/mod_authz_host.so LoadModule expires_module modules/mod_expires.so ... <VirtualHost *:1234> ... # after WSGIScriptAlias Alias /media /home/your_user_name/webapps/your_wsgi_app_name/virtualenv_directory/media/ <Directory /home/your_user_name/webapps/your_wsgi_app_name/virtualenv_directory/media> Order … -
Getting started with WTForm
I found out that Clint mentioned the little pet project of mine on his weekly roundup of happenings in the Django world. So I thought this would be a great time to tell the world a bit more about the project, and do a little practical example for the readers. What does WTForm do? Basically, WTForm does the following: Widget and Field type as class names on field container div for easier CSS styling. (and a required indicator as well) Allows you to group your form fields and columns in fieldsets. Allows you to group your form fields and fieldsets in columns. (oh - the redunancy!) With regard to the last two points, WTForm allows for "infinite" nested structures. Enables you to insert arbitrary HTML snippets in the middle of your form. Project background The project was started as small hack when we (at GMTA) needed another output scheme (namely using div tags) and needed to allow for easier CSS styling using classes on every container div tag. The problem is that the "vanilla" newforms will output the form without any classes indicating whether the field is required or what kind of field we are working with. This resulted in … -
Using decorators in Django views to assert the DRY principle
A lot of people finds decorates to be pure magic. I will try to de-mystify some of the magic around them, and show how we can use them to clean up the views in a Django application. I will start by giving a short introduction to decorators and closures and then afterwards show how we can use this in Django to, as the title says, assert the DRY (dont repeat yourself) principle So, what is a decorator? A decorator is a closure. That is, a set of local variables (an evironment) and some code (in this case, a function) to be executed in this environment. A small example of a simple closure: def adder(n): def inner(m): return n + m return inner When a call to adder is made, a new function is returned. This function has one local variable (n) already and takes another one as an argument (m). An example use could be: >>> add_five = adder(5) >>> add_five(37) 42 In Python, it's very popular to say that everything is an object and can be tangled with. This is also the case of functions. When defining a function, all you really do is defining a variable pointing to … -
Managing local settings in Django
Sometimes it is nice to be able to configure specific Django settings for a single host and not get tons of conflicts the next time you do svn up. I personally solve this by exploiting that the Django settings.py is nothing but Python code. At the last line of the file I do a: from local_settings import * That is, way I do a relative import from local_settings.py and gets every global symbol mixed into the current namespace, allowing me to overwrite every option. An example could be to configure the global settings.py to use sqlite as a database backend for the project, but in the production environment overwrite the DATABASE_* options in the local_settings.py. This goes as well for caching - not many developers run a PostgreSQL and memcached on their laptop. To make sure that local_settings.py never is committed to the repo (and maybe compromising database passwords), it is a good idea to add it to the Subversion property svn:ignore: svn propset svn:ignore local_settings.py /path/to/your/project Furthermore, I usually put up a local_settings.py.dist with a couple of commented out examples for the developers of what could be done here. -
Rolling your own simple authentication system for Django
Sometimes you need to roll a simple authentication system for Django. For example, I just hacked a client interface on our corporate intranet at GMTA, using a similar approach as the one I will describe here. The solutions consists of four parts: Extending the model to include a password Login form Login / Logout views A login_required decorator for views to require authentication Extending the model In order for us to authenticate a Client we need to add a password to the model. As it is not good practice to store passwords in plain text in the database, we will store the password as a salted hash. In this example I will use the SHA-1 secure hash function. We will add two functions to the Client model in order to handle the hashing itself. This is also helpful if we later in the project decides to change the hashing algorithm used. The two functions and the password field: password = models.CharField(maxlength=40) def update_password(self, new_password): self.password = sha.new(settings.PASSWORD_SALT + new_password).hexdigest() def check_password(self, password): hsh = sha.new(settings.PASSWORD_SALT + password).hexdigest() return hsh == self.password In order to update the password for a Client we just call the update_password method - as well as … -
Filtering foreign key choices in newforms-admin
I decided it was time to learn something about the newforms-admin branch of Django, so I set out to try to write a couple of simple models for administering a mail server (something we always needed at GMTA, but I have never really liked any of the currently available options). Here is the models so far: class Customer(models.Model): name = models.CharField(max_length=100) slug = models.SlugField() def __str__(self): return self.name class UserProfile(models.Model): user = models.ForeignKey(User, unique=True) customer = models.ForeignKey(Customer) class Domain(models.Model): name = models.CharField(max_length=100) customer = models.ForeignKey(Customer) def __str__(self): return self.name def save(self): # When a user adds a Domain, save the current customer. if not self.id: self.customer = get_current_user().customer_set.all()[0] # Call the super save method super(Domain, self).save() class Mailbox(models.Model): name = models.CharField(max_length=100) domain = models.ForeignKey(Domain) password = models.CharField(max_length=100) def __str__(self): return '%s@%s' % (self.name, self.domain) class Meta: verbose_name_plural = "mailboxes" class Alias(models.Model): name = models.CharField(max_length=100) domain = models.ForeignKey(Domain) destination = models.EmailField() def __str__(self): return '%s@%s -> %s' % (self.name, self.domain, self.destination) class Meta: verbose_name_plural = "aliases" Model notes There is a few gotchas in the above model code: Remember to add AUTH_PROFILE_MODULE to your settings. See the Django Book on user profiles for details. I use a thread locals hack to … -
New WTForm release
I have just published a new version of WTForm, my Django newforms addon to allow for grid form layouts (using YUI) and extra classes for more specific and easier CSS styling. There was a problem when using WTForm with form_for_model or form_for_instance resulting in not getting any fields in the generated form class. This should be fixed now. The djangosnippets page for WTForm now also includes a link for a screenshot to see an example of a form using grids and some other neat CSS styling by my colleague Oscar. -
M2m, tout petite astuce
Il faut parfois utiliser des m2m. Dans django, il y a deux moyens de les utiliser. Soit d'une façon simple, en déclarant juste un ManyToManyField soit en passant par une table explicite grâce à l'argument through. L'argument through permet d'avoir une table explicitement déclaré que l'on peut donc manipuler avec l'ORM. Mais si on utilise la ... -
How to sell Python panel at Pycon
Do you want to use Python but are you fighting Fear, Uncertainty, and Doubt (FUD) in your organization? Does any of this sound familiar to you?Python is just a scripting language.Python is too new to use in real production environments.Language X has better benchmarks doing obscure math so it is better for web development.Python sounds great but we've already spent so much on this expensive tool that does things badly!A lot of people want to use Python, but their customer, management, school, government, or organization won't let them. Python is a great tool, but can be challenging to get in the door. This panel will explore how companies and individuals have successfully introduced Python, what tools are available to sell Python, how to fight the good fight, and what pitfalls there are to avoid.Panel QuestionsFor this event at Pycon I've got some stock questions ready but I want the community at large to suggest some more.SpeakersFor this panel I choose five speakers from the broad categories of academia, government, commercial organizations, and non-profit who have had a known impact in their organizations:AcademiaC. Titus Brown is an Open source hacker, Artificial Life/Digital Evolution, B.A. Math (Reed), Earthshine research, Ph.D. Developmental Biology and Regulatory genomics (Caltech), … -
CSRF: Flash + 307 redirect = Game Over
CSRF: Flash + 307 redirect = Game Over. Here’s the exploit that Django and Rails both just released fixes for. It’s actually a flaw in the Flash player. Flash isn’t meant to be able to make cross-domain HTTP requests with custom HTTP headers unless the crossdomain.xml file on the other domain allows them to, but it turns out a 307 redirect (like a 302, but allows POST data to be forwarded) confuses the Flash player in to not checking the crossdomain.xml on the host it is being redirect to. -
Solr, Jetty, and daemons: debugging jetty.sh
I recently added a Solr-powered search feature to this site (using django-haystack). Rather than go to the trouble (and server resources drain) of deploying Solr via Tomcat, I decided instead to deploy it via Jetty. There's a wiki page with detailed instructions for deploying Solr with Jetty, and the wiki page also includes a link to the jetty.sh startup script. The instructions seem simple enough. However, I ran into some serious problems when trying to get the startup script to work. The standard java -jar start.jar was working fine for me. But after following the instructions to the letter, and after double-checking everything, a call to: sudo /etc/init.d/jetty start still resulted in my getting the (incredibly unhelpful) error message: Starting Jetty: FAILEDMy server is running Ubuntu Jaunty (9.04), and from my experience, the start-stop-daemon command in jetty.sh doesn't work on that platform. Let me know if you've experienced the same or similar issues on other *nix flavours or on other Ubuntu versions. Your mileage may vary. -
New Careers Page Inaugurated with Django Job Posting
I'm pleased to announce that we just released a new Careers section of our web site here at Caktus.  The section has been inaugurated with a new posting for a full-time Django developer position based out of our Carrboro, NC office (not far from Raleigh, Durham, or Chapel Hill), so kindly check it out and ... -
django CMS 2.1.1 released
django CMS 2.1.1 released -
Geographical django website demo
One of the reasons I gave for giving me a mac was I'll be able to make proper screencasts. I've got a mac now, so I have to make screencasts :-) The style I thought out for myself is to make two different versions of each video: One on my personal title on my own blog on my own smugmug account. I get to decide what to say and what to show, naturally. I'll do it in English and I'll show the technical aspects. One for Nelen & Schuurmans. A bit more polished and a bit less personal. In Dutch :-) And focused more on our customers' language. Not all videos that I make will get that second version, probably. Anyway, I've got a new toy to play with (again) and I've made my first screencast in two years! What do I want to show in this screencast? Hey, we make pretty OK geographical websites with lots of water data behind it. Hey, it is all working pretty OK. Not always terribly fast, but that's because we didn't get to the proper caching part yet. There's no TileCache or whatever to speed it all up. Just a bit of basic … -
Geographical django website demo
One of the reasons I gave for giving me a mac was I'll be able to make proper screencasts. I've got a mac now, so I have to make screencasts :-) The style I thought out for myself is to make two different versions of each video: One on my personal title on my own blog on my own smugmug account. I get to decide what to say and what to show, naturally. I'll do it in English and I'll show the technical aspects. One for Nelen & Schuurmans. A bit more polished and a bit less personal. In Dutch :-) And focused more on our customers' language. Not all videos that I make will get that second version, probably. Anyway, I've got a new toy to play with (again) and I've made my first screencast in two years! What do I want to show in this screencast? Hey, we make pretty OK geographical websites with lots of water data behind it. Hey, it is all working pretty OK. Not always terribly fast, but that's because we didn't get to the proper caching part yet. There's no TileCache or whatever to speed it all up. Just a bit of basic … -
The big secret project I've been working on...
With the official press release out the door, I can finally start talking about the project that has been consuming most of my time for the last several months! StoryMarket is an online system for making finding, buying, selling and sharing content a la carte easy. It's primarily focused towards journalists and the news industry, but we expect there to be a large contingent of bloggers and other new media content creators from all sectors. StoryMarket was developed as a partnership between Revolution Systems and The World Company, publisher of the Lawrence Journal-World the same great company that brought us our beloved Django. The news industry is in trouble, with declining revenues and increasing costs, the old content syndication models are proving to be more than smaller organizations can bear. You can think of it as a mashup of eBay or Etsy and iTunes with a strong social component similar to LinkedIn. Sellers are given extremely flexible tools for managing their individual prices and rights. From being really open and cheap, to being really restrictive and expensive and everything in between. For example, I could give this particular blog post a price of $20 to all StoryMarket users, $10 for … -
Django and Remote Sybase Servers
It took me a while to figure it out, so I decided to share. DATABASES = { 'default': { 'ENGINE': 'sqlany_django', 'USER': 'YOUR-USER', 'PASSWORD': 'YOUR-PASSWORD', 'OPTIONS': { 'links': 'tcpip(host=YOUR-DB-IP-OR-HOSTNAME;DoBroadcast=None)', 'ServerName': 'YOUR-SERVER-NAME', }, } } For the sake of completeness, connecting via the regular DB API would be: sqlanydb.connect( links='tcpip(host=YOUR-SERVER-HOST;DoBroadcast=None)', ServerName='YOUR-DB-NAME', uid='YOUR-USER', pwd='YOUR-PASSWORD' ) It’s not hard – but tedious to tinker out. -
Evented Django part one: Socket.IO and gevent
The buzz around the asynchronous, real-time web has been getting more and more attention lately, and for good reason. The old paradigm of thick servers and thin clients is getting outdated as the new web demands rich, fast, asynchronous, full-duplex messaging. The technologies that enable server-to-browser asynchronous messaging have been given the umbrella term "Comet," and the number of ways to provide Comet services is growing constantly. The transport options include XHR-multipart, WebSockets, and Adobe Flash Sockets, among others. Socket.IO was invented to provide a unified interface for server-browser messaging and let the developer not worry about the inconsistent browser support. In this post, I'm going to explain how to use Django with Socket.IO.Socket.IO was developed with a Node.JS server implementation, but work is being done to add server implementations to a variety of languages. Two such servers exist for Python, tornadio and gevent-socketio. I'm a big fan of gevent, so I will use gevent-socketio, but tornadio looks well-written and very promising. Why you should be thinking about gevent Socket.IO runs great under Node.JS, but I think it's important to highlight why I think Python and gevent need more attention (feel free to skip ahead if you have already drank … -
Rename uploaded files to ASCII charset in Django
Last time I had to substitute files from backups brought unpleasant surprise. About 80 files, thats 10% of all files, did not display on website. Digging into issue fast revealed that all those files contained exotic, non ASCII characters in them. Django does not convert uploaded unicode filenames, and all modern operating systems have support for unicode filenames. Amazon S3 have support for unicode filenames. The problem was, that sync scripts that was used to backup files does not have so good support for unicode in filenames. Fortunately changing Django default file storage so it saves all files in ASCII encoding takes only two steps: 1. Subclassing default FileSystemStorage: import unicodedata from django.core.files.storage import FileSystemStorage class ASCIIFileSystemStorage(FileSystemStorage): """ Convert unicode characters in name to ASCII characters. """ def get_valid_name(self, name): name = unicodedata.normalize('NFKD', name).encode('ascii', 'ignore') return super(ASCIIFileSystemStorage, self).get_valid_name(name) 2. Tell Django to use ASCIIFileSystemStorage as default storage. This is to be added in settings.py. DEFAULT_FILE_STORAGE = 'utils.storage.ASCIIFileSystemStorage' More info about File storage is available in Django documentation. -
Django on twistd.web.wsgi – Issue Workaround
My last few posts have generally been about my final year project - Backtrac Backup System. One of the recent challenges I faced was getting the Django-based web interface to run under the Twisted WSGI server. In this post, I'll describe a major issue with this process, and how I worked around it. TAC or [...] -
development with virtualenv
`Virtualenv `_ is a tool for isolating `Python `_ projects. Such Python projects can have their own set of packages and their versions without affecting each other. This article will tell you how to create and work with such environments and how to use them with your `Django `_ projects. This is a follow-up to the `last article `_ on virtualenv and pip. -
Announcing django-constance
`django-constance `_ is live settings for `Django `_ in `Redis `_ featuring a Django admin interface and easy migration from your static Django settings. -
Profiling Django Projects with Cachegrind
Your site is slow in the production and you do not know why. It is time to profile it while it is running live: with `cProfile `_ and `KCachegrind `_. You will get very nice charts and tables with time information and other data. It will show where your bottleneck is. -
Trying: REST api design
Two months ago I made my opinion about SOAP and REST clear. Happily, I now have a small project where I have to actually make a REST interface. So here's some preliminary thinkwork. Suggestions welcome! My thought on what a proper REST api ought to be about: Linked data. No magic URLs which you have to know exactly. From a root URL, you should be able to find everything you need by just following the URLs. So not a list of IDs of something that you have to hand-craft a URL with, but simply a list of URLs with the ID already included. Json. In principle, json is returned. No html, no xml. Json is the de-facto simple data exchange format nowadays. Images, html, wms: those are alternative representations that can be requested. Self-describing. Adding a couple of 'name' and 'explanation' attributes to the json helps a lot in explaining the data. It aids discovery and debugging. Resource-based. The 'normal' part of the URL is for identifying resources. Extra parameters (?width=100&height=80) are for small tweaks and adjustments. Identify resources, not actions. Give every resource a URL, restrict the use of verbs. So not /get_document?id=42, but /documents/42. In our case, in …