Login

Top-rated snippets

Snippet List

collectmedia command: Copy or link media files from installed apps

This management command discovers media files from all `INSTALLED_APPS` (or the apps you specify) and copies or links them to `MEDIA_ROOT`. Put this code in a file like so: yourapp/management/commands/collectmedia.py ...and don't forget the necessary `__init__.py` files. This command includes an interactive mode (`-i` or `--interactive`) and a dry run mode (`-n` or `--dry-run`) for previewing what will happen. See `manage.py help collectmedia` for more options.

  • media
  • management
  • command
Read More

Timedelta Database Field

This subclass of django.models.Field stores a python datetime.timedelta object as an integer column in the database. It includes a TimedeltaFormField for editing it through djangos admin interface. Feedback welcome. 2011-04-20: TimedeltaField can now be (de)serialized. Tested with JSON. 2009-11-23: `_has_changed()` added. Before form.changed_data contained timedelta FormFields, even if nothing changed.

  • datetime
  • timedelta
  • dbfield
Read More

keeping Model and Field History (everywhere)

Usage: class MyModel(ModelWithHistory): class History: model = True # save model changes into admin's LogEntry table fields = ('f1', 'f2') # save these fields history to AttributeLogEntry table f1 = CharField(max_length=100) f2 = IntegerField() for threadlocals, see http://code.djangoproject.com/wiki/CookBookThreadlocalsAndUser Aware! Not thoroughly tested yet. May cause problems with loading fixtures.

  • model
  • history
Read More

local django model test

Sometimes you need to test some model features without a complete django app installation. Just play only with the model object. With these small script you have a complete in memory django installation. Some Links: http://www.djangosnippets.org/snippets/1044/ (en) http://www.jensdiemer.de/permalink/150/mein-blog/99/django-db-model-test/ (de) http://www.python-forum.de/viewtopic.php?f=3&t=15649 (de) See also: https://github.com/readevalprint/mini-django/

  • model
  • testing
  • test
  • local-test
Read More

Copy a model instance

Create a copy of a model instance. Works in model inheritance case where ``instance.pk = None`` is not good enough, since the subclass instance refers to the parent_link's primary key during save. M2M relationships are currently not handled, i.e. they are not copied. See also Django #4027.

  • model
  • copy
Read More

Fixture for test users

This is a simple fixture that is useful for many tests. It contains the following users: * admin * staff * user0 * user1 * user2 * user3 * inactive0 * inactive1 The password of every user is the same as his username, e.g.: admin/admin

  • testing
  • fixtures
  • auth
  • test
  • fixture
  • users
Read More
Author: V
  • 3
  • 3

breadcrumbs

With this script you can create a simple breadcrumbs line for navigation: Home >> Page >> Subpage ...

  • breadcrumbs
Read More

Model dependency graph using graphviz (script)

Instructions: Set your environment variables, install graphviz, and run. Finds all models in your installed apps and makes a handsome graph of your their dependencies based on foreignkey relationships. Django models have green outlines, yours have purple. The edge styling could be changed based on edge type.

  • models
  • graph
  • graphviz
Read More

object-oriented generic views

Here's an example of writing generic views in an object-oriented style, which allows for very fine-grained customization via subclassing. The snippet includes generic create and update views which are backwards compatible with Django's versions. To use one of these generic views, it should be wrapped in a function that creates a new instance of the view object and calls it: def create_object(request, *args, **kwargs): return CreateObjectView()(request, *args, **kwargs) If an instance of one of these views is placed directly in the URLconf without such a wrapper, it will not be thread-safe.

  • views
  • generic
  • object
  • update
  • create
Read More

Accepting and processing PayPal IPN messages (including using App Engine)

PayPal's [https://www.paypal.com/ipn](IPN mechanism) is ridiculously easy to consume. You can tell PayPal to POST every single transaction on your account to a URL somewhere, then set up a handler at that URL which processes those transactions in some way. Security is ensured by POSTing the incoming data back to PayPal for confirmation that the transaction is legitimate. These classes are probably over-engineered, but they were a fun experiment in creating class-based generic views.

  • appengine
  • paypal
  • payment
  • ipn
Read More

[UPDATE]Filter to resize a ImageField on demand

A filter to resize a ImageField on demand, a use case could be: <img src="{{ object.image.url }}" alt="original image"> <img src="{{ object.image|thumbnail }}" alt="image resized to default 104x104 format"> <img src="{{ object.image|thumbnail:200x300 }}" alt="image resized to 200x300"> Original http://www.djangosnippets.org/snippets/192/

  • template
  • image
  • thumbnail
Read More

versioned_media templatetag

Best practice based on [YSlow recommendations](http://developer.yahoo.com/yslow/), add the following to your Apache config for your media directory. <Directory /home/.../site_media/> ... FileETag None ExpiresActive on ExpiresDefault "access plus 10 years" AddOutputFilterByType DEFLATE text/css application/x-javascript </Directory` Make sure to enable mod_deflate and mod_expires.

  • templatetag
  • css
  • media
  • js
  • versioned_media
Read More

Credit Card With Newforms

Alternative version of newform code for handling credit cards. Unlike the other two credit-card snippets (http://www.djangosnippets.org/snippets/764/ and http://www.djangosnippets.org/snippets/830/), this uses two drop-down boxes instead of text fields for the expiration date, which is a bit friendlier. It doesn't do as much checking as snippet #764 since we rely on the payment gateway for doing that. We only accept Mastercard, Visa, and American Express, so the validation code checks for that.

  • newforms
  • datefield
  • credit-card
  • expiration-date
Read More

3110 snippets posted so far.