Login

Most bookmarked snippets

Snippet List

DualPasswordForm

DualPasswordForm is a simple form that contains two password fields and validation to ensure that the two passwords match. A minimum password length of 7 characters is imposed, but feel free to change that.

  • form
  • password
  • match
  • dual
  • dualpasswordform
Read More

Refactor template files to use {% block %} tags.

Refactors two similar django template files to use `{% block %}` tags. Takes two template files, where one is a modified version of the other, and looks for differences and similiarities between the two. It then generates refactored (and renamed) versions of each file and a third 'base' file, adding an `{% extends %}` tag as appropriate. Block tags are named with matching numbers in all 3 files, making it easy to do a search and replace with more meaningful labels. `sample_data_in_base` controls whether or not the content from file 1 is copied into place in the base file as an example. Problems: it doesn't identify open `{% for %}` or `{% if %}` tags, so this needs some manual fixing (moving the `{% endfor %}` and `{% endif %}` tags into the proper blocks). It doesn't add the correct path for the `{% extends %}` tag either (ie. `"app/base.html"`). `collapse_whitespace` is not working at all.

  • template
  • refactor
Read More

[middleware] Rewrite anchors to point into Coral CDN

This simple middleware rewrites the 'href' attribute of any `<a>` tags in your response content. The URL href is modified by appending the string '.nyud.net', which causes the Coral Content Distribution Network to retrieve a copy of the page and cache it before returning it to the user agent. This might be useful if you're writing another Slashdot and you want to avoid turning the servers you link to into smoking craters. You [should be able](http://www.djangosnippets.org/snippets/910/) to apply this functionality to a single view as well (though I haven't tried this yet).

  • middleware
  • coral
  • cdn
  • anchor-rewrite
Read More

Serializing booleans correctly when doing dumpdata from a MySQL database using Django 0.96

Django 0.96 seems to have a bug when serializing from MySQL. BooleanFields are encoding as 0/1 instead of true/false. Hacking the python serializer seems to fix that. The bug shows up as (fx. when using loaddata on a dump from MySQL in PostgreSQL): Problem installing fixture '/tmp/data.json': ERROR: column "is_staff" is of type boolean but expression is of type integer HINT: You will need to rewrite or cast the expression.

  • dumpdata
  • 0.96
  • seralization
Read More

newforms-admin done the old way

If you view Django's admin pages as a convenience for experts, and so don't see the point in heavily modifying it beyond a few simple things like list_display and some ordering or filtering options, you may feel that newforms-admin makes things harder, not easier. ... Well, there's some truth to that, but it turns out that fighting newforms-admin is doomed - too much has changed to make the fight to preserve the old simplicity winnable to any useful extent, so I'm withdrawing this.

  • newforms-admin
Read More

auto_now using signals

There has been some discussion about removing `auto_now` and `auto_now_add` some time ago. `auto_now_add` can be replaced be using a callable default value, `auto_now` can't. So I wrote this litte function for my current project (older ones still use `auto_add`) to fill the gap...but I'm not sure if `auto_now` will be removed at all.

  • datetime
  • model
  • auto_now
Read More

Search Docs with Shortwave

. . For use with [Shortwave](http://shortwaveapp.com/). Add this to your hosted `wave.txt` for a search of the current Django docs.

  • search
  • documentation
  • shortwave
Read More

Template tag for compressed CSS & JS (GAE version)

Extension of the idea from [WuzHere example from Google IO](http://code.google.com/p/wuzhere/) about creating one compressed js or css file. Original code used not very elegant if statements. I've changed it to template tags. On production server it will also use current version id. Insert code in *cssjs.py* file in *templatetags* dir in your application and use as below (more details in docs): `<script type="text/javascript" src="/media/jsmergefile.js"></script>` **This code does not compress CSS and JS on the fly, because GAE is read-only and using Datastore is too heavy.**

  • templatetag
  • google-app-engine
Read More

3110 snippets posted so far.