Login

All snippets written in Python

Snippet List

reCAPTCHA integration

Integrating [reCAPTCHA](http://recaptcha.net/) with Django. **Warning**: although *remoteip* is used as optional parameter in this snippet, it is likely to become mandatory in the future. Please see [the comment by Jim Garrison](http://www.marcofucci.com/tumblelog/26/jul/2009/integrating-recaptcha-with-django/#comment-262) for more detail. Generic version of [this snippet](http://www.djangosnippets.org/snippets/433/). 1. Register on [reCAPTCHA](http://recaptcha.net/) to get your public/private key pair 2. Add your keys in settings.py 3. Add [recaptcha-client](http://pypi.python.org/pypi/recaptcha-client) to your project 4. Put ReCaptchaField and ReCaptcha widget somewhere (I've used a generic app `marcofucci_utils`) 5. Configure your form More information on my website [marcofucci.com](http://www.marcofucci.com/tumblelog/26/jul/2009/integrating-recaptcha-with-django/).

  • captcha
  • recaptcha
Read More

Decorator that limits request methods

I wanted to be able to limit which types of requests a view will accept. For instance, if a view only wants to deal with GET requests. @methods(GET) def index(request): # do stuff Now, calling this view with a non-GET request will cause a 403. You can easily change this to a 404, by using a different return function: which you may wish to do with openly available sites, as a 403 indicates there is a resource present.

  • decorator
  • request
Read More

More information about users and groups in user admin

Based on [this snippet](http://www.djangosnippets.org/snippets/876/). More clean, with links to the related admin forms. Nice example on customization of contributed django admin apps. It adds the following to the user list interface: fields for is_superuser and is_staff, last login time; by default, short names of groups user is in (mousehover to see full names) It adds the following to the to group list interface: list of users in your groups. To enable, just put it somewhere and import it from your main urls.py: import utils/admin_auth.py

  • admin
  • customization
  • users
  • groups
  • roles
Read More

Hidden Date Display Widget for Admin

This is a custom widget for displaying a view only date field in the django admin. I used it to get around this ticket: [http://code.djangoproject.com/ticket/342](http://code.djangoproject.com/ticket/342)

  • admin
  • view
  • date
  • widgets
  • field
  • only
Read More

Media RSS generation for Photologue

Provides a basic implementation of Yahoo's [MediaRSS](http://video.search.yahoo.com/mrss) format for [Photologue](http://code.google.com/p/django-photologue/) galleries Simplest usage: I have feedgenerator.py in a utils directory. Import photofeeds and hook up the feed url in your URLConf: from utils.feedgenerator import photofeeds urlpatterns += patterns('', url(r'^feeds/(?P<url>.*)/$', 'django.contrib.syndication.views.feed', {'feed_dict': photofeeds}),) Without customization, this will generate a feed for the gallery archive at `/feeds/gallery/` It will contain a single photo per gallery, as returned by Gallery.sample() Additionally, each gallery has a dynamic feed available at the url via Gallery.title_slug: `/feeds/gallery/gallery-title-slug/` This feed will contain an Item for each Photo in the Gallery All that is left is to add an autodiscovery feed to your pages in &lt;head&gt;. An RSS agent like CoolIris can then parse your gallery and provide a slick view of your photos. e.g Add something like this to gallery_detail.html: `<link rel="alternate" href="/feeds/gallery/{{ object.title_slug }}/" type="application/rss+xml" title="Photologue Gallery - {{ object.title }}" id="gallery_feed" /> `

  • feed
  • rss
  • photologue
  • syndication
Read More

More readable Enumeration class for Django choices

We currently use two-level tuples to specify choices of a field in models or forms. But, because it has only (value, verbose name) pair, the readability is bad whenever we indicate a specific choice value in our Python codes. So I made a small class that does "magic" for this: A Named Enumeration. Instead of `myobj.status == 0`, use `myobj.status == STATUS.UNREVIEWED`, for example.

  • choices
  • model
  • orm
  • enumeration
  • enum
Read More

StateField

Based on [CountryField](http://www.djangosnippets.org/snippets/494/).

  • model
  • field
  • state
  • statefield
Read More

Template tags to integrate with modconcat

Assumes mod_concat is installed: http://code.google.com/p/modconcat/ Django template tags that combine blocks of CSS and Javascript into modconcat friendly URLs. Takes this: `{% cssconcat "{{ MEDIA_URL }}css/" %} <link href="{{ MEDIA_URL }}css/a.css" rel="stylesheet" type="text/css" /> <link href="{{ MEDIA_URL }}css/b.css" rel="stylesheet" type="text/css" /> <link href="{{ MEDIA_URL }}css/c.css" rel="stylesheet" type="text/css" /> {% endcssconcat %}` And outputs this: `<link href="/site_media/??a.css,b.css,c.css" rel="stylesheet" type="text/css" /> ` Similarly for javascript: `{% jsconcat "{{ MEDIA_URL }}js/" %} <script src="{{ MEDIA_URL }}js/a.js" type="text/javascript"></script> <script src="{{ MEDIA_URL }}js/b.js" type="text/javascript"></script> <script src="{{ MEDIA_URL }}js/c.js" type="text/javascript"></script> {% endjsconcat %}` becomes: `<script src="/site_media/??a.js,b.js,c.js" type="text/javascript"></script> `

  • javascript
  • css
  • modconcat
  • mod_concat
Read More

Show users' full names for foreign keys in admin

This is a ModelAdmin base class you can use to make foreign key references to User a bit nicer in admin. In addition to showing a user's username, it also shows their full name too (if they have one and it differs from the username). **2009-08-14**: updated to handle many to many fields and easily configure whether to always show the username (if it differs from full name)

  • user
  • modeladmin
  • get_full_name
Read More

Template class to test custom tag libraries

TestableTemplate behaves just like django.template.Template, but you can give it a list of template.Libraries to load before parsing the template. This is equivalent to adding a bunch of {% load %} tags to the beginning of your template string, but you can use custom tag libraries which do not belong to Django applications' templatetags packages. This is occasionally useful in testing.

  • templates
Read More

2956 snippets posted so far.