Login

Most bookmarked snippets

Snippet List

sublist

*Usage:* `list|sublist:"a:b"` Returns `list[a:b]` Accepts `":b"` and `"a:"` shortcuts Note that the double quotes are necessary

  • filter
  • lists
  • templates
Read More

self-related objects list with links

The problem was to output self-related objects (like category objects which may be a sub-category in any category and so on) in unordered list with links to certain view (providing some object.id arg). Its somewhat like unordered_list tag but takes plain list of objects and provides links. For example: category_list = Category.objects.all() In your template: {% related_linked_list category_list view_category %} This tag, however, have some limits: 1. Model shoud have self-related "parent" and id that may be passed into view. 2. In model parent = ForeignKey('self', ...) *related_name* is "child_set". But its simple enough that you may easily rewrite it for your own needs. :)

  • template-tags
  • output
Read More

divShareApi

A python module for integration with http://www.divshare.com/integrate/api.

  • files
  • api
  • media
  • photos
  • divshare
  • video
Read More

HTML to POST dict converter

If you want to test a post request to a form, you need to give all input fields, even if you just want to test if one value gets changed. This snippets parses the HTML of a view and gives you a dictionary that you can give to django.test.Client.

  • testing
Read More

A simple rest template filter

**Attention! This snippet must be ignored**, like [zgoda](http://www.djangosnippets.org/users/zgoda/) pointed with reason: already exists this functionality in `markup` contrib. **Explanations:** This template filter allows you to render easily a reStructuredText to **HTML** or another format it supports. **Setup:** Insert the snippet into an_app/templatetags/restutils.py. **Use in template:** `{% load restutils %}` and use it as following: - `{{ entry.content|rest:"html" }}`

  • rest
  • template-filters
Read More

Partial JSON template rendering

this decorator will render template and encode it as JSON string if it find **ajax_request** variable in GET. It will not parse parent (extended) templates, only given template and nested (included). Only blocks will be rendered and encoded to JSON naming block__*BLOCKNAME*. Simple javascript can do ajax request adding *?ajax_request* or *&ajax_request* (if needed) and update given html elements if they id's are same as block__*BLOCKNAME*. you can find a full code (including javascript) at my (http://projects.barbuza.info/trac/browser/django_addons/django_ajax/trunk/ sandbox).

  • ajax
Read More

PyCrust Shell

I thought it would be a neat idea to use the PyCrust shell for interacting with my models, instead of using the plain old shell. So what I did, is take a copy of the file: django/core/management/commands/shell.py Altered it, and saved it as: django/core/management/commands/pycrust.py For this to work, you should have pycrust installed (python-wxtools in ubuntu) and save this snippet as django/core/management/commands/pycrust.py, then run as follows: ./manage.py pycrust instead of ./manage.py shell Have fun!

  • pycrust
  • shell
Read More

Template tag to sort a list of links

Sorts a list of HTML anchor tags based on the anchor's contents. This is useful, for example, when combining a static list of links with a dynamic list that needs to be sorted alphabetically. It ignores all attributes of the HTML anchor. {% load anchorsort %} {% anchorsort %} <a href="afoo.jpg">Trip to Fiji</a> <a href="bfoo.jpg">Doe, a deer, a female deer!</a> {% for link in links %} <a class="extra" href="{{ link.href|escape }}">{{ link.name|escape }}</a> {% endfor %} {% endanchorsort %} Note that case (capital vs. lower) is ignored. Any HTMl within the node itself **will not be removed**, so sorting `<a>Bar</a><a><strong>Foo</strong><a/>` will sort as `<a><strong>Foo</strong></a><a>Bar</a>` because `<` is logically less than `b`.

  • template
  • sort
  • anchor
  • a
Read More

MetaOptions

A class called MetaOptions that enables decoration of Django models with meta classes in similar style to Admin and Meta. Included is an example usage to enable CSV export of any set of models. The package installs into django.contrib.options and is available for download at the [Python Cheeseshop](http://cheeseshop.python.org/pypi/django_options/r6)

  • csv
  • options
  • meta
Read More

Make a string a fixed-width

This snippet is a template filter based on ["truncate letters"](http://www.djangosnippets.org/snippets/126/) only it will either truncate or pad a string to ensure the output is always a set width.

  • text
  • fixed-width
Read More

run "silent" dev server

This will hide the scrolling output from the development server so you can ssh and run the server in peace, for those of us with only one SSH session active developing on a remote server.

  • bash
  • runserver
  • daemon
Read More

Forcing unit test runner to abort after failed test

Sometimes running a test suite can take a long time. This can get especially annoying when you see a failure or error, but have to wait for all the remaining tests to run before you can see the relevant stack traces. Pressing ctrl+c to interrupt the tests does not work; the KeyboardInterrupt exception does not get caught and only a stack trace irrelevant to your tests is produced. This behavior can be altered through overriding the testcase's `run` method and catching the exception yourself. Now you can stop the tests whenever you want to and still see what is wrong with your tests.

  • unit-test
Read More

several_random template filter

Allows the selection of one or more items from a list. The built-int `random` filter only allows you to select a single at a time, and repeated use can return the same item many times. **Example:** `{% for random_item in item_list|several_random:3 %} ... {% endfor%}` **Note:** If you're running this on an uncached QuerySet, it can result in many database queries... a future improvement might check to see if the passed object is a QuerySet and act accordingly.

  • filter
Read More

User or Group entry field & widget

This is a helper field & widget that lets you ask for a 'user or group'. It presents a select box for whether you wish to enter a user or a group, and then a text field for the name of the user or group you wish to have. It will validate that the username or group name selected exists. You would use this like any other field: ` class AddForumCollectionCreatePermForm(forms.Form): user_or_group = UserOrGroupField(label = "User or Group", help_text = "The user or group to grant " "forum collection create priveleges to.") `

  • newforms
Read More

Roman Numeral Filter

Template filter to convert integer or long integer into roman numeral with support for upper and lower case numerals. Requires python-roman package on Debian which apparently comes from http://www.diveintopython.org/

  • django
  • python
  • roman-numerals
Read More

3110 snippets posted so far.