Login

Most bookmarked snippets

Snippet List

Case-insensitive lookup by default

I wanted lookups on tags to be case insensitive by default, so that things like Tag.objects.get(name='Tag') would return any similar tags (ignoring case differences), i.e. `<Tag: tag>`. This snippet makes lookup on the 'name' field case-insensitive by default, although case-sensitive lookups can still be achieved with 'name__exact'. Methods like get_or_create will work as expected and be case-insensitive.

  • model
  • tags
  • tagging
  • manager
  • case-insensitive
  • iexact
  • queryset
Read More

Simple solution for model schema evolution / database changelog

This is a very simple approach to "schema evolution" (Not sure if you can even call it so) - I use it for my project: [SCT](http://sct.sphene.net) and it seems to work quite nicely. The idea is, that if you add or change your models, you add a 'changelog' attribute to your class which defines the SQL queries required to update your models. Of course this only works for one database type... An example 'syncdb' call if a new changelog entry was detected: kahless@localhost ~/dev/python-new/sphenecommunity/sphenecommunity $ ./manage.py syncdb Executing module body. 2007-06-16 00: SQL Statement: ALTER TABLE "sphboard_post" ADD markup varchar(250) NULL Detected changes - Do you want to execute SQL Statements ? (yes,no): So if the SQL statement won't work with the database the user is currently running, he at least knows exactly what he is expected to change in his models. and he can stop automatic execution by simply entering 'no' here.

  • changelog
  • schema-evolution
Read More

Script for getting Google Page Rank of page

I was looking for such script written in python and found google checksum algorithm at http://pagerank.gamesaga.net/ And just added complete functionality to it. Usage: script.py PR http://somepage.com/page.html It also has function to retrieve Yandex TYC.

  • google
  • pagerank
  • yandex
Read More

prettify html

with this middleware you can use tidy to prettify your html, just add the class to the `MIDDLEWARE_CLASSES` setting. tidy has an enormous number of options, se [Tidy Options Reference](http://tidy.sourceforge.net/docs/quickref.html) . you must have [µTidylib](http://utidylib.berlios.de/) installed.

  • html
  • xhtml
  • tidy
  • standard
Read More

Days Since Filter

Very simple filter that returns one of the following by default: 1. \# days ago 2. yesterday 3. today 4. January 01, 2007 Example template code: This thread was started {{ post.date_created|dayssince }}. This thread was started today. E-mail sent: {{ email.date_sent|dayssince|capfirst }} E-mail sent: Yesterday Object created: {{ obj.date_created|dayssince|upper }} Object created: 12 DAYS AGO User's bogus birthday: {{ user.get_profile.bday|dayssince }} User's bogus birthday: April 20, 3030

  • filter
Read More

Using Templates to Send E-Mails

This is a basic example for sending an email to a user (in this case, when they've signed up at a website) using the Django template framework. It's really quite simple - we're just using a plain text template instead of HTML, and using the output of the template's 'render()' method as the message body. Of course, in your project you won't blindly use data from request.POST! This example was first posted on [my blog at rossp.org](http://www.rossp.org/blog/2006/jul/11/sending-e-mails-templates/)

  • templates
  • email
Read More

ForeignKey dropdown selector

Most of the time when you want a dropdown selector based on a ForeignKey, you'll want to use [snippet #26](http://www.djangosnippets.org/snippets/26/) Here's an alternative approach, perhaps useful when you want to define choices once and reuse it in different views without overriding Form `__init__`.

  • dynamic
  • foreignkey
  • dropdown
  • choices
  • property
Read More

Using Textarea

A very common field in forms is the `<textarea>`, but `newforms` has no such field. Instead, you must use a dummy field (such as `newforms.CharField`) and use the `newforms.widgets.Textarea()` widget to render a textarea.

  • newforms
  • textarea
  • forms
Read More

Allow foreign key attributes in list_display with '__'

This snippet provides a subclass of admin.ModelAdmin that lets you span foreign key relationships in list_display using '__'. The foreign key columns are sortable and have pretty names, and select_related() is set appropriately so you don't need queries for each line. EDITS: * Fixed error when DEBUG=False. * Broke out `getter_for_related_field` so you can override short_description manually (see example). * Added regular foreign key fields to select_related(), since this is overriding the code in ChangeList that usually does it.

  • admin
  • foreign-key
  • list_display
Read More

Registration with confirmation email

This registers a users taking data from a submitted form, sends a confirmation email, activates the account when the confirmation link is clicked and logs the user in

  • registration
  • email
  • register
  • confirmation
Read More

Drag and drop ordering of admin list elements for Grappelli

Adds drag-and-drop ordering of rows in the admin list view for [Grappelli](http://code.google.com/p/django-grappelli/). This is based on [Snippet #2057](http://djangosnippets.org/snippets/2057/) and fixes some bugs as well as switching to jQuery/jQuery UI provided by Grappelli. No additional files need to be installed. The model needs to have a field holding the position and that field has to be made list_editable in the ModelAdmin. The changes of the ordering are applied after clicking 'Save'.

  • admin
  • sort
  • jquery
  • order
  • sortable
  • grappelli
Read More

Access transparently the user profile information

Splitting the information about a user across two models (User and UserProfile) is not to everyone's liking. This snippet monkeypatches the User model so that users can access transparently their profile information while still storing the data in two tables under the hood. Thus it is similar to the inheritance approach (http://scottbarnham.com/blog/2008/08/21/extending-the-django-user-model-with-inheritance/) but has the benefit of (a) not requiring a custom authentication backend or middleware and (b) loading the profile instance lazily, so there's no extra overhead if the profile infromation is not accessed. Read the docstrings for more details.

  • auth
  • profie
  • user profile
Read More

adding fields to User model

This code adds new field to Django user model. It must be executed early as much as possible, so put this code to __init__.py of some application.

  • fields
  • model
  • user
  • auth
  • field
Read More

3110 snippets posted so far.