Login

Top-rated snippets

Snippet List

Jinja2 Django integration

Integration of django and Jinja2. Just import render_to_response from this file and you are ready. This file automatically adds template search path from yout TEMPLATE_DIRS and from each installed application. You can also use several variables in your settings.py to add filters, tests and global functions to the default context. This can be done by using JINJA_GLOBALS, JINJA_FILTERS and JINJA_TEST e.g. `JINJA_FILTERS = ( 'myapp.filters.myfilter', 'myapp.filters.myfilter2', )`

  • templates
  • jinja
  • jinja2
Read More

CheckedField

Here's a snippet to pair an arbitrary form Field type (the "target field") with a checkbox. If the checkbox is not selected, the cleaned_data value for the field will be None. If the checkbox is selected, it will return the target field's cleaned value.

  • checkbox
  • forms
  • combining
Read More

render_with decorator

Automatically render your view using render_to_response with the given template name and context, using RequestContext (if you don't know what this is you probably want to be using it). For example: @render_with('books/ledger/index.html') def ledger_index(request): return { 'accounts': ledger.Account.objects.order_by('number'), }

  • render_to_response
  • requestcontext
  • decorator
  • rendering
Read More

twitter_status

Ah simple_tag for get last update of twitter. How twitter limit the fetch rss so, add cache tag for each 30min ( 60*30) update the twitter. if.

  • template
  • simple_tag
  • twitter
Read More

auto generate admin.py

When you switch you django project from 0.9.6 to 1.0, you can use this script to generate admin.py automatically. You need copy cvt.py to the parent directory of your project(where your project lies) and type "python cvt.py <project> <app>". The admin.py will generated in the <project>/<app>(where it should be!). Enjoy this small work!

  • django
  • python
  • admin-interface
Read More

simple guestbook

a simple guestbook. the guestbook-area and entries can (and should) be styled by CSS. The template extends a "base.html", which should contain a "content" block. The Model is very simple without moderation, admin-comment or any other advanced features, but its easy to extend. i.e. add a Field "active=models.BooleanField()" and add "exclude=['active']" to the forms.EntryForm.Meta class for moderated Entries. Now you can switch the entries on/off in the admin-interface by setting active=True/False this snippet is public domain, use for everything you want. UPDATE: added basic SPAM protection (a do_not_fill field), but you might want to try a captcha-form/Field like snippet 812

  • guestbook
  • guest
  • book
Read More

Upload, Progressbar with sessions

This script is an adaptation from http://www.djangosnippets.org/snippets/678/ . Here, it doesnt use the cache middleware but relies on sessions. The script set a session cookie to identify the upload and track it to make it available for a progress bar like this one : http://www.djangosnippets.org/snippets/679/ . Note the progress bar cannot work with development server as it is single-threaded. Tested with apache/mod_python and mod_wsgi. any comments appreciated ;)

  • upload
  • handler
  • sessions
  • progressbar
Read More

Cacheable resources

This snippet provides a template tag that automatically replaces references to any resource you want cached forever with a version of the file that is based on the MD5 sum. For an image, you would use something like: {% load utils %} <img src="{% cacheable "/media/images/logo.png" %}"/> To install it, put a setting in your settings.py file called "DOCUMENT_ROOT", put the python code into a templatetag-friendly file (e.g. app/templatetags/utils.py), load that template tag, then use either a string literal, as above, or a variable name to refer to your resource: <img src="{% cacheable my_media_file %}"/> The cacheable resource will be used when `DEBUG = False`, but in DEBUG mode, the path you give it will be passed back untouched (so you don't have a proliferation of cacheable files as you develop). Django will need write access to the directory you've specified as "DOCUMENT_ROOT" (so it can copy the original file into a forever-cacheable version). You'll also need to set up your webserver to serve files called "MYMD5SUMNAME.cache.(js|css|png|gif|jpg) with an expires header that is far into the future. The goal here is to create a version of your file that will never have to be downloaded again. If you ever change the original file, the MD5 sum will change and the changed file's cacheable name will reflect that. Besides simply changing the name of resources, if the file is a JavaScript or CSS file, and you've specified `MINIFY = True`, the file will be minified using YUI compressor.

  • md5
  • cacheable
  • cacheing
Read More

SiteRedirectMiddleware

Redirects to the default site (from Django's Sites contrib app), specified by the `SITE_ID` setting. That's for example useful if you configured your webserver to handle multiple domains with the same virtual host and want to make sure every requests is then redirected to the right domain.

  • middleware
  • sites
Read More

debug info middleware

Hi, I have developed a middleware that enables to view debugging information in production for a single user filtered by useragent or ip. The debug info is appended to the html code as a remark and can be viewed with a view source operation from the browser. Take a look at http://code.google.com/p/debugview/ Enjoy

  • middleware
  • django
  • debug
  • information
Read More

Serve static media and indexes from app directories [Python2.5, Development only]

This view serves static media and directory indexes for a django application. It should only be used in development, media should be provided directly by a web server in production. This view assumes a django application stores its media in app/media (which is very common) and the file is referred to in the templates by the last part of a django app path. e.g. As in django.contrib.admin -> 'admin'. First we check if the media is a request in an application directory; if so we attempt to serve it from there. Then we attempt to provide the document from the document_root parameter (if provided). To use this view you should add something like the following to urls.py: ` if settings.DEBUG: urlpatterns += (r'^media/(?P<path>.*)$', 'site.media.serve_apps', {'document_root' : settings.MEDIA_ROOT}) ` You can then have the admin media files served by setting ADMIN_MEDIA_PREFIX = '/media/admin/'

  • media
  • static-media
  • directory-index
Read More

DualPasswordForm

DualPasswordForm is a simple form that contains two password fields and validation to ensure that the two passwords match. A minimum password length of 7 characters is imposed, but feel free to change that.

  • form
  • password
  • match
  • dual
  • dualpasswordform
Read More

3110 snippets posted so far.