Login

Top-rated snippets

Snippet List

Execute a signal once

Decorates signals for executing only one time Exemple usage : from django.core.mail import EmailMultiAlternatives from django.template.loader import render_to_string from django.contrib.auth.models import User @one def user_welcome(sender, instance, created, **kwargs): # Send a welcome email if created == True and isinstance(instance, User): instance.message_set.create(message=_(u"Ho, Welcome %s!" % instance)) subject, from_email, to = 'Welcome !', '[email protected]', instance.email text_content = render_to_string('mail/welcome.html', { 'user': instance }) msg = EmailMultiAlternatives(subject, text_content, from_email, [to]) msg.send()

  • decorator
  • signals
  • once
Read More

JSONField

This is a custom field that lets you easily store JSON data in one of your model fields. This is updated to work with Django 1.1. **Example: (models.py)** from django.db import models import JSONField class MyModel(models.Model): info = JSONField() ** Example: (shell)** >>> obj = MyModel.objects.all()[0] >>> type(obj.info) <type 'NoneType'> >>> obj.info = {"test": [1, 2, 3]} >>> obj.save() **[Code at GitHub](http://github.com/bradjasper/django-jsonfield/tree/master)**

  • models
  • fields
  • model
  • json
  • db
  • field
  • json-field
  • jsonfield
Read More

Changing the locale temporary

This piece of code allows the quickly set the locale to a different language. This can be used for instance in cron jobs to send emails to users in their specific language. This is pretty rough code, it be better to create an object, having two functions: set_locale and reset_locale. set_locale would than contain steps 1 to 4 (and return the request object on succes), reset_locale would be step 6 Enjoy!

  • django
  • locale
Read More

DiggBarMiddleware

give diggbar users something to think about (http://daringfireball.net/2009/04/how_to_block_the_diggbar)

  • middleware
Read More

RequiredNullBooleanField

In our situation, we want the user to choose either yes or no. The only requirement is that they fill out the form. It is _not_ required that they answer True/Yes. The BooleanField treats None and False as False; The NullBooleanField distinguishes between None and False, but it doesn't raise any validation errors. Subclassing the NullBooleanField was better than overriding the clean method on all of our NullBooleanField instances.

  • required
  • boolean
  • bool
  • boolean-field
  • null-boolean-field
Read More

Significant digits filter

Formats float values with specified number of significant digits (defaults to 3). Usage: `{{value|sigdig}} # with 3 significant digits by default` `{{value|sigdig:digits}}` Examples: `{{0.001432143|sigdig}}` renders as `0.00143` `{{874321.4327184|sigdig}}` renders as `874000` `{{874321.4327184|sigdig:5}}` renders as `874320` Useful for scientific or engineering presentation.

  • filter
  • filters
  • math
  • engineering
Read More

Language-aware template inclusion

Looks up for a template based on the template-name plus the current users language code. Loads the template and renders it with the current context. Example:: {% langinclude "foo/some_include.html" %} Based on the users LANGUAGE_CODE, assumed we have 'de', it tries to render the template 'foo/some_include.html.de'. If that doesn't exists, it renders the template 'foo/some_include.html'. This is the default behavior of the include-Tag. Basically this is a shortcut for the following code, just with a fallback for the default template:: {% ifequal LANGUAGE_CODE "de" %} {% include "foo/some_include.html.de" %} {% else %} {% include "foo/some_include.html" %} {% endifequal %} --- Ein deutscher [Weblogeintrag mit Beschreibung](http://www.mahner.org/weblog/sprachabhangige-template-imports/)

  • templatetag
  • i18n
  • language
  • include
Read More

MAC address field

Supported MAC formats: aa:bb:cc:dd:ee:ff, separator : or - aabbccddeeff both lower case and upper case

  • model
  • field
  • custom-field
  • address
  • mac
Read More

Decorator to ease dynamic redirections in Django

I often use it so I hope it helps! Usage: urlpatterns = patterns('', url(r'^nav/$', redirect('/navigation/')), url(r'^search/(?P<category_name>\w+)/$', redirect('/documents/%(category_name)s/')), and so on... ) It keeps GET arguments too.

  • dynamic
  • decorator
  • redirect
Read More

3110 snippets posted so far.