Login

Top-rated snippets

Snippet List

Validate by file content type and size

A simple way to handle file validation by checking for a proper content_type and by making sure that the file does not go over its limit upload size limit. In the example I am checking to make sure the file is an image or a video file then I check to make sure that the file is below the max upload limit. if conditions are not met, then a validation error is raised

  • forms
  • validation
  • filefield
  • file
Read More

Automatic Manager Choice Filters

Automatically adds filter methods to your objects manager based on their display name. class Foo(models.Model): MOO_CHOICES=((1,'foo'),(2,'bar')) moo = models.IntegerField(choices=MOO_CHOICES) objects = ChoiceFilterManager('moo',MOO_CHOICES) Foo.objects.foo() Foo.objects.bar()

  • managers
  • choices
Read More

Breadcrumbs filter

Filter for a list if breadcrumbs. All necessary code included but obviously only the breadcrumbs method that is needed with necessary changes for esc(). I use it as {% load breadcrumbs %} {{ request.path|breadcrumbs:"" }} enclosed in an unordered html list with id="breadcrumbs"

  • filter
  • breadcrumbs
Read More

DaGood breadcrumbs

Provides two template tags to use in your HTML templates: breadcrumb and breadcrumb_url. The first allows creating of simple url, with the text portion and url portion. Or only unlinked text (as the last item in breadcrumb trail for example). The second, can actually take the named url with arguments! Additionally it takes a title as the first argument. This is a templatetag file that should go into your /templatetags directory. Just change the path of the image in the method **create_crumb** and you are good to go! Don't forget to `{% load breadcrumbs %}` at the top of your html template! [http://drozdyuk.blogspot.com/2009/02/dagood-django-breadcrumbs.html](http://drozdyuk.blogspot.com/2009/02/dagood-django-breadcrumbs.html)

  • template
  • tags
  • navigation
  • breadcrumbs
Read More

Load Windows ICO files

PIL IcoImagePlugin is twelve year old and it can't handle recent Windows ICO files. Here is a function that handles all ICO versions and preserve transparency. Usage: # Load biggest icon from file image = load_icon('icon.ico') # Save third icon as PNG load_icon('icon.ico', 2).save('icon.png')

  • image
  • pil
  • ico
Read More
Author: dc
  • 2
  • 2

Duplicate related objects of model instance

I've got a bunch of `Models` that form a tree like structure. I'd like to duplicate them all changing one field to something else. Say for example I've got a `Website` which has `Pages` and `Links` and all kinds of other `Models`. Each one of these belong to a `User` (through a foreign key relation). I could use `duplicate` to create a copy of an entire website and give it to another `User` with something like this: class Website(Model): owner = ForeignKey('auth.User') ... class Link(Model): owner = ForeignKey('auth.User') ... class Page(Model): owner = ForeignKey('auth.User') ... ################################## website = Website.objects.get(pk=1) new_owner = User.objects.get(pk=1) duplicate(website, new_owner, 'owner') For a in depth example of the problem see: [Duplicating Model Instances @ STO](http://stackoverflow.com/questions/437166/duplicating-model-instances-and-their-related-objects-in-django-algorithm-for-r) *Note* * Not tested with anything but simple Foreign Key relations - the model ordering is _very_ naive.

  • copy
  • duplicate
  • denormalize
Read More

AddThis Social Networking TemplateTag

Code to add an 'AddThis' button to your blog posts. Simply do: {% add_this post.title post.get_absolute_url %} Also, specify your ADD_THIS_USERNAME to your settings. <!-- blog/add_this.html --> <script type="text/javascript">var addthis_pub="{{ username }}";</script> <a href="http://www.addthis.com/bookmark.php" onmouseover="return addthis_open(this, '', '{{ site.domain }}{{ url }}', '{{ site.name }} - {{ title }}')" onmouseout="addthis_close()" onclick="return addthis_sendto()"> <img src="http://s7.addthis.com/static/btn/lg-addthis-en.gif" width="125" height="16" border="0" alt="" style="border:0"/></a> <script type="text/javascript" src="http://s7.addthis.com/js/152/addthis_widget.js"></script>

  • template-tag
  • add-this
  • social-networking
Read More

uuid model field

This code provides a primary key field that is globally unique. It uses the pre_save method to auto-populate the field with a Universal Unique Id as the record is saved the first time.

  • model
  • field
  • uuid
  • universally-unique-identifier
Read More

jQuery color picker model field

This uses the Really Simple Color Picker in jQuery: http://www.web2media.net/laktek/2008/10/27/really-simple-color-picker-in-jquery/ Get source from there or GitHub: http://github.com/laktek/really-simple-color-picker/tree/master

  • models
  • admin
  • jquery
  • widgets
Read More

Boolean Image Flag TemplateTag

Something I end up doing all the time, making a boolean variable show up as a nice image. With this code you can do the following: `{% boolean_img user.is_active %}` And get the following output: `<img src="/media/icons/accept.png" alt="True" />` All you need to do is use the custom templatetag code, load it in your template and use the `boolean_img` tag. **Adjust templates, html and images where needed**

  • image
  • templatetag
  • boolean
  • flag
Read More

Search Engine Referrer info in request

This is exacly the same snippet as #197 http://www.djangosnippets.org/snippets/197/ but returning search enigne, search engine domain and search term in: request.search_referrer_engine request.search_referrer_domain request.search_referrer_term I wanted to show ads only to people comming from search engines so I took snippet #197 and modify it to put that info in the request object.

  • middleware
  • referer
  • http_referer
  • request
  • search-engine
  • referrer
Read More

Auto Generate/Save Thumbnails using Template Filter (scale max_x, max_y, or both)

Couldn't get the original to work, and wanted more functionality (scale on x or y coordinates) <img src="{{ object.image.url }}" alt="original image"> <img src="{{ object.image|thumbnail:"250w" }}" alt="image resized to 250w x (calculated/scaled)h "> <img src="{{ object.image|thumbnail:"250h" }}" alt="image resized to (calculated/scaled)w x 250h h "> <img src="{{ object.image|thumbnail:"250x200" }}" alt="image resized to 250wx200h "> <img src="{{ object.image|thumbnail }}" alt="image resized to default 200w (or whatever you default it to) format"> Original http://www.djangosnippets.org/snippets/192/ Adapted http://www.djangosnippets.org/snippets/955/ Sampled From: http://batiste.dosimple.ch/blog/2007-05-13-1/ http://vaig.be/2008/05/17/stdimagefield-improved-image-field-for-django/

  • template
  • filter
  • image
  • template-filter
  • thumbnail
  • templatetags
Read More

Full Model History

This was a wild experiment, which appears to work! One model holds all the data, from every object version ever to exist. The other model simply points to the latest object from its gigantic brother. All fields can be accessed transparently from the little model version, so the user need not know what is going on. Coincidently, Django model inheritance does exactly the same thing, so to keep things insanely simple, that's what we'll use: class EmployeeHistory(FullHistory): name = models.CharField(max_length=100) class Employee(EmployeeHistory): pass That's it! Django admin can be used to administer the `Employee` and every version will be kept as its own `EmployeeHistory` object, these can of course all be browsed using the admin :-) This is early days and just a proof of concept. I'd like to see how far I can go with this, handling `ForeignKey`s, `ManyToManyField`s, using custom managers and so on. It should all be straightforward, especially as the primary keys should be pretty static in the history objects... *updated 3 August 2009 for Django 1.1 and working date_updated fields*

  • history
  • audit-trail
Read More

3110 snippets posted so far.