Login

Most bookmarked snippets

Snippet List

Block IP addresses

I needed a quick and dirty way to block a user from my site. Just include this middleware class under the 'MIDDLEWARE_CLASSES' variable in your settings.py file. Also include the variable BLOCKED_IPS = ('123.123.123.123',) variable, where the value is a tuple of IP addresses you want blocked from your site.

  • middleware
  • ip-address
Read More

Username genrator

This function generate an username based on the user's full name. First it tries to use the first name first letter plus the last name, second it tires the first name plus the last name first latter, and at last it tries to use the first name with a sequential number at the end. The username generated are all lowercase and ASCII only characters.

  • auth
  • username
  • name
Read More

Django Registration without username

A simple adaptation of RegistrationFormUniqueEmail from django-registration http://code.google.com/p/django-registration to allow users to register without using a username. This works great with the code from this comment: http://www.djangosnippets.org/snippets/74/#c195 to allow users to completely eliminate the need for a username.

  • registration
  • auth
Read More

Twin column model admin index

**Problem:** A large project has lots of apps, so naturally you want a twin column admin interface, with the history out beyond those two columns... **Solution:** Involved editing the admin/index.html template, a simple modification to the adminapplist template tag, and a flag placed in each models.py for models you want in column two. **Instructions:** 1. Create the app templates/admin/index.html file by copying and pasting the provided code. 2. Copy django/contrib/admin/templatetags/adminapplist.py to your app/templatetags directory as column_adminapplist.py and edit as shown in the provided code. 3. add a \_admin\_index\_column = 2 near the top of each models.py file for each one you want to be on the right hand side.

  • admin
  • admin-panel
Read More

create_update for newforms (ModelForm)

Based on [danjak's](http://www.djangosnippets.org/users/danjak/) [snippet](http://www.djangosnippets.org/snippets/99/) but updated to use ModelForms - so can easily handle generic CRUD operations. A replacement create_update.py for use with ModelForm create_object and update_project modified to handle newforms (including FileFields) with ModelForm - it also had delete_object as well for completeness. In addition, it has some extras: * extra_fields - this is a dict or callable that contains additional fields to be passed to the form, for example stuff that is in the session. * on_success - callback called if form is valid and object created/updated, if this is not set the default behaviour is to send a redirect * on_failure - callback called if form is invalid, the default is to redisplay the form. Note that once newforms are finally done these functions are likely to be redundant, as generic views will be updated to use the newforms API, so use with caution!

  • newforms
  • forms
  • generic-views
  • modelform
Read More

More informative error mailings

This middleware makes the admin error emails a lot more informative: you get the same HTML response that you get with `DEBUG=True`. It uses the base class defined in [#638](http://www.djangosnippets.org/snippets/638/). You will probably want to apply the patch for [#6748](http://code.djangoproject.com/ticket/6748) to help avoid slowdowns caused by unintentional database queries. As the ticket (and django-developers thread) notes, it isn't foolproof; you may still find this executing database queries.

  • admin
  • debug
  • error
  • mail
  • 500
Read More

Caching tag with singnal-based invalidation

Example usage: @cached('/data/something_hard') def get_something_complex(): .... return result dispatcher.connect(get_something_complex.invalidate, django.db.models.signals.post_save, Model)

  • cache
  • decorator
  • invalidation
Read More

MODPYTHON logging

A MODPYTHON Apache Log Handler This module provides a logging Handler class to a MODPYTHON Apache server. Python's standard [logging API](http://www.python.org/doc/2.5/lib/module-logging.html) explains how to [use a handler](http://www.python.org/doc/2.5/lib/multiple-destinations.html). The handler, by default, writes entries to the Apache error_log using the standard Python logging API. VIRTUAL HOSTS (Python 2.5) This handler also supports Apache Virtual Hosts where the mp_server object is available. Then, it writes entries to the specific virtual-host server's error_log. To get the mp_server object out of Django, you need the **log_extras()** function in your logging call (See the source comments). This module must be bound to the Python logging API. Use a site_logging.py module to do that as this [related example](http://www.djangosnippets.org/snippets/960/) shows.

  • logging
  • modpython
Read More

Regrouping admin models

This is modification of Django's original adminapplist template tag. You can move your models from one app to other or completely hide them with this mod. Copy django_dir/contrib/admin/templates/admin/index.html file to your templates/admin folder, open it, then change {% load adminapplist %} to {% load custom_adminapplist %} (or whatever you named the templatetag file) After that, write your regrouping schema to settings.py file like this; UPDATED, now using tupples instead of dicts in APP_SCHEMA to make it more DRY. ` APP_SCHEMA=[ ( ['Model','List'], 'From App', 'To App', ), ( ['FlatPage'], 'Flatpages', 'Site Content', ), ( ['Product'] 'Product', 'Shop', ), ( ['Site'] 'Sites', #We are hiding Site model by not defining a target. ), ] `

  • templatetag
  • models
  • admin
  • app
Read More

Add rel=lightbox to all image-links

Add the attribute "rel='lightbox'" to all Links, if the target is an image. `<a href="/path/to/image.jpg">Image</a>` becomes `<a rel="lightbox" href="/path/to/image.jpg">Image</a>` Works for JPG, GIF and PNG Files.

  • filter
  • re
  • lightbox
  • regular-expression
Read More

Mathematical Captcha

Simple mathematical captcha where human is asked to solve a simple mathematical calculation like 2+3=?. Don't require database access or external libraries like PIL. To use MathCaptchaForm subclass it and add your fields. For example of usage and discussion about security of this captcha comparing with traditional image captcha please see my blog entry [Improved Mathematical Captcha](http://www.mysoftparade.com/blog/improved-mathematical-captcha/)

  • captcha
Read More

function_tag

register any python function as a tag with an optional name. usage: in templatetags: @function_tag() def foo(arg): return do_something(arg) in template: {% foo arg %} or {% foo arg as variable %}{{ variable.bar }}

  • tag
  • function
Read More

Clouds Tag

This is a simple template tag to create Tag Clouds. Based on the number of Posts (change the model_name according to your schema), Tags are provided different size ( most number of posts => most popular => and hence the largest. We create a property called cloudsize for each tag, which is an integer between minsize and maxsize. Check the example of sample template use here http://www.djangosnippets.org/snippets/472/

  • tag
  • template-tag
  • tagging
  • clouds
  • cloud
Read More

3110 snippets posted so far.