Services Blog Français

Never hardcode absolute paths

| by jpic | python django best-practice

This is nonono:

    STATIC_ROOT = '/home/coat/www/site/app/static/'

Never hardcode absolute paths, you’re just making your settings file less portable and probably killing kittens. Adapt this to your needs:

    import os.path
    import posixpath

    PROJECT_ROOT = os.path.abspath(os.path.join(os.path.dirname(__file__), '..')

    STATIC_ROOT = os.path.join(PROJECT_ROOT, 'static')

    # fix STATICFILES_DIRS too
    # and TEMPLATE_DIRS

How to see exception stacktraces with django LiveServerTestCase

| by jpic | django python

After playing with selenium and LiveServerTestCase a little while, you might see this:

    Traceback (most recent call last):
      File "/usr/lib/python2.7/wsgiref/handlers.py", line 85, in run
        self.result = application(self.environ, self.start_response)
      File "/home/jpic/env/local/lib/python2.7/site-packages/django/contrib/staticfiles/handlers.py", line 68, in __call__
        return super(StaticFilesHandler, self).__call__(environ, start_response)
      File "/home/jpic/env/local/lib/python2.7/site-packages/django/core/handlers/wsgi.py", line 241, in __call__
        response = self.get_response(request)
      File "/home/jpic/env/local/lib/python2.7/site-packages/django/contrib/staticfiles/handlers.py", line 63, in get_response
        return super(StaticFilesHandler, self).get_response(request)
      File "/home/jpic/env/local/lib/python2.7/site-packages/django/core/handlers/base.py", line 153, in get_response
        response = self.handle_uncaught_exception(request, resolver, sys.exc_info())
      File "/home/jpic/env/local/lib/python2.7/site-packages/django/core/handlers/base.py", line 228, in handle_uncaught_exception
        return callback(request, **param_dict)
      File "/home/jpic/env/local/lib/python2.7/site-packages/django/utils/decorators.py", line 91, in _wrapped_view
        response = view_func(request, *args, **kwargs)
      File "/home/jpic/env/local/lib/python2.7/site-packages/django/views/defaults.py", line 32, in server_error
        t = loader.get_template(template_name) # You need to create a 500.html template.
      File "/home/jpic/env/local/lib/python2.7/site-packages/django/template/loader.py", line 145, in get_template
        template, origin = find_template(template_name)
      File "/home/jpic/env/local/lib/python2.7/site-packages/django/template/loader.py", line 138, in find_template
        raise TemplateDoesNotExist(name)
    TemplateDoesNotExist: 500.html

Adding templates/500.html won’t give you a stacktrace.

The solution is to add something like this to settings.LOGGERS:

    'handlers': {
        'console':{
            'level':'DEBUG',
            'class':'logging.StreamHandler',
        },
    },
    'loggers': {
        'django.request': {
            'handlers':['console'],
            'propagate': True,
            'level':'DEBUG',
        },

Free services you can use to make a great python app

| by jpic | python

A great django app should have:

  • a source repository
  • an issue tracker
  • documentation
  • release packages
  • a mailing list
  • continuous integration
  • translations

Source code repository and issue tracker

Usually, source code repository services come with an issue tracker. The most common are Github and Bitbucket for mercurial.

You can find other git hosts such as Baregit but they don’t necessarily include an issue tracker like Github or Bitbucket.

Most django apps use github, but also many of them are on bitbucket.

Read More

Dvorak intl code

| by jpic | dvorak-intl-code

After upgrading Ubuntu, I had to redo my custom dvorak layout.

To enable it, the only way I’ve found is to add the layout to /usr/share/X11/xkb/us.

Then you can enable it through a GUI, or with command:

setxkbmap us dvorak-intl-code

It is optimized for multilinguists and hackers:

// Dvorak international with dead keys and optimisations for code
// Olivier Mehani (shtrom-xorg@ssji.net) & James Pic
// Reproduce the per-key mapping of us(intl) for the dvorak layout
// aka "I just swapped my keys over"
// Symbols and numbers swapped for code
// colon and semi colon swapped for vim
partial alphanumeric_keys
xkb_symbols "dvorak-intl-code" {

    name[Group1]= "English (Dvorak international with dead keys)";

    include "us(dvorak)"

    key <tlde> { [dead_grave, dead_tilde,         grave,       asciitilde ] };

    key <ae01> { [     exclam,     1,    exclamdown,      onesuperior ] };
    key <ae02> { [         at,     2,   twosuperior, dead_doubleacute ] };
    key <ae03> { [ numbersign,     3, threesuperior,      dead_macron ] };
    key <ae04> { [     dollar,     4,      currency,         sterling ] };
    key <ae05> { [    percent,     5,      EuroSign,     dead_cedilla ] };
    key <ae06> { [asciicircum,     6,    onequarter,  dead_circumflex ] };
    key <ae07> { [  ampersand,     7,       onehalf,    dead_horn ] };
    key <ae08> { [   asterisk,     8, threequarters,      dead_ogonek ] };
    key <ae09> { [  parenleft,     9, leftsinglequotemark, dead_breve ] };
    key <ae10> { [ parenright,     0, rightsinglequotemark, dead_abovering ] };
    key <ae11> { [ bracketleft,  braceleft,  guillemotleft, leftdoublequotemark ] };
    key <ae12> { [bracketright, braceright, guillemotright, rightdoublequotemark ] };

    key <ad01> { [ apostrophe,        quotedbl, dead_acute, dead_diaeresis ] };
    key <ad02> { [     comma,       less,      ccedilla,         Ccedilla ] };
    key <ad03> { [    period,    greater, dead_abovedot,       dead_caron ] };
    key <ad04> { [     p,          P,    odiaeresis,       Odiaeresis ] };
    key <ad05> { [     y,          Y,    udiaeresis,       Udiaeresis ] };
    // key <ad06> { [      f,   F       ]   };
    // key <ad07> { [      g,   G       ]   };
    key <ad08> { [     c,          C,     copyright,             cent ] };
    key <ad09> { [     r,          R,    registered,       registered ] };
    key <ad10> { [     l,          L,        oslash,         Ooblique ] };
    key <ad11> { [     slash,   question,  questiondown,        dead_hook ] };
    // key <ad12> { [     equal,       plus,      multiply,         division ] };

    key <ac01> { [     a,          A,        aacute,           Aacute ] };
    key <ac02> { [     o,          O,        oacute,           Oacute ] };
    key <ac03> { [     e,          E,        eacute,           Eacute ] };
    key <ac04> { [     u,          U,        uacute,           Uacute ] };
    key <ac05> { [     i,          I,        iacute,           Iacute ] };
    key <ac06> { [     d,          D,           eth,              ETH ] };
    // key <ac07> { [      h,   H       ]   };
    key <ac08> { [     t,          T,         thorn,            THORN ] };
    key <ac09> { [     n,          N,        ntilde,           Ntilde ] };
    key <ac10> { [     s,          S,        ssharp,          section ] };
    // key <ac11> { [     minus, underscore,           yen,    dead_belowdot ] };

    key <ab01> { [      colon, semicolon,     paragraph,           degree ] };
    key <ab02> { [     q,          Q,    adiaeresis,       Adiaeresis ] };
    // key <ab03> { [      j,   J       ]   };
    key <ab04> { [     k,          K,            oe,               OE ] };
    // key <ab05> { [      x,   X       ]   };
    // key <ab06> { [      b,   B       ]   };
    key <ab07> { [     m,          M,            mu,               mu ] };
    key <ab08> { [     w,          W,         aring,            Aring ] };
    // key <ab09> { [      v,   V       ]   };
    key <ab10> { [     z,          Z,            ae,               AE ] };

    key <bksl> { [ backslash,        bar,       notsign,        brokenbar ] };

    include "level3(ralt_switch)"
}; </bksl></ab10></ab09></ab08></ab07></ab06></ab05></ab04></ab03></ab02></ab01></ac11></ac10></ac09></ac08></ac07></ac06></ac05></ac04></ac03></ac02></ac01></ad12></ad11></ad10></ad09></ad08></ad07></ad06></ad05></ad04></ad03></ad02></ad01></ae12></ae11></ae10></ae09></ae08></ae07></ae06></ae05></ae04></ae03></ae02></ae01></tlde>

Automatic virtualenv activation

| by jpic | virtualenv python

This article proposes a proven standard which enables automatic virtualenv activation.

Demonstration

Before, I had to do something like:

    cd myproject
    source ../env/bin/activate

Now I just do:

cd myproject

Virtualenv standard

Say I have a project called projectX, you could expect to find it as such on my servers:

    /srv
        /projectX_prod
        /projectX_test
        /projectX_dev
            /projectX_dev_env -> the virtualenv
            /env -> symlink to projectX_dev_env
            /main -> the checkout of the python project

As you can see, it is easy and consistent. For the record, this is the command I use to create the symlink:

Read More

Django schema migrations: nashvegas vs. South vs. evolution

| by jpic | django migrations

Schema migration consists of altering a database structure. For example, adding a column, an index …

When creating a Django model, running the syncdb command can create the tables. But syncdb doesn’t support schema migrations. So when modifying a Django model, reflecting the changes on to the database is left as an exercice for the sysadmin.

Django-extensions

If you don’t speak fluent SQL, use the sqldiff command from django-extensions. It compares the database structure with the actual models for an app, and output SQL migration code. Installing the sqldiff command is really easy:

Read More
Previous Page 31 of 32 Next Page

They trust us

Contact

logo