Services Blog Français

Testing django signals properly

| by jpic | python django

Asserting that signals were sent properly can be a bit tricky: they must be sent in a particular order with specific arguments.

Fortunately, mock-django provides plenty of cool features, including a nice signal receiver mock. The only problem with mock-django is that it’s not documented. So I had to reverse-engineer and here’s what I’ve found: the best way to test a signal is to use the call_args_list property of the receiver mock.

Read More

Handling relations between ZODB persistent objects, presentation of SubstanceD's pattern

| by jpic

ZODB is an object oriented database for Python, or “NoSQL” database. In one sentence it resumes as such:

Don’t squeeze your objects into tables: store them in an object database.

It takes normal Python objects and make them persistent. ZODB has been there for more than ten years, it’s mature, more than the new “NoSQL” stuff like CouchDB, MongoDB, etc, etc … To be precise, it’s been doing NoSQL ten years before NoSQL even existed !

Read More

Surviving django.contrib.staticfiles (or: how to manage static files with django)

| by jpic | django best-practice

This article describes how to use django.contrib.staticfiles as this is a FAQ StackOverflow and IRC.

Structure

The HTTP server is to serve static files directly from a public folder, without going through Django at all.

Uploaded files

If the HTTP server configuration serves /srv/project/media_root/ on url /media_url/, for example with this nginx configuration:

    location ^~ /media_url/ {
        alias /srv/project/media_root/;
        autoindex off;
    }

Then:

  • settings.MEDIA_ROOT should be /srv/project/media_root/
  • settings.MEDIA_URL should be /media_url/

/srv/project/media_root/ is managed by Django’s FileField and ImageField file system storage, you should not add files there by yourself.

Read More

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',
        },
Previous Page 31 of 33 Next Page

They trust us

Contact

logo