Michael Abrahamsen

Jan 23, 2014

Creating something from nothing

I recently started using Less Css with Django, after using it briefly I found that I did not want to manually compile Less after making changes during development. Using this approach I am able to compile Less client-side during development and have django-compressor compile server-side on live site.

1. Install less via your package manager of choice:

# Using arch linux
sudo pacman -S npm
sudo npm install -g less

# Other distribution
sudo apt-get install npm
sudo npm install -g less

2. Install django-compressor app:

pip install django-compressor

3. Add compressor as installed app:

INSTALLED_APPS = ( 
# other installed apps 
'compressor',

4. Specify less as a precompiler:

COMPRESS_PRECOMPILERS = (
    ('text/less', 'lessc {infile} {outfile}'),
)

5. Add compressor as a static file finder:

STATICFILES_FINDERS = (
# other static files finder
'compressor.finders.CompressorFinder',
)

6. Compiling less

There are many ways to set this up. A simple, yet effective way is to compile less client-side when debugging and server-side during production. To do this we need to do the following:

  1. Download less js file from the Less Website
  2. Create a theme.less (or other name) file
  3. Now your base.html file will be setup something like this:
    {% load staticfiles %}
    {% load compress %}
    <!DOCTYPE html>
    <html>
    <head>
        {% if debug %}
        <link rel="stylesheet" type="text/less" media="all" href="{% static 'less/theme.less' %}" />
        <script src="{% static 'js/less-1.6.1.min.js' %}"></script>
        {% else %}
        {% compress css %}
        <link rel="stylesheet" type="text/less" media="all" href="{% static 'less/theme.less' %}" />
        {% endcompress %}
        {% endif %}
    </head>
    
posted at 21:50  ·   ·  less  django  css

Sep 24, 2013

Django south tables

I was recently adding a slug field to a model in a Django application when I ran into this error when doing the South migration:

Error in migration: test_app:0002_auto__add_field_category_slug
DatabaseError: table "_south_new_test_app_category" already exists

When South makes changes to an existing table, it creates a new table with a temporary name. The table is named "_south_new_" + table_name. In this case, the new table name is _south_new_test_app_category. Assuming the changes are successful, the old table is deleted and replaced with the new table.

However, the previous migration failed and the temporary table still existed. To fix this, we need to drop the temporary table and redo the migration. If you are not familiar with dropping a table, you can run the sqlclear command to get a list of the drop table sql statements for an app. For this test app it looks like this:

> python manage.py sqlclear test_app

BEGIN;
DROP TABLE "test_app_person";
DROP TABLE "test_app_category";
DROP TABLE "test_app_organization";
DROP TABLE "_south_new_test_app_category";

COMMIT;

Using the syntax you see from sqlclear you can run the dbshell command and drop temporary table.

> python manage.py dbshell

sqlite> DROP TABLE "_south_new_test_app_category";

Now as long as you fixed the issue with the migration that failed, you can run the migration again and it will work as planned.