Django community: RSS
This page, updated regularly, aggregates Community blog posts from the Django community.
-
Evaluating Django Caching Options
Caching is one of the first things you can do when you need to start thinking about scaling. Among efforts such as query minimization, denormalization, code optimizations, compression, database tuning, indexing, and load balancing, caching remains one of the lowest hanging fruits in methods to lighten your server load and handle huge amounts of traffic. There are many options, and I chose to evaluate a few of the most interesting setups.This is not intended to be a rigoriously scientific test, but more of a first impression of the different caching systems. For all the tests I'm describing, I'm using a single VPS on Rackspace Cloud with 320MB of RAM, a quad-core AMD Opteron 2350HE, and a bleeding edge server stack using Ubuntu Server 9.10, NGINX with UWSGI, Python 2.6, Django 1.1, and PostgreSQL 8.4. I'm serving the home page view of Django-Mingus, which provides a realistic amount of complexity to the Python side of things and gives us a 9387 byte response. I'm using 4 UWSGI processes and a single NGINX worker. All my tests are using ApacheBench, which I'm running on the same machine. Note that for all my cache tests I'm prepopulating the cache before running the benchmark. … -
Thread-Safe Object-Oriented Views in Django
An emerging design pattern in Django is the usage of class-based views. Writing views as classes is made possible by Python features that allow classes and objects to behave as callables, just like functions. They can help organize view code and promote reusability by offering a greater level of customization. However, callable objects sometimes have thread safety issues that developers are often not aware of. Update 2/2/11: This post is now mostly irrelevant, because Django will soon solve this problem with the newly-refactored generic view module in Django 1.3, which is due for release this month. Since I wrote this post a year ago, massive arguments over the design of class-based views erupted on the mailing list. It definitely got out of hand, but a lot was learned, and I'm happy with the solution that was committed. I still encourage people to understand how to avoid threading side-effects. I would also suggest reading the new documentation on class-based generic views. The most common approach to class-based views is to create a callable object, a class that has been written with a __call__ method, therefore making instances of the class callable. The view is instantiated either as a module-level variable in … -
Introduction to Surlex
Surlex (Simple URL Expression) is a language for matching and extracting text, similar to regex (regular expression). It works by generating a regular expression from a more concise syntax that is designed to produce easy-to-read url patterns.Installation Surlex is indexed in PyPI, so it can be installed via pip or easy_install: pip install surlex Alternatively, the source code repository is available via git: git clone git://github.com/codysoyland/surlex.git Basic Capturing A capture is performed by enclosing a variable name inside angle brackets: <name> This is equivalent to the following regex: (?P<name>.+) A usage example for matching a blog index view is: /blog/<year>/ This is equivalent to the following regex: /blog/(?P<year>.+)/ This surlex would match the following URLs: /blog/2008/ /blog/2009/ To invoke the capture in our source code we can use the Surlex class or the match function: from surlex import Surlex, match surlex = Surlex('/blog/<year>/') # returns surlex object surlex.match('/blog/2009/') # returns {u'year': '2009'} match('/blog/<year>/', '/blog/2009/') # returns {u'year': '2009'} match('/blog/<year>/<month>/', '/blog/2009/09/') # returns {u'year': '2009', u'month': '09'} Advanced Capturing with Embedded Regex Surlex allows you to insert custom regex strings inside your surlex by postfixing the capture name with the equals sign (=) followed by a regular expression: from surlex import … -
Introduction to Surlex Slides from Djangocon 2009
I have uploaded my slides from my lightning talk on Surlex at Djangocon 2009.I have uploaded my slides from my lightning talk on Surlex at Djangocon 2009. Here is the link: http://media.codysoyland.com/pdf/surlex-presentation.pdf. -
Django Template Debugging Made Easier with django-template-repl
When working with the Django template language, specifically writing template tags or trying out new template tags that are not well documented, it's easy to fall into a testing loop that involves modifying your code, saving the file, causing runserver to restart, which could take some time for large projects, switching to your web browser, hitting reload, and viewing the results. This workflow can be repetitive and unproductive. I decided to improve template interpreter interactivity by writing a REPL for it, and I released the project as django-template-repl, which is freely available on Github and PyPI. I was surprised and happy to see it was well accepted judging from the twitter chatter and github statistics, so I wrote this to better explain how to use it.One of the greatest advantages of Python, Lisp, and other programming languages is the ease of debugging and understanding code behavior provided by REPLs. A REPL, which stands for Read-Eval-Print Loop, is a shell that gives you an interactive command line session with your language interpreter. The two REPLs most Django users should be familiar with are ipython and (i)pdb. These tools are incredibly helpful and really boost the usability of Python. I spend more … -
We're Hiring!
Lincoln Loop is looking to hire a Python/Django developer. -
Gondola for Real Estate Launch
I’m pleased to announce that Gondola for Real Estate”, our GeoDjango-backed real estate CMS is publicly accepting new customers. A couple weeks ago, we quietly re-launched gondolacms.com with some details about the platform and what it has to offer, as well as some sites that have been built on it over the last few months. -
Custom Filters in the Django Admin
Adding custom data filters to the Django admin interface -
Django 1.0 Template Development Book Review
Packt Publishing was nice enough to send us a copy of Django 1.0 Template Development by Scott Newman for review recently. I get most of my technical information via the web, so picking up a technical book was a nice change of pace. This is a well written and enjoyable read for people looking to learn a little more about Django than what is provided in the Django tutorial. -
Django 1.1 Testing and Debugging Book Review
A review of Django 1.1 Testing and Debugging by Karen M. Tracey, published by Packt Publishing. -
Using Proxy Models to Customize the Django Admin
Fellow Lincoln Looper, Martin Mahner, posted an excellent write up on how to use proxy models to separate staff and user accounts in Django’s admin. We frequently have a need for this in client projects, but people don’t often consider proxy models for this functionality. Here’s another scenario we came across recently where they came in useful. -
Lincoln Loop Development Sprints and Limited Time Discount
Today, we are launching a new offering at Lincoln Loop, development services paid by the sprint. Previously, our typical engagements involved a (sometimes lengthy) discovery, estimation, and bidding process. Lately, however, people have been asking us to jump in with both feet and help them get started immediately. For those people, we think paying for services on a sprint-by-sprint basis will be a great option. As part of our initial promotion for the service, we’re discounting the price 30% for a limited time. -
Installing GeoDjango Dependencies with Homebrew
Installing the GeoDjango dependencies (PostGIS, GEOS, Proj4, and GDAL) on OS X with Homebrew. -
Easy Fabric Deployment, Part 2: Multiple Committers and the Dreaded Umask
In part 1, we showed how we use Fabric to update and deploy Django sites to our development server with a single command. This works great when you only have one developer pushing changes to the server, but what happens when multiple committers need to update the development server? -
Tracking Application Response Time with Nginx
Using Nginx to track response times for multiple Django sites served via Apache/mod_wsgi -
Using Haystack to index non-database content
Over on ReadTheDocs, I wanted to build search around the documentation that we're hosting. I chose Haystack and Solr for this, because it's the best way to do search in Django these days. However, I've only ever used Haystack to index content that is in the database. I thought about trying to add all the rendered HTML from the documentation into the database, but that was a non-starter. I ended up adding a ImportedFile model to the database, which would contain the metadata for the HTML file: class ImportedFile(models.Model): project = models.ForeignKey(Project, related_name='imported_files') name = models.CharField(max_length=255) slug = models.SlugField() path = models.CharField(max_length=255) md5 = models.CharField(max_length=255) This allows me to link the SearchIndex in haystack to a model. Then the interesting part is in the Haystack SearchIndex, where I override the prepare_text method, allowing me to read the data in from the filesystem instead of from the database. class ImportedFileIndex(SearchIndex): text = CharField(document=True) author = CharField(model_attr='project__user') project = CharField(model_attr='project__name') title = CharField(model_attr='name') def prepare_text(self, obj): full_path = obj.project.full_html_path to_read = os.path.join(full_path, obj.path.lstrip('/')) try: content = codecs.open(to_read, encoding="utf-8", mode='r').read() return content except IOError: print "%s not found" % full_path site.register(ImportedFile, ImportedFileIndex) This means that I don't have to bloat my database with … -
Building a Django App Server with Chef: Part 2
Alternate title: Actually doing something useful. Yesterday we covered the basics to getting started with Chef. You should have a remote server configured with chef, and have curl installed! Now lets go ahead and get some useful bits for your Django application. What we'll need So this is going to be based around the way that I set up my servers, so if this is different than you, I'm sorry. However, I think it is a pretty solid way of managing them. A lot of the ideas here are stolen from Travis when he set up the server for Pypants. So lets assemble a list of things we're going to want in order to get a super basic Django configuration running: A user to run our code as and who's home directory we'll store the data. A basic global python ecosystem, including setuptools and pip A virtualenv to store all the project-specific packages and code in A copy of the project that we'll be running Let's get started. The finished code for today is located on github, with the tag blog-post-2. It is a copy of the completed steps, so feel free to peek through that and come back here … -
Building a Django App Server with Chef: Part 3
Alternate title: Show the world what you've got. This is Part 3 of my Chef tutorial. Today we're talking about deployment. You can check out the first 2 parts of the series: Part 1: Chef Beginnings Part 2: Python environment buildout Today's code will be in the git repo under the tag blog-post-3. What we'll need We'll be taking the Django application that we have on the server and actually deploying it. Let's make a list of what we'll need: A web server to sit in front and proxy requests A WSGI server A way to keep both of these things running A caching layer We'll be using Nginx, Memcached, Upstart, and Gunicorn. This is my preferred deployment stack as of late, mainly because of the simple setup. Let's get started A web server Getting Nginx up and running should be old hat by this point. We're going to need the package and service Resources, which will tell Chef to install and run it. cookbooks/main/recipes/nginx.rb package "nginx" do :upgrade end service "nginx" do enabled true running true supports :status => true, :restart => true, :reload => true action [:start, :enable] end cookbook_file "/etc/nginx/sites-enabled/readthedocs" do source "nginx/readthedocs" mode 0640 owner "root" … -
Djangocon Talk
I just gave a talk title "Large problems, Mostly solved", which you will recognize if you've been reading this blog for a little while. I took my past series of Large problems posts, and expanded on them into a full talk. The video and slides are posted below. Thanks for the A/V team kicking ass this year and getting the videos posted in amazing time. Also many thanks to the conference organizers and volunteers who managed to make it all happen, it was a great time! Large problems, Mostly SolvedView more presentations from ericholscher. -
Virtualenv Tips
Virtualenv is a project that is indispensable for most Python devs these days. I am writing down some tips here so mainly for personal reference, and because I found them useful. Virtualenv & Upstart For my ReadTheDocs project, I was wanting to run celery as a background process that processes documentation building. I'm running Ubuntu, so their built-in upstart service seems like a logical choice. I really like upstart because of it's simple configuration, but it is rather undocumented (this Stanzas) page is a useful starting point). Carl Meyer pointed out to me that in order to get inside the context of a virtualenv, you don't need to munge your pythonpath or anything, but simply run the correct script from inside the virtualenv. So a simple /path/to/virtualenv/bin/django-admin.py celeryd was all that was needed to get inside the virtualenv's context. This also true of the python executable inside your python directory. /path/to/virtualenv/bin/python will allow you to run any python script inside of that virtualenv's context. I also wanted to be running my jobs as the user for that site, so sudo is the correct tool for that. The final file ended up looking like this: description "Celery for ReadTheDocs" start on … -
Read the Docs Updates
Documentation writing will always be hard work. It's a much different mind-set than programming, and people that write good code might not necessarily write good docs. However, this is a known issue, and something that can't really be solved. What you can do is make it easier to write documentation. Every step along the way that you can give yourself an excuse to not write documentation is another undocumented open source project. Luke Plant has a great post up about how important documentation is, and I completely agree. I imagine a lot of the people using Django are using it because of the documentation. I think as members of the Django community, we need to build a culture of documentation within the greater Python world. Python does tend to have better documentation than a lot of languages, but it's still not nearly what it could be. Read the Docs exists to make it easier to host your Sphinx documentation. Over the weekend, Bobby, Jonas, and I added a bunch of new features to the site. I think it's getting to the point where there isn't an easier or better way to host the documentation for your Django project, and we're … -
Handling Django Settings Files
I have seen a lot of talk over the past couple years about how to handle different settings files and databases, synced between production and development. I have happened onto a way of doing it that makes me happy, and figured I would share it with the world. File structure I use a file structure that looks like this: project/ settings/ __init__.py (empty) base.py sqlite.py postgres.py The base.py contains all of the configuration options that are shared among the databases. INSTALLED_APPS, etc. All of the DATABASE settings should be specified in the more-specific files. As well as things that differ by environment, like remote servers, cache settings, cookie domains, and other things. This allows you to run the sqlite settings file, and have it be set to localhost, or whatever your development settings are. Then in production you just run against the postgres settings. A good example of this being used in practice is on Read the Docs. But wait, there's more! manage.py for dev ./manage.py is great for development. It is the easiest way to get started, and it automatically sets up your paths and stuff. With my setup, I actually explicitly set manage.py's settings file to the sqlite … -
Celery Tips
Following on yesterday's post about Virtualenv Tips, I will be talking about celery tips. Yesterday I talked about how to run celery with upstart easily, and today I'll be expanding on that below as well as talking about how to set it up using supervisord. Note: Also interesting, I wrote a Big list of django tips back in 2008, that still has a lot of good information. Running celery in development When you run celery in production, you should be using a queue on the backend. However, when you're running celery in development, it's nice to execute the code paths, but not actually need a queue. This is where the CELERY_ALWAYS_EAGER setting comes in handy. It makes celery run the code in process, but will make sure your code paths work correctly. I talk about this and more in my djangocon talk. Killing long running tasks On ReadTheDocs I would run into problems with celery tasks never returning. Luckily, celery has a way to handle this. The CELERYD_TASK_TIME_LIMIT setting lets you set the number of seconds that a task can run until it is killed. This is nice to make sure that a run-away task won't take down all your … -
Building a Django App Server with Chef: Part 4
Alternate title: There's no place like home! This is Part 4, the final part, of my Chef tutorial. Today we're talking about the odds and ends left over to make the server nice to use. You can check out the first 3 parts of the series: Part 1: Chef Beginnings Part 2: Python environment buildout Part 3: Deployment Today's code will be in the git repo under the tag blog-post-4. What we'll need So we have our app server up and running, and ready for traffic. Now we just need to add some other bits around the outside for it to be fully functioning and nice to use. Monitoring with Munin Background tasks with Celery A firewall for security A /etc/hosts file for talking with other nodes A .bash_profile file so that when you shell in you'll have a nice environment Let's get started. Monitoring with Munin For doing monitoring with munin, we're going to need to learn our final Chef concept, which is Templates. You should be pretty familiar with them already, except they use Erb, which is a template language that lets you embed Ruby. We're only going to be configuring the Munin node here. This assumes that … -
Building a Django App Server with Chef: Part 1
Alternate title: Fucking Chef, How does it work? When I started looking at Chef, I found it incredibly complex and lacking in fundamental documentation. This is going to be my experience understanding Chef while setting up a single server. This strategy can be used across multiple servers, with a little tweaking. I'd like to thank Jacob for the ideas and some of the inspiration behind the code and ideas. The code for this blog post can be found on github. It will be expanded as I write updates. Today's code will be using tag blog-post-1 in the repo. Goal I'm hoping to write a blog series that goes from explaining what Chef is, to having a working Django server, and to release all this code so that you can tweak and use it with your own servers. I'll be doing this to set up a new and configuration managed server for ReadTheDocs. There are a couple of other good chef intros available. However, they only get you to the super basic first step of setting up the server. Hopefully this series will be more in depth and useful to actually getting a real server running under Chef. Basic terminology Chef …