Login

All snippets

Snippet List

Django cycle tag that doesn't continue where it left off

This tag is equivalent to {% cycle %} but resets when we exit the containing loop. See Django ticket "Cycle tag should reset after it steps out of scope" [https://code.djangoproject.com/ticket/5908](https://code.djangoproject.com/ticket/5908) This code is a lightly modified version of Simon Litchfield's attachment on that ticket.

  • forloop
  • cycle
Read More

Pagination helper

You have pagination for some blog. At the bottom of the page you have space for seven page links. This module tries to always fill out those slots. It handles the corner cases for first page and last page etc. The logic for this is complex and sadly the Django pagination module does not deal with this. Also, it's a hell to implement this in Django templates. Believe me. See pydoc for detailed documentation. Running tests was done with `doctest`.

  • pagination
Read More

Automatic Memoization Decorator

This decorator will memoize the results of instance methods, similar to `django.util.functional.memoize`, but automatically creates a cache attached to the object (and therefore shares the life span of the object) rather than requiring you to provide your own. Note this is intended for instance methods only, though it may work in some cases with functions and class methods with at least one argument. This is useful for memozing results of model methods for the life of a request. For example: class MyModel(models.Model): #Fields here @auto_memoize def some_calculation(self): #some calculation here return result

  • cache
  • memoize
Read More

MongoDB data load

This snippet loads data from JSON files into a MongoDB database. The code is related with the other snippet [MongoDB data dump](http://djangosnippets.org/snippets/2872/). To get it working, just create a ``MONGODB_NAME`` variable in settings, holding the name of your Mongo database. This can be edited to fit more your needs. The snippet requires ``Pymongo``, since it uses its bson module and the ``MongoClient``.

  • loaddata
  • fixtures
  • mongodb
Read More

MongoDB data dump

This Django management command just dumps data from a given MongoDB collection into a JSON file. To get it working, just create a ``MONGODB_NAME`` variable in settings, holding the name of your Mongo database. This can be edited to fit more your needs. The snippet requires Pymongo, since it uses its ``bson`` module and the ``MongoClient``.

  • dump
  • fixtures
  • mongodb
Read More

Link If templatetag

Django templatetag wraps everything between ``{% linkif %}`` and ``{% endlinkif %}`` inside a ``link`` if ``link`` is not False. Sample usage:: {% linkif "http://example.com" %}link{% endlinkif %} {% linkif object.url %}link only if object has an url{% endlinkif %}

  • link
Read More

Drag and drop ordering of admin list elements for Grappelli [v2]

Adds drag-and-drop ordering of rows in the admin list view for Grappelli. This is a updated version of Snippet [#2306](http://djangosnippets.org/snippets/2306/) that works with the current version of Grappelli. The model needs to have a field holding the position and that field has to be made list_editable in the ModelAdmin. The changes of the ordering are applied after clicking 'Save'.

  • django
  • sorting
  • django-admin
  • sortable
  • grappelli
Read More

JSON decorator for views handling ajax requests

Sample usage for using decorator `@json_response(ajax_required=True, login_required=True)` `def subscribe(request):` ` return {"status":"success"}` Converts a function returning dict into json response. Does is_ajax check and user authenticated check if set in flags. When function returns HttpResponse does nothing.

  • ajax
  • json
  • decorator
  • jsonp
Read More

Generic CSV export admin action factory with relationship spanning fields and labels

Based on [#2712](../2712/) "This snippet creates a simple generic export to csv action that you can specify the fields you want exported and the labels used in the header row for each field. It expands on #2020 by using list comprehensions instead of sets so that you also control the order of the fields as well." The additions here allow you to span foreign keys in the list of field names, and you can also reference callables.

  • admin
  • generic
  • export
  • csv
  • admin-actions
  • export-csv
Read More

Spaceless all HTML pages

This middleware remove all space between tags and line break of all HTML pages. Use a standard Django method. Set *force_spaceless* for dev. purpose.

  • middleware
  • optimization
  • spaceless
Read More

3109 snippets posted so far.