Login

Top-rated snippets

Snippet List

Smarter USPhoneNumberField

The original USPhoneNumberField only validates xxx-xxx-xxxx values. This field validates... > (xxx) xxx xxxx > xxx-xxx-xxxx > (xxx)-xxx-xxxx > many others. **Explanation of the regular expression:** 1. Accepts either (xxx) or xxx at the start of the value. 2. Allows for any amount of dashes and spaces (including none) before the next set of digits. 3. Accepts 3 digits 4. Allows for any amount of dashes and spaces (including none) before the next set of digits. 5. Accepts 4 digits, finishing the value. This field saves in the same format that the original USPhoneNumberField does (xxx-xxx-xxxx). Enjoy!

  • forms
  • validation
  • field
  • phone
Read More

RefreshSessionMiddleware

This middleware refreshes the session before it expires to avoid dropping the session of an active (but read-only) user. By default it refreshes the session after half the expiry time has elapsed. (This middleware does nothing for browser-length sessions.)

  • sessions
  • refresh
Read More

very simplistic url cache flushing

This is an (overly simple) example of how to manually delete something from the cache. Usage is simply `delete_url_cache('/some_view/')` This most likely doesn't work when using Vary headers but does seem to work fine for the most general case of simply needing to programmatically flush the cache after certain actions.

  • cache
Read More
Author: jpt
  • 1
  • 3

Replace Paragraph Tags for Flash

This template tag does two things needed to display content from something like a TextField wrapped with TinyMCE in Flash's htmlText component: 1. Replace `<p>` tags with `<br />` 2. Strip all occurances of `\n`

  • template
  • tag
Read More

[middleware] Rewrite anchors to point into Coral CDN

This simple middleware rewrites the 'href' attribute of any `<a>` tags in your response content. The URL href is modified by appending the string '.nyud.net', which causes the Coral Content Distribution Network to retrieve a copy of the page and cache it before returning it to the user agent. This might be useful if you're writing another Slashdot and you want to avoid turning the servers you link to into smoking craters. You [should be able](http://www.djangosnippets.org/snippets/910/) to apply this functionality to a single view as well (though I haven't tried this yet).

  • middleware
  • coral
  • cdn
  • anchor-rewrite
Read More

Load a local settings file for dev/test environments

Add the snippet to your settings.py. If you have a settings_local.py it will load that one. Can be used in development environments where you might have different settings for your dev sandbox. You should exclude settings_local.py from SVN. By Rudy and Ed Menendez

  • settings
  • environment
  • production
Read More

FilteredSelect

Displays as an ordinary selectbox with an additional text-input for filtering the options. An adaption of the example at [1] for use with Django. The code assumes the java script from [2] is downloaded into your media folder. Remember to output your form's media, for instance like: "{{form.media|safe}}", somewhere above your form. **License:** [2] by Mr Patrick Fitzgerald is licensed under GPL and the python code written by myself is GPL as well. **References:** 1. http://www.barelyfitz.com/projects/filterlist/index.php/ 2. http://www.barelyfitz.com/projects/filterlist/filterlist.js

  • javascript
  • widget
Read More

Using Google Apps Premium infrastructure for user management

To create a lower entry barrier to logging into our intranet I created a very simple backend using Google Apps Premium provisioning API (http://code.google.com/apis/apps/gdata_provisioning_api_v2.0_reference.html). This enables controlling access for your users based on their status in your Google Apps instance. *(NOTE! Since the provisioning API is only available in the Premium version of Google Apps you first need to upgrade if you haven't done so already)* Requirements: You need Google Data libraries for python. They can be downloaded from http://code.google.com/p/gdata-python-client/downloads/list Google Apps Premium -- User used by the script must have admin rights

  • authentication
  • login
  • auth
  • google
  • google-apps
  • auth-backend
Read More

Gzip decorator

Rather than using the full GZipMiddleware, you may want to just compress some views. This decorator lets you do that. @gzip_compress def your_view(request, ...): ....

  • middleware
  • decorator
  • gzip
Read More

PostGIS "nearest" including range limiting

this is an enhancement to paulsmith's code snippet 190, to provide an extra "search by distance" parameter rather than "limit number of results returned". i need this for a social networking site where users can search for people within a certain geographical area.

  • postgis
Read More

Caching XHTML render_to_response

This code improves on Django's render_to_response shortcut function in the following ways: 1. If the caller does not provide a MIME type, and the caller is passing a RequestContext, it interrogates the request to determine if the HTTP client supports application/xhtml+xml encoding. If so, it sets the request type to application/xhtml+xml to override the HttpRequest class's default mime type of text/html. 2. It caches parsed templates in its own cache to improve performance. If you aren't using XHTML in your templates, you may choose to comment out the code that tests for and sets the application/xhtml+xml MIME type. I place this code in a file named "util.py" in my application. In my views, I write "from app.util import render_to_response" where I used to write "from django.shortcuts import render_to_response". Note that the caching functionality provided by this code means that you will need to recycle your Django instance when you make template changes. Instructions for doing this depend on how you have deployed your Django application.

  • render_to_response
  • template
  • cache
  • html
  • xhtml
Read More

Admin definition converter (for newforms-admin)

A script I wrote a little while ago to help speed up newforms-admin conversion. From memory it works best when run from an old-forms installation, but it is still useful post-conversion. There are some features missing, but it's supposed to be a starter. Just install this command and run: ./manange.py port2nfa > admin.py (To install, copy the file to management/commands/port2nfa.py in an app somewhere)

  • newforms
  • admin
  • command
Read More

Parse TemplateTag Variables Safely

Often times I want to be able to interchangeably pass either literal strings or context variables to a custom templatetag. This function first checks a string for the existence of surrounding quotes and uses the string inside. If the string didn't explicitly use quotes it checks to see if it can resolve the string as a context variable. As a fallback the function will simply return the full string. Example: {% get_latest_by_tag 'django' %} {% get_latest_by_tag object.tag %}

  • templatetag
Read More

3110 snippets posted so far.