Login

Top-rated snippets

Snippet List

Simple e-mail template tag

Usage: {% mailto email [linktext] %} `email' parameter is required; linktext is optional, defaulting to the email address.

  • template
  • tag
  • templatetag
  • email
Read More

Default Template Loading

One of the things about Django that has always irked me is that there seems to be no standard facility for just loading a page and displaying it, simply. Yes, there is the flatpages module, but it is primarily designed for loading content from a DB. While you specify a custom template and put in junk values for the DB content, it just feels like extra, unnecessary work. Thinking about the problem some more, I borrowed from the idea of flatpages and implemented a view that will load an otherwise unmapped template off the filesystem and render it. This is very useful when converting an existing site over. Just copy your files over. No need to map anything in the URL conf or use the admin to add any flatpage entries. Since it'll render the template, too, you can even use tags and what not, so it need not be a static page.

  • template
  • loader
Read More

Dynamically specify TEMPLATE_DIRS

In your site’s settings.py module (in your site root), TEMPLATE_DIRS takes absolute paths. Here is a way to dynamically determine the absolute path to the application directory so you only have to specify relative paths within settings.py. Obviously, replace “application_directory” with the name of your application’s directory.

  • configuration
Read More

Easy Form Structuring

With this, you can group form fields very easily. Since it does not create any html, you can use it not only to create fieldsets (with tables, lists, etc).

  • fieldset
  • form
  • field
  • grouping
  • structuring
Read More
Author: jug
  • -4
  • 3

Expose the 404/500 views during development

A simple addition for the urls.py that exposes the 404/500 templates during development. This way you can test how those look. They're mounted under /404/ and /505/ respectively. Add this at the bottom of your main urls.py.

  • development
  • debug
  • urlsconf
Read More

Delete View

Usage: @login_required def action_delete(request,object_id): return delete_view(request,object_id,ActionItem)

  • view
  • delete
Read More

Type less with newforms admin

I recently converted a site with over 60 models to newforms admin. I like the seperation of the display from the defintion, but it does introduce quite a bit more typing which isn't in the spirit of Django DRY... I particular I got bored of typing admin.site.register(Model, ModelAdmin) Over and over again so I wrote this little bit of code which does a bit of introspection. It assumes that 1. You import all your models into your admin.py, ie from "myapp.models import *" 2. The admin class for Model is called ModelAdmin Put this snippet at the end of your admin.py I hope that saves someone a bit of typing!

  • admin
Read More
Author: ncw
  • -4
  • 4

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

3110 snippets posted so far.