Login

Most bookmarked snippets

Snippet List

JSON decode datetime

If you have JSON objects with `datetime` attributes that you want to decode to python [datetime](http://docs.python.org/library/datetime.html#datetime.datetime) objects, you can use `decode_datetime` as a [simplejson](http://simplejson.googlecode.com/svn/tags/simplejson-2.0.9/docs/index.html) object hook. `simplejson.loads(s, object_hook=decode_datetime)`.

  • datetime
  • json
Read More

JSON encode ISO UTC datetime

If you want to do your own JSON serialization of [datetime](http://docs.python.org/library/datetime.html#datetime.datetime) objects instead of using DjangoJSONEncoder, use `simplejson.dumps(o, default=encode_datetime)`. The `encode_datetime` method will convert the datetime object to UTC and output an ISO format string just like the [isoutc template filter](http://www.djangosnippets.org/snippets/1424/).

  • datetime
  • json
  • utc
Read More

HTML/JS template filter to show localized dates/times

While working on my website projects today I had the idea to use HTML/JS instead of a IP database to localize the dates and times shown on the websites. Here are the snippets to use the JS snippets as filters for Django running on Google App Engine. You can use those filters on datetime objects.

  • template
  • filter
  • django
  • datetime
  • date
  • time
  • appengine
  • local
Read More

Deli.cio.us rss template tag

This snipped get the latest rss links from delicious from a DELICIOUS_USER that you have to define in your settings. I use this snippet in the [trespams code blog](http://trespams.com) to inform the visitor about the last links I have bookmarked. This is a modified version of [snipped 819](http://www.djangosnippets.org/snippets/819/) to allow using any variable in the template to obtain the results. It also added a 6 hours caché for the rss.

  • delicious
Read More

Built-in Slugify with filtering.

This is based on snippets 29 and 43, both of which had good ideas, and I thought, "why not combine them?" Given the new_topic string above, the resulting slug will be: "test-long-string-which-has-many-words-here" John Crawford Note - requires python 2.5, for list comprehensions. Otherwise you could just use a for loop to build the clean_list.

  • django
  • slugify
Read More

Script factory for monitoring django-sphinx with Nagios

This snippet is used to create a script for monitoring sphinx status with Nagios via [django-sphinx](http://code.google.com/p/django-sphinx/). It returns 0 (OK) or 2 (CRITICAL). Remember to change this strings `ModelToMonitor` and `app_name`. Usage : `./manage your-controls-command --log` > /your/script/name.py

  • sphinx
  • script
  • management
  • nagios
  • monitoring
  • django-sphinx
  • factory
Read More

soaplib service integration 2

Based on our [first version of soaplib service integration](http://www.djangosnippets.org/snippets/979/), this second one adds Basic Auth with credentials specified in settings. It can be tested with [django soaplib test cliente](http://www.djangosnippets.org/snippets/1406/)

  • soap
  • soaplib
  • wsdl
Read More

django soaplib test client

This code *monkey patches* soaplib client to allow the usage of django test client for local web service testing (without a running server). It adds *basic* authentication.

  • soap
  • soaplib
  • test-client
Read More

simple DomainsAliasMiddleware

Permit to redirect desired domain name to the 'domain' of Site app. Useful if you have different domains name for the same website. #1. Add to your settings DOMAINS_ALIAS like this: DOMAINS_ALIAS = ( 'my-second-domain.com', 'www.my-second-domain.com', 'third-domain.com', 'www.third-domain.com', ) notice: all these domains are redirected to the **domain** db entry of Site ID. #2. add all these domains to ServerAlias directive in your vhost apache configuration. #3. enable the middleware by adding to your MIDDLEWARE_CLASSES: MIDDLEWARE_CLASSES = ( ... 'utils.middleware.domainsalias.DomainsAliasMiddleware', ... )

  • middleware
  • redirect
  • domain
Read More

"Dynamic" form with a hidden field

This code demonstrates two simple techniques: 1. so-called "dynamic" forms, in which the form is created at run time by the model 2. using a widget (forms.widgets.HiddenInput) for a field of the form. I feel like this could be highlighted more in the documentation. You need to do something similar to get a textarea (forms.CharField(widget=forms.widgets.Textarea()) and it took me too long to figure this out. There are, no doubt, good reasons not to do what I'm doing here the way I'm doing it. Peanut gallery?

  • dynamic
  • form
  • field
  • hidden
Read More

Filter to adjust forloop.counter across pages in a paginated view

**Update:** Never mind. See [dc's comment](http://www.djangosnippets.org/snippets/1391/#c1763) below for a much easier way to do this. I recently had to write a template for a paginated view which displayed a serial number for each `object` in the `object_list`. I normally use `forloop.counter` for general purpose serial numbers. However this did not work with paginated views as the counter gets reset in each page. This caused the serial numbers to go from 1 to #-of-results-in-the-page and then repeat. **Assumptions:** The `adjust_for_pagination` filter adjusts the value of `forloop.counter` based on the current page. `Page` and `is_paginated` variables are expected to be present in the context. These should respectively denote the current page number (1 based) and if the results are paginated. `RESULTS_PER_PAGE` is currently taken from the settings file. I couldn't think of a way to pass this value also from the template.

  • filters
  • pagination
  • forloop.counter
Read More

ChoiceField with widget in choice

This code creates a widget that we used to generate a list of radiobuttons as follows: * Radio button 1 --Widget__1 * Radio button 2 --Widget__2 * Radio button 3 --Widget__3 How to use it is: `ImagenForm class (forms.ModelForm): widget1 = forms.BooleanField () widget2 = forms.TextInput widget3 = forms.ModelChoiceField (queryset = Modelo.objects.all ()) widget4 = forms.FileInput optConListas = ChoiceWithOtherField (choices = [(2, 'widget1' widget1), (3, 'widget2' widget3.widget), (4, "widget2" widget2), (5, 'widget4' widget4)])`

  • django
  • choice
  • choicefield
  • widget
  • alfonso
Read More

3110 snippets posted so far.