Login

Most bookmarked snippets

Snippet List

Gravatar support for Django comments

A templatetag to add [Gravatar](http://www.gravatar.com/) support for [Django comments](http://docs.djangoproject.com/en/dev/ref/contrib/comments/ "Django Comments"). Based on [this snippet](http://www.djangosnippets.org/snippets/772/) but works for everyone who comments even if they are not a registered user.

  • comments
  • gravatar
Read More

Require login by url

An example of using it in your settings.py: MIDDLEWARE_CLASSES = ( 'django.middleware.common.CommonMiddleware', 'django.contrib.sessions.middleware.SessionMiddleware', 'django.contrib.auth.middleware.AuthenticationMiddleware', 'django.middleware.doc.XViewMiddleware', 'util.loginmiddleware.RequireLoginMiddleware', ) LOGIN_REQUIRED_URLS = ( r'/payment/(.*)$', r'/accounts/home/(.*)$', r'/accounts/edit-account/(.*)$', ) In a nutshell this requires the user to login for any url that matches against whats listing in LOGIN_REQUIRED_URLS. The system will redirect to [LOGIN_URL](http://www.djangoproject.com/documentation/settings/#login-url)

  • middleware
  • authentication
  • url
  • login
  • auth
Read More

Command Line Script Launcher

I often write short test or development scripts that are intended as one-offs. I typically want to execute these scripts from the command line but am always forgetting the necessary steps to set up the Django environment [as described here][bennett]. This snippet allows you execute arbitrary Python scripts from the command line with the context of a given project: python manage.py execfile /path/to/some/script.py Add the code to a file named `execfile.py` within the `management/commands` directory of one of your apps ([see here][ref] for details). [ref]: http://www.djangoproject.com/documentation/django-admin/#customized-actions "Customized Actions" [bennett]: http://www.b-list.org/weblog/2007/sep/22/standalone-django-scripts/ "Standalone Django Scripts"

  • script
  • management
  • execfile
Read More

YouTube Template Filter

Given a youtube url (can copy and paste right from the browser) turns the url into an embedded video. If you put this in your app's templatetags/filters.py, you can do the following >{{ object.youtube_url|youtube }} and it would embed the youtube video.

  • template
  • filter
  • youtube
  • embed
Read More

in_group template filter

Allows you to search if a user belongs to a given group. Along the same lines as snippet [390](http://www.djangosnippets.org/snippets/390/), but uses a regular ``if`` tag so it is more flexible. (Updated for efficiency. Running a boolean test on a QuerySet avoids a bit of unnecessary overhead.) (Updated to accept a list of groups.)

  • template
  • filter
  • group
Read More

Verbose template filter : avoid too many if

This tiny template filter saves you the tedious test "if this variable is set, print this text based on this variable". 'verbose' filter takes one parameter : a string containing '%s' which is a placeholder for the value to test. Check those examples : * Replace this : {% if name %} Hello {{ name }}, this is a dummy text {% endif %} * By this : {{ name|verbose:"Hello %s this is a dummy text" }} This is also usefull for HTML : {{ image|verbose:"<img src=\"%s\" />" }}

  • template
  • filter
Read More

YAAS (Yet Another Auto Slug)

This is the self-populating AutoSlugField I use. It's not the [first such snippet](http://www.djangosnippets.org/tags/slug/), but (IMO) it works a bit more cleanly. It numbers duplicate slugs (to avoid IntegrityErrors on a unique slug field) using an "ask-forgiveness-not-permission" model, which avoids extra queries at each save. And it's simply a custom field, which means adding it to a model is one line. Usage: class MyModel(models.Model): name = models.CharField(max_length=50) slug = AutoSlugField(populate_from='name')

  • slug
Read More

Auto slug field

New field type which allows prepopulate_from to work not only from javascript but in python too. If the slugfield has unique=True creates a unique slug too.

  • slug
  • field
  • auto
  • prepopulate_from
Read More

Forms splitted in fieldsets

This template tag build a Form splitted in fieldsets. The fieldsets are configured with a second parameter, that is a tuple like the one used in the Admin class in models in the attribute "fields". You pass to the template the form and the tuple and than use them as parameters for the templatetag. You can take a look at the source and modify It to build forms the way you like. It is very useful If you do not like the way Django build forms with the methods as_p, as_ul or as_table and also do not like to write html by hand.

  • forms
  • fieldset
  • form
  • fieldsets
Read More

URL based breadcrumbs

This is a simple URL based breadcrumb filter I'm using on a site I'm currently building. To use it, just throw the code into template-tag file, and simply pass the current url in a page template like so {{ request.get_full_path|breadcrumbs }} As an example of how to style it, wrap it in a unordered list with an id of breadcrumb, and then in your styles use #breadcrumb li { display: inline; }. On a contact form for a site user, this will produce something along the lines of you are here : [home](/) » [users](/users/) » [username](/username/) » contact form Hopefully someone may find it useful.

  • filter
  • breadcrumbs
Read More

Specify a manager for the Admin

This example shows how you can easily limit the objects in the admin by specifying which Manager the admin should use. I haven't seen this documented anywhere (perhaps I've missed it), but it's proven extremely useful to me. The example here will limit objects to those that are attached to the current Site, but you can use any Manager you want (for example, a Manager that shows only published Articles). Finally -- not that I'm suggesting this -- but you *could* combine this with the ThreadLocals trick to show only objects that have been created by that user.

  • managers
  • models
  • admin
  • sites
Read More

Another Memcache Status View

This was inspired by [this memcache status snippet](http://www.djangosnippets.org/snippets/54/) However, this version uses the quasi-internal cache._cache.get_status(), and it compiles a list of stats for each server that you specify in your CACHE_BACKEND setting.

  • memcache
Read More

require XMLHttpRequest view decorator

Decorator to make a view only accept requests from AJAX calls. Usage:: @require_xhr() def my_view(request): # Returns data # ... by [skam](http://skam.webfactional.com/)

  • ajax
  • views
  • view
  • decorator
  • decorators
  • xhr
  • xmlhttprequest
Read More

3110 snippets posted so far.