Login

Most bookmarked snippets

Snippet List

Make anything into a template

This is a quick and dirty way to reuse the Django templating system for your own ends. Just pop in the familiar Django template syntax into whatever content you like and any chunk of content can be a template. This is great if you need to wrap data in HTML (as in for a mass email). The best part about this is that your "template" can now be stored in a database, instead of just in the file system.

  • template
  • templating
Read More

Pad integers with leading zeros (template filter)

Disclaimer: I'm not the world's greatest programmer, so there may be better ways to do this, but it works for me (feel free to offer your improvements, though!). Basically, this will pad an integer with leading zeros and return a string representation. User it like this: {% forloop.counter|leading_zeros:"5" %} ...where "5" is the number of desired digits. In this case, if it was the 12th time through the forloop, the filter would return "00012". Why do this? Either for alignment, such as in tables, or for aesthetics -- for an example, see [Shaun Inman's comment section](http://shauninman.com/archive/2007/11/16/mobilesafari_view_source).

  • template
  • filter
Read More

newforms and ModelForm

I noticed that form_for_* in newforms now carry deprecation warnings. This code is a minimal demonstration of how to use ModelForm as a replacement. Full information can be found on [Stereoplex](http://www.stereoplex.com/two-voices/django-modelform-and-newforms). Hope this helps you.

  • newforms
  • form_for_instance
  • modelclass
  • form_for_class
  • deprecated
Read More

freshdb management command

This is useful especially during the model-creation stage, when things are in constant flux. The `freshdb` command will drop the project's database, then create a new one. A common use case: manage.py freshdb manage.py syncdb

  • db
  • database
  • management
Read More

Link filter

Almost too dumb to be worth posting but its saved me a lot of typing. Instead of: <div> <a href="{{ object.get_absolute_url }} class="object-icon">{{ object }}</a> </div> just put: <div>{{ object|link }}</div>

  • filter
  • links
Read More

URL redirects middleware

Sometimes you need to make redirects that involve domains, you can't define those on the site urls, this middleware lets you define multiple redirects on your site settings. Note: *You also can use the web server to do this, but I have found this middleware a useful tool to quickly or temporarily define redirects*. Depending on your needs you may also find useful [snippet 434](http://www.djangosnippets.org/snippets/434/).

  • middleware
  • url
  • redirect
  • domain
Read More

NewForms Readonly / Edit Pattern

This is a little base class that I use to display forms in a readonly mode. Providing a simple javascript on the front end to allow a user to click an **edit** link that will show/hide the actual form. To use it simply inherit from it. Then in your templates do the following: `{{ form.as_readonly_table }}` **Other Notes** * **form.html** `{% for field in bound_fields %} {% include "newforms/field.html" %} {% endfor %}` * **field.html** `{% if field.errors %} <tr> <td>&nbsp;</td> <td>{{ field.errors }}</td> </tr> {% endif %} <tr> <th style="text-align:right"> <label for="id_{{ field.name }}">{{ field.label }}{% if field.field.required %}<span class="required"><sup>*</sup></span>{% endif %}</label> </th> <td>{{ field }} {% if field.help_text %} <p class="helptext">({{ field.help_text }})</p> {% endif %} </td> </tr>`

  • template
  • newforms
  • readonly
  • edit-mode
Read More

Copy/Paste form generation

Generator to help make newforms classes a bit like inspectdb does for generating rough model code. This is a standalone script to go in your project directory with settings.py and called from the prompt. It looks up the model, and generates code to copy/paste into your app. Hopefully to make a building a custom form with a lot of fields a little easier. Every argument should be a model identifier of form appname.modelname. Usage from the command line: python form_gen myapp.MyModel myapp.MyOtherModel

  • form
  • copy
  • generator
  • paste
Read More

ingore_fields

I use this snippet when creating newforms with form_for_model and form_for_instance. Both do not have an argument that lets you filter out fields easily so I wrote this instead. Typical use: `form_class = form_for_instance(obj, fields=ignore_fields(model_class, ['field_name1']))`

  • newforms
  • fields
  • ignore
Read More

bigger textfields in admin panel

Sometimes a textarea field is to small for usage. I add some JavaScript and CSS to resize all textareas a little dynamically: The JS change the size in dependence text-lengthen. You should store this snippet into a file like this: templates_django/admin/base_site.html

  • admin
  • textfield
  • admin-panel
Read More

Logging solution for mod_python/FCGI

The solution is based on [dballanc's snippet](http://www.djangosnippets.org/snippets/420/). Can easily be combined with any of the [SQL tracing solutions](http://www.djangosnippets.org/tags/debug/). You might want to run a separate logging server and redirect your logs there. Please refer to the [logging reference manual](http://docs.python.org/lib/module-logging.html).

  • log
  • mod_python
  • debug
  • logging
  • fcgi
Read More

Trigger a user password change

I would like to catch the raw plaintext password if a user created or change his password. First i tried to handle this with signals.post_save at the User class, like this: `dispatcher.connect(update, signal=signals.post_save, sender=User)` The problem is, in the User model exists e.g. 'last_login'. So the save method called every time, the user logged in :( And with post_save i get only the hashed password and not the plaintext source password. I found a simple way to trigger a user password change. I hacked directly into the django.contrib.auth.models.User.set_password() method. See the sourcecode. There exists a discussion in the [django-users thread](http://groups.google.com/group/django-users/browse_thread/thread/7c074e80a9cdcd21/) about this.

  • password
  • user-account
Read More

3110 snippets posted so far.