Login

Top-rated snippets

Snippet List

ReCaptcha for django forms (improved and with remoteip)

My previous snippet with captcha wasn't very portable but Marco Fucci figured out the thing that I couldn't - value_from_datadict function. So all credits go to him and his snippet, I adapted it to my needs, maybe you like my version better - it doesn't need any captcha libraries and let's you modify the widget's html easily. Also I added an option to pass remoteip to google api's verify method. How to use it: In your settings.py add: RECAPTCHA_PUBKEY = 'your recaptcha public key' RECAPTCHA_PRIVKEY = 'your recaptcha private key' After that just import and use ReCaptchaField in your form as you would any other field. That's it. *** Important *** If you want to have peace of mind in case google decided that the remoteip parametr is mandatory then: Derive every form that has the captcha field from ReCaptchaForm and when you create the form object after receiving POST/GET, pass a remoteip parameter like that: form = YourCaptchaForm(data=request.POST, remoteip=request.META['REMOTE_ADDR'])

  • forms
  • captcha
  • recaptcha
Read More

is_in

I know in Django 1.2 we may acquire the same result using {% if value in arg %} but I need this filter in Django 1.1.

  • template
  • filter
  • django
  • contains
  • in
  • is
Read More

notify admin what fields have changed in form submission

here is some working code from a site that maintains profile information (address, phone number, etc) for users ("Partners"). this snippet handles the submitted form (after validation) and checks to see if any fields have been changed by the partner. if so, it shoots off an email to the admin showing the current profile information with changed fields marked with asterisks.

  • fields
  • forms
  • changed
Read More
Author: pjv
  • 0
  • 2

JavaScript implementation of Python xrange() builtin

I don't like not having the `range()/xrange()` in JavaScript — particularly when working with [Underscore.js](http://documentcloud.github.com/underscore/) and other such libraries — so I wrote it. It's not rocket science, but it might help make the world a slightly less annoying place for a couple of people.

  • javascript
  • python
  • list
  • generator
  • array
  • builtin
  • xrange
Read More

Thumbnails in admin using django-thumbnails-works

Very straightforward way to display a thumbnail in the admin using [django-thumbnails-works](http://pypi.python.org/pypi/django-thumbnail-works) . django-thumbnails-works requires [cropresize](http://pypi.python.org/pypi/cropresize/#downloadsInstaller) (which requires and installs PIL). Add 'thumbnail_works'to INSTALLED_APPS in settings.py and here you go. Tested in django 1.3 alpha.

  • image
  • admin
  • thumbnail
Read More

TLS(SSL) middleware, per URL pattern or whole site

Allows url patterns to include a boolean indicating whether a view requires TLS(SSL). The accompanying middleware handles the redirects needed to make sure that it upholds this requirement. **WARNING**: this monkey-patches some Django internals and is difficult to test since Django's TestClient does not support TLS. If you use this make sure you test it thouroughly. Add this to your Django settings USE_TLS = True # The default for this setting is False. URL pattern usage url(r'^login$', 'myproject.login.index', {'require_tls': True}, name='login-index'), Use `require_tls` True to force the middleware to perform redirects needed to make sure your are serving this view using https. Use `require_tls` False to force the middleware to redirect to http. Be careful with this setting, this may not behave as you expect. If you don't care if the view is served via https or http then do not include `require_tls` in the pattern. If you wish to have every view in the site served with TLS then specify the following Django setting ALWAYS_USE_TLS = True # Django setting, use TLS for every view

  • middleware
  • http
  • ssl
  • https
  • tls
Read More

Bit.ly url shortener

A small function to convert a url to another shortened via the bit.ly service. Requires a username and password in django settings.

  • bit.ly
  • rest-api
  • url-shortening
Read More

joinstrings filter

In one situation I needed to join strings in template, so I wrote this filter. Use it like this: 1) var = 23 {{"I have eat %d apples today."|joinstrings:var}} -> "I have eat 23 apples today." var = '23' {{"I have eat %s apples today."|joinstrings:var}} -> "I have eat 23 apples today." 2) var = [23, 45] #or any iterable object (except string - see pt. 1) {{"I have eat %d apples and %d pears today."|joinstrings:var}} -> "I have eat 23 apples and 45 pears today."

  • join strings
Read More

Django Admin Replacer Code

Ok let's descrive what i have done I subclassed the django admin to create a form that makes you choose if activate a delete and replace login inside your admin. Then i have added a form with a modelChoiceField to make you select another model instance when you are selecting an istance to delete. If you select another instance the current instance will be replaced.

  • admin
  • object
  • delete
  • modeladmin
  • replacer
Read More

Image inlining template tag lib

A custom templatetag for inlining image in the browser. The principe is to base64 encode the image and avoid a http request. There is a cache handling, you just have to specify a writable directory. An example of the utilisation (template part): [http://djangosnippets.org/snippets/2267/](http://djangosnippets.org/snippets/2267/) The explication on [http://raphaelbeck.wordpress.com/2010/11/14/make-inline-images-to-improve-performance-with-django-template-tags/](http://raphaelbeck.wordpress.com/2010/11/14/make-inline-images-to-improve-performance-with-django-template-tags/)

  • tag
  • image
  • templatetag
  • base64
Read More

Improved Accept middleware with webkit workaround

An accept middleware, which is based on the code of http://djangosnippets.org/snippets/1042/ but adds a workaround for the buggy accept header, sent from webkit browsers such as safari and chrome. The workaround affects any accept header, that has xml and (x)html in the best q, but also the xml mediatype at first place in the list. If this is the case, the header is rearanged, by shifting the xml mediatype to become the last element of the best quality entries in the header. If the workaround did manipulate the header, and there is a html entry in the list with lower quality as an xhtml entry that is also in the list (with best q), then the html entry is also raised in q to be one entry in front of xml.

  • middleware
  • accept
  • header
  • webkit
Read More

3110 snippets posted so far.