Login

All snippets

Snippet List

DebugFooter middleware with textmate links

a minor remix of simon's debug footer: <http://www.djangosnippets.org/snippets/766/> > Adds a hidden footer to the bottom of every text/html page containing a list of SQL queries executed and templates that were loaded (including their full filesystem path to help debug complex template loading scenarios). This version adds TextMate links : if you are working on your local machine and using TextMate you can click on the template paths and they will be opened in TextMate. This speeds up development time considerably ! also, this works with django 1.0 (simon's version got broke by the 'connect' refactor) update: the view function is now linked > To use, drop in to a file called 'debug_middleware.py' on your Python path and add 'debug_middleware.DebugFooter' to your MIDDLEWARE_CLASSES setting.

  • middleware
  • debug
  • textmate
Read More

Model inheritance with content type

Contact is a parent class. Subclasses might be Company, Person, Artist, Label etc. Basic address, email etc. fields can be added to the parent class and all subclasses will have those. Having searched your database for contacts (undifferentiated by class) you then want to reload the chosen object as the subclass that it really is : ``thing.as_leaf_class``

  • inheritance
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

Chaining select boxes in admin with MooTools

This code will allow you to use chained select boxes in the django automatic admin area. For example, you may have a product, then a category and subcategory. You'd like to create a product, and then choose a category, and then have a chained select box be filled with the appropriate subcategories.

  • ajax
  • admin
  • select
  • mootools
Read More

breadcrumbs

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

  • breadcrumbs
Read More

Moving items up/down from the admin interface

Move Items up and down from the admin interface. Like phpBB does it with its forums. An additional select field is added to the admin form. After the model has been saved, a model method is called (with the value of the new field), which handles the reordering. A more detailed description and a screenshot can be found [here](http://blog.vicox.net/2008/09/04/ordering-in-django-10/).

  • admin
  • ordering
  • moving
  • up/down
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

Integer Currency Input

This accepts values such as $1,000,000 and stores them to the database as integers. It also re-renders them to the screen using the django.contrib.humanize.intcomma method which takes 1000000 and turns it into 1,000,000. Useful for large currency fields where the decimals aren't really necessary.

  • newforms
  • currency
  • field
  • integer
  • input
Read More

render_with decorator

Automatically render your view using render_to_response with the given template name and context, using RequestContext (if you don't know what this is you probably want to be using it). For example: @render_with('books/ledger/index.html') def ledger_index(request): return { 'accounts': ledger.Account.objects.order_by('number'), }

  • render_to_response
  • requestcontext
  • decorator
  • rendering
Read More

Something like list_detail generic view but returns PDF document instead

This should work as a `django.views.generic.list_detail` generic view but will produce PDF version of given template. This code is merged code from perenzo's [example](http://www.djangosnippets.org/snippets/659/) and code from `django.views.generic.list_detail` module. `pisa` package is required from (http://www.htmltopdf.org/download.html) with `html5lib` package and Reportlab Toolkit 2.1+ NOTE: this is code for Django 0.96. In Django 1.0 change in line 3: ObjectPaginator to Paginator

  • generic-views
  • pdf
  • html
  • css
Read More

Form splitting/Fieldset templatetag

Syntax: `{% get_fieldset list,of,fields as new_form_object from original_form %}` Example: {% load fieldsets %} ... <fieldset id="contact_details"> <legend>Contact details</legend> <ul> {% get_fieldset first_name,last_name,email,cell_phone as personal_fields from form %} {{ personal_fields.as_ul }} </ul> </fieldset> <fieldset> <legend>Address details</legend> <ul> {% get_fieldset street_address,post_code,city as address_fields from form %} {{ address_fields.as_ul }} </ul> </fieldset>

  • fields
  • forms
  • fieldset
Read More

Timestamps in Model

A simple way to add `date_created` and `date_modified` timestamps to a model. Adds a `date_created` timestamp when the object is first created and adds a `date_modified` timestamp whenever the item is saved. **Note:** You might be tempted instead to use: `date_created=models.DateTimeField(default=datetime.now())` but that won't work as Python will calculate `datetime.now()` only once when it interprets your model. This means that every object created will get the same `date_created` timestamp until you restart your server.

  • python
  • save
  • timestamp
Read More

easy admin registration

This essentially wraps [snippet 917](http://www.djangosnippets.org/snippets/917/) (with full credit to author ncw) in a convenience function so that you can type: admin_register(admin, namespace=globals()) or more concisely: admin_register(admin, globals()) at the end of your admin.py file without having to register each model and admin class individually.

  • admin
  • register
Read More

3110 snippets posted so far.