Login

All snippets

Snippet List

Showell markup--DRY up your templates

The code shown implements a preprocessor for Django templates to support indentation-based syntax. The pre-markup language is called Showell Markup. It allows you to remove lots of close tags and random punctuation. It also has a syntax for cleaning up individual lines of HTML with a pipe syntax for clearly separating content from markup. You can read the docstrings to glean the interface. Here are examples: >> table >> tr >> td Right >> td Center >> td Left >> div class="spinnable" >> ul >> li id="item1" One >> li id="item2" Two %% extends 'base.html' %% load smartif %% block body %% for book in books {{ book }} %% if condition Display this %% elif condition Display that %% else %% include 'other.html' >> tr class="header_row" Original Author | th class="first_column" Commenters | th Title | th Action | th Last words | th By | th When | th >> ol class="boring_list" One | li Two | li Three | li {{ element4|join:',' }} | li Hello World! | b | span class="greeting" ; br Goodbye World! | i | span class="parting_words"; br ; hr >> p class="praise" Indentation-based syntax is so Pythonic! Archive LINK collection.archive referral.pk Home LINK home.home Friends LINK friendship.friends Create a card LINK referrals.create Recent changes LINK activity.recent_changes >> FORM referrals.create {{ form.as_p }} {{ referral.id } | HIDDEN referral

  • templates
  • dry
  • preprocessor
Read More

Accordion changelist admin

Allows you to hide the filters in the pages of lists of admin. Very useful when filters hide one or more columns. Add this code after the block "extrastyle"

  • admin
  • changelist
Read More

tag: render form field

this solves a common problem where you want to specify html tag attributes for form fields in the template itself and not have to do it by writing a custom form class. eg. the size of the field, css classes, tabindex etc. usage: {% render_field form.comments "cols=40,rows=5,class=text,tabindex=2" %} where form.comments is a form field with a text area widget it will show data (if the form is bound or if there is initial data) and will display errors if the field has errors

  • forms
Read More

reorder apps in admin index

A quick & dirty way of sorting the apps on the admin index page however you want. It requires you to override the default admin/index.html template. The instructions are in the docstring, they assume you insert the code above in a templatetag file called admin_app_order.py

  • admin
  • reorder
Read More

safe(r) monkeypatching scheme for django testing

In test code, it is sometimes useful to monkeypatch a Django method to have stubbed out behavior, so that you can simplify data setup. Even with decent data setup you might want to avoid execution of Django code that is not the target of your test. The code snippet shown here illustrates a technique to limit the scope of your monkeypatches. It uses the Python "with" statement, which was introduced in 2.5. [with statement](http://effbot.org/zone/python-with-statement.htm) The key aspect of the "with" machinery is that you can set up an __exit__ method that gets called even if the code inside the "with" raises an exception. This guarantees that your monkeypatch gets un-monkeyed before any other code gets called. I don't recommend monkeypatches in production, but if you HAVE to resort to a monkeypatch, I definitely advise using "with" to limit their scope. The examples on the left illustrate how to suppress versions of reverse() and timesince()--look at the import statements to see which ones I am talking about. Obviously, monkeypatching is not for the faint of the heart, as you need to be able to find the code to monkeypatch in Django source, and you need to be sure there aren't decorators at play.

  • testing
  • monkeypatch
Read More

Build tags files for emacs and vim

Save this shell script to the root of your Django project as "tags.sh", make it executable with "chmod +x tags.sh", and run it from the project root with "./tags.sh", and you will have a "tags" file for vim and a "TAGS" file for emacs. Tags will be created for Python, JavaScript, and block names found in HTML templates. You may need to change "/usr/share/pyshared/django" to the location of your Django installation. Documentation on [Tags in emacs](http://www.gnu.org/software/emacs/manual/html_node/emacs/Tags.html) | [Tags in vim](http://vimdoc.sourceforge.net/htmldoc/tagsrch.html)

  • tags
  • emacs
  • vim
Read More

django paginator

This a basic pagination example. It shows new 5 pnews items. We added a code to our template so we can view previous or next pages. It also show us how many pages we have.

  • django
  • paginator
Read More

Load initial form fields from GET parameters

It is often convenient to be able to specify form field defaults via GET parameters, such as /contact-us/?reason=Sales (where "reason" is the name of a form field for which we want a default value of "Sales"). This snippet shows how to set a form's initial field values from matching GET parameters while safely ignoring unexpected parameters.

  • forms
Read More

Increase maximum number of changelist items for "Show all" link to appear

By default, a "Show all" link will appear in the changelist pager only if fewer than 200 records are in the result. Since it is rare that there will be more than one page of records yet fewer than 200, the "Show all" link almost never shows up. Pasting this code somewhere in your app will allow you to increase the 200-record maximum. "Show all" is very handy when used in combination with batch actions and filters, and this change will enable it for most situations. Note that this allows a changelist with up to 10,000 results, which results in a lot of HTML that can tax slower browsers and older machines. For me, it has been worth the tradeoff, since my users have fast enough computers and need to be able to make batch changes efficiently.

  • admin
  • changelist
Read More

Git media cache busting tag

This tag appends the current git revision as a GET parameter to a media files so the web server can set an expires header far in the future. Your project must be structured such that MEDIA_ROOT/../.git exists. Usage: `<link rel="stylesheet" type="text/css" href="{% media myapp/css/base.css %}">`

  • cache
  • media
  • git
Read More

Past days template filter

Returns a list of date objects for a given number of past days, including today. Useful for summaries of recent history. Inspired by [Template range filter](http://www.djangosnippets.org/snippets/1357/)

  • template
  • filter
  • date
Read More

Dom ID

Helper for identifing records in views similiar to rails dom_id helper.

  • templatetag
  • templatefilter
  • dom_id
Read More

ImageField for Google App Engine

This is a replacement for Django's built-in ImageField. It uses the Google AppEngine image APIs in order to validate. Notes: 1. Validation of the field counts against your App Engine transformations quota. 2. This code assumes you're only using the in-memory file upload handler. None of the other stock handlers work well on App Engine; you should probably disable them.

  • fields
  • imagefield
  • google
  • appengine
  • gae
Read More

3109 snippets posted so far.