Login

All snippets

Snippet List

create_template_postgis-ubuntu_lucid

The template creation script referenced [here] (http://docs.djangoproject.com/en/dev/ref/contrib/gis/install/#ubuntudebian) doesn't work on Ubuntu Lucid, where the default PostgreSQL version is now 8.4 and some things have been moved around. I've edited the script to work on Ubuntu Lucid.

  • postgis
  • ubuntu
Read More

Template tag to create a list from one or more variables and/or literals

This code is taken from a [Stack Overflow answer by Will Hardy](http://stackoverflow.com/questions/3715550/creating-a-list-on-the-fly-in-a-django-template/3715794#3715794). Usage: `{% collect var1 var2 'foo' 'bar' 5 as some_list %}`. Sometimes one wishes to create a list on the fly within a template. Perhaps a collection needs to be passed to a template filter, but the collection cannot be created in the view since the values of one or more of its items are set in the template. A contrived example: {% with 5 as max %}{% with posts|length as len %} {% for post in posts %} {% if forloop.counter <= max %} {% include 'excerpt.dhtml' %} {% endif %} {% endfor %} {% collect len max as limits %} <p>Displaying {{ limits|minimum }} of {{ len }} post{{ posts|pluralize }}.</p> {% endwith %}{% endwith %} The final line will state how many posts are displayed: something like "5 of 24" or "2 of 2". This particular problem can be solved in a number of other ways, some of which are more appropriate. Having a template tag that can create lists on the fly is potentially useful in quite a few situations, though. I don't know whether this need is common enough to warrant being in the core. If something like this is to be included one day, it'd be much nicer to overload the `with` tag than to introduce a new tag. `{% with var1 var2 var3 as some_list %}` reads well.

  • template-tags
Read More

ForeignKey filterspec

Unfortunately, it is not possible currently to use foreign keys in list filter of the admin website. list_filter=['city__country'] doesn't work. This filter spec tries to workaround this problem. It is also possible to have 2 filters for a foreign-key field but it requires to add a dummy field to the model. Set the fk_filterspec dictionnary on this dummy field and add 'fk':'real-field' to the dict.

  • foreignkey
  • django-admin
  • filterspec
Read More

Multiple cache backend

Sometimes you might find useful to cache a value in different backends. Just put this code in a file named "multicache.py" somewhere in your python path and set it in CACHE_BACKEND setting: CACHE_BACKEND = 'multicache://?fallback=1&backends=file:///var/tmp/django_cache,locmem://' Separate the backends settings with commas, the first one will be set as default. Setting fallback to 1 provides fallback to default backend.

Read More

Django code generator

django-app-generator takes as an input your project (ORM) and generates repetitive code. At this moment there is only one example a simple RESTservice, but goal is to build alternative implementation of admin panel that will be fully customizable. The project is in the beta stage, if you encounter any problems feel free to create an issue.

  • Admin
  • REST
  • code-generator
Read More

ReportBug() (exception emails - ala django debug style)

ReportBug() allows you to send exception details to you, via email, but with far more detail than the default. It uses the base function for the traceback used by the Debug mode on Django. This is a first revision, so the emails have no decent styling, but it works, and shows scope on each stack. It will automatically generate a random serial number per error, so you can track them in your favourite bug tracker. It also has support for you to pass it a request variable, so the mail would also contain request/response context. Again, i'm gonna look into doing this manually in the future. Hope this helps! Mwah. Cal Leeming. cal [at] simplicitymedialtd.co.uk. Simplicity Media Ltd.

  • email
  • debug
  • mail
  • exception
  • report
  • bug
  • mail_admins
  • reportbug
Read More

ModelValue for djagno-livesettings

Extension for django-livesettings project - http://bitbucket.org/bkroeze/django-livesettings/ Allow to specify the model instance in settings Usage: config_register( ModelValue(BASE_GROUP, 'TestValue', queryset = Value.objects.all(), required=False)

  • livesettings
Read More

Parse custom template tag's args or kwargs

Enhanced version of snippet [1113](http://djangosnippets.org/snippets/1113/) Usage example (not self-contained): @register.tag def groupurl(parser, token): ''' Syntax:: {% groupurl view_name group [key=val, [key=val, ...]] [as varname] %} Example:: <a href="{% groupurl blog_detail group slug=blog.slug %}">{{ blog.name }}</a> ''' bits = token.contents.split() tag_name = bits[0] if len(bits) < 3: raise template.TemplateSyntaxError("'%s' takes tag at least 2 arguments (path to a view and a group)" % (tag_name,) bits_iter = iter(bits[1:]) # view_name + group and url kwargs args, kwargs = parse_args_and_kwargs(parser, bits_iter, stop_test='as', tagname=tag_name) if len(args) != 2: raise template.TemplateSyntaxError("'%s' takes exactly two non-kwargs (path to a view and a group)" % (tag_name,)) view_name, group = args # as var asvar = None for bit in bits_iter: asvar = bit return GroupURLNode(view_name, group, kwargs, asvar)

  • tag
  • templatetag
  • parse
  • args
  • kwargs
Read More

One step up from __icontains

The [REGEX and IREGEX](http://docs.djangoproject.com/en/dev/ref/models/querysets/#iregex) operators were added in Django 1.0 and I'm sure you can think of fancier ways of doing word delimiting and things like that but this was all I needed to make a user-friendly autocomplete search function.

  • ORM
  • QuerySet
Read More

Access transparently the user profile information

Splitting the information about a user across two models (User and UserProfile) is not to everyone's liking. This snippet monkeypatches the User model so that users can access transparently their profile information while still storing the data in two tables under the hood. Thus it is similar to the inheritance approach (http://scottbarnham.com/blog/2008/08/21/extending-the-django-user-model-with-inheritance/) but has the benefit of (a) not requiring a custom authentication backend or middleware and (b) loading the profile instance lazily, so there's no extra overhead if the profile infromation is not accessed. Read the docstrings for more details.

  • auth
  • profie
  • user profile
Read More

Google Account authentication

Use django_openid_auth from https://launchpad.net/django-openid-auth to authenticate your users with their Google Account. This snippet will allow your users having a Google Account address as username to log in using it.

  • authentication
  • openid
  • google-account
Read More

Silk icon tags

This simple template tag can be used to add famfamfam's silk icons easily to any element in your template.

  • templatetags
  • icons
  • silk
Read More

3109 snippets posted so far.