Login

Top-rated snippets

Snippet List

XFN Assist

This code assumes your XFN field is called 'rel' in models. See the last call (addLoadEvent down the bottom) for the two lines requiring modification; otherwise, aside from the Django-styling involved, it can be used anywhere. The accompanying model.py and admin.py information can be found here: [http://www.djangosnippets.org/snippets/1265/](http://www.djangosnippets.org/snippets/1265/). I'm no master programmer, so feedback/comments/criticism is more than welcome. EDIT: Fixed my esoteric dev. comments and removed references to spawning more overlords.

  • xfn
  • microformats
Read More

Links with XFN (and Validation)

Slightly different validation to that of jpwatts; developed with knowledge of that snippet but written up from scratch for use as a fallback for when the Javascript Assist is off. I'm no master programmer, so feedback/comments/criticism is more than welcome. The accompanying Javascript (xfn-admin.js) can be found here: [http://www.djangosnippets.org/snippets/1266/](http://www.djangosnippets.org/snippets/1266/).

  • xfn
  • microformats
Read More

Django Akismet

http://sciyoshi.com/blog/2008/aug/27/using-akismet-djangos-new-comments-framework/

  • akismet
Read More

(Modified/Improved) MultiQuerySet

My modified version of the [MultiQuerySet by mattdw](http://www.djangosnippets.org/snippets/1103/) (see the link for further information). My purpose for this was to enable me to combine multiple different types of querysets together, which could then be iterated on as one object (i.e. like a tumblelog).

  • multiple
  • queryset
  • chain
Read More

Text Highlighting Filter

This filter can be used to wrap <span class='highlight'> around anything you want to highlight in a block of text. For example, if you had 'foo bar foo baz' inside the context variable MYTEXT, you could do {{ MYTEXT|highlight:'foo' }}, and "<span class='highlight'>foo</span> bar <span class='highlight'>foo</span> baz" would be returned. How you style the highlight class is up to you.

  • filter
  • text
  • highlight
Read More

Admin Input Field Character Count via jQuery

Use this code in *change_form.html* in your projects admin templates to add a character counter beside the input field(s) in admin to let users know how many characters they have remaining for a particular input field. The total number of characters allowed is determined by the max_length in your model for the models.CharField you're using this with. This code is designed to add the counter after the input field, but could easily be customized to fit the style of any admin. If the number of characters remaining is 10 or less the background color changes to yellow to visually warn the user. **Usage Examples:** In this example only the input field with id=id_pull_quote will receive the counter: $(document).ready(function() { $("#id_pull_quote").counter(); }); You could also apply the counter to all input fields on a page: $(document).ready(function() { $("form input[@maxlength]").counter(); }); **Note:** *You have to download jQuery to your project and place the appropriate call in order for this to work. The best place to do this is in the extrahead block. I left my call in as an example but your path and file name will probably vary.* Credit for base jQuery code goes to Brad Landis at [bradlis7.com](http://www.bradlis7.com).

  • template
  • javascript
  • admin
  • jquery
  • form
  • input
Read More

Clear nullable foreign keys on delete

Django 1.0 is apparently hard-coded for cascading deletes. I find that I often have nullable foreign keys on models whose records must not be deleted along with those they refer to. I override Model.delete() in an intermediate base class and execute this method to clear out all nullable foreign keys before allowing a delete to proceed.

  • model
  • delete
  • cascade
Read More

Dynamic image generator

This is the complete image_processor.py module, allowing you to add an image containing an arbitrary piece of text. I use this to label the horizontal axis of a skills-matrix report. Credit www.renewtek.com for paying me to do this stuff!

  • image
  • pil
Read More

table with n items per row using custom modulo tag

As a quick, simple way to take a list of items make a table with n items per row I added a custom template filter for modulo of an integer. To make the custom filter first create a "templatetags" directory in your application folder then add an empty file called "__init__.py" in that new directory. Finally add a file "myapp_tags.py" with the above code in it. In the template you load your custom filter with {% load pictures_tags %} and then you can make a table with n elements per row using a simple for loop and in if statement. This works because if ( for_loop.counter modulo n ) is zero ("if not" evaluates to True on zero values), then it makes a new table row. You might note that if the number of items in list is a multiple of n, there will be an empty row at the end... preventing that adds needless complexity so I left that out!

  • filters
  • modulo
  • tables
Read More

Convert a Couchdb document into a Python object

Originally posted by [Leah Culver on her blog](http://leahculver.com/2008/11/25/couchdb-documents-python-objects/). This allows you to convert a CouchDB document into a python object and work on CouchDB fields as if they were properties of a python object.

  • python
  • couchdb
Read More

Add extra form elements in your contib admin

Add extra form elements in your contib admin Install add this in header of base.html <script type="text/javascript" src="/static/jquery-1.2.6.min.js"></script> http://code.google.com/p/jqueryjs/downloads/detail?name=jquery-1.2.6.min.js

  • form
  • extra
  • elements
Read More

FlatPageOverrideMiddleware

This snippet is a piece of middleware that can be used to allow an administrator to create a flatpage that will override an existing URL. Why would you want such a thing? Maybe an admin needs to be able to override individual detail pages of an item list at will and doesn't want to be bothered mucking around with urlconf and restarting apache2. Obviously, it's not the ideal situation for permanent overrides (in that case, you'd be better off writing an exception in the urlconf), but it's good for the temporary, one-off, situations that may arise for whatever reasons.

  • flatpage
  • override
Read More

New Pattern

Now you can use object-oriented URL resolve and include and you can add function cp_before wich will be running before any any cp__* functions in his module

  • url
  • resolving
  • object-oriented
  • newpattern
  • pattern
Read More

Restructuredtext directive for photolouge

Directive for inserting images using photolouge in django. Usage: Just make a .py with this code and import it in some apps models.py Or make a custom formatter with django-template-utils.

  • image
  • insert
  • rst
  • directive
  • photolouge
Read More

3110 snippets posted so far.