Filed under IT – Django

Python-celery

This document describes Celery 3.0. For development docs, go here. First steps with Django Configuring your Django project to use Celery You need four simple steps to use celery with your Django project. Install the django-celery library: $ pip install django-celery Add the following lines to settings.py: import djcelery djcelery.setup_loader() Add djcelery to INSTALLED_APPS. Create the celery database tables. If you are … Continue reading

Django utility scripts

is a Web framework, but that doesn’t mean you can’t interact with it outside of a browser. In fact, one of the great things about Django being written in Python, as opposed to a Web-specific language, such as ColdFusion or PHP, is that it’s designed for use in a command-line environment. You can have periodic … Continue reading

Django template tag

import os import random import posixpath from django import template from django.conf import settings register = template.Library() def files(path, types=[“.jpg”, “.jpeg”, “.png”, “.gif”]): fullpath = os.path.join(settings.MEDIA_ROOT, path) return [f for f in os.listdir(fullpath) if os.path.splitext(f)[1] in types] @register.simple_tag def random_image(path): pick = random.choice(files(path)) return posixpath.join(settings.MEDIA_URL, path, pick)

Django overrides data manager

Enhancing Django’s ORM with Custom Managers Though Django’s ORM system is not designed as a complete replacement for SQL, it’s more than sufficient to power many Web applications. You’ve already learned how to supplement Django ORM queries by passing raw SQL commands via the extra method. However, custom managers give you another way. You’ve already … Continue reading

Django data manager

Post.objects is a manager object. It’s an instance of a class that inherits from models.Manager, and the methods of this class determine what you can do with a queryset—filtering, in the case of this example.

Python optimalsation tip

if you are generating bits of a string sequentially instead of: s = “” for x in list: s += some_function(x) use slist = [some_function(elt) for elt in somelist] s = “”.join(slist) Avoid: out = “<html>” + head + prologue + query + tail + “</html>” Instead, use out = “<html>%s%s%s%s</html>” % (head, prologue, query, … Continue reading