Login

All snippets

Snippet List

template + cache = crazy delicious

A couple of utility `Node` subclasses that will automatically cache thier contents. Use `CachedNode` for template tags that output content into the template: class SomeNode(CachedNode): def get_cache_key(self, context): return "some-cache-key" def get_content(self, context): return expensive_operation() Use `CachedContextUpdatingNode` for tags that update the context: class AnotherNode(CachedContextUpdatingNode): # Only cache for 60 seconds cache_timeout = 60 def get_cache_key(self, context); return "some-other-cache-key" def get_content(self, context): return {"key" : expensive_operation()}

  • tag
  • templatetag
  • cache
Read More

Wizard class

Wizard class - subclass this, supply a done() method and then you can use `MyWizard( [list, of, newforms, classes])` as a view function. see [#3218](http://code.djangoproject.com/ticket/3218)

  • newforms
  • form
Read More

Script for getting Google Page Rank of page

I was looking for such script written in python and found google checksum algorithm at http://pagerank.gamesaga.net/ And just added complete functionality to it. Usage: script.py PR http://somepage.com/page.html It also has function to retrieve Yandex TYC.

  • google
  • pagerank
  • yandex
Read More

Ignore HTTP Accept-Language headers

A little tiny middleware that, when used in multilingual sites, will make Django I18N ignore any `Accept-Language` headers in the request, thus ensuring that every first-time visitor (with no explicit language preference set via session or cookie) will see the site in the language specified by `settings.LANGUAGE_CODE`. (Please note that I think that overriding user preferences is generally a bad practice, but I had my reasons to use it :) )

  • middleware
  • i18n
  • l10n
  • locale
Read More

User or Group entry field & widget

This is a helper field & widget that lets you ask for a 'user or group'. It presents a select box for whether you wish to enter a user or a group, and then a text field for the name of the user or group you wish to have. It will validate that the username or group name selected exists. You would use this like any other field: ` class AddForumCollectionCreatePermForm(forms.Form): user_or_group = UserOrGroupField(label = "User or Group", help_text = "The user or group to grant " "forum collection create priveleges to.") `

  • newforms
Read More

encode_mailto

This is a django filter which will create an html mailto link when you use the syntax: {{ "[email protected]"|encode_mailto:"Name" }} Results in: <a href="mailto:[email protected]">Name</a> Except everything is encoded as html digits. The encoding is a string of html hex and decimal entities generated randomly. If you simply want a string encoding use: {{ "name"|encode_string }} This was inspired by John Gruber's [Markdown](http://daringfireball.net/projects/markdown/syntax#autolink) project

  • filter
  • email
  • encode
Read More

Set Template Tag

Add a value to the context for easy access and for access from include templates. NOTE: This tag is composed from Django ticket: http://code.djangoproject.com/ticket/1322

  • tag
Read More
Author: Xin
  • 2
  • 3

WTForm (What The Form)

WTForm is an extension to the django newforms library allowing the developer, in a very flexible way, to layout the form fields using fieldsets and columns WTForm was built with the well-documented [YUI Grid CSS](http://developer.yahoo.com/yui/grids/) in mind when rendering the columns and fields. This should make it easy to implement WTForm in your own applications. Here is an image of an [example form rendered with WTForm](http://www.gmta.info/files/wtform.png).

  • newforms
  • html
  • css
  • fieldset
  • form
  • yui
  • rendering
  • grid
  • columns
  • layout
Read More
Author: chrj
  • 23
  • 101

Single Django App behind multiple Apache Proxies

** Django Proxied Behind Apache Path** Middleware and Context Processor for django applications behind a proxy like Apache's mod_proxy. Setting an HTTP header with the base path of the django application in the Apache mod_proxy configuration, we can pull that variable out of the request and adjust our template URIs and redirects. This allows us to serve the same Django application out from behind multiple VirtualHosts at different URL paths. Example use in templates: <a href="{{ base_path }}/login/">Login</a> Example Apache configuration: <LocationMatch ^/path/to/django/app/.*> RequestHeader set X-Base-Path "/path/to/django/app" </LocationMatch> RewriteRule ^/path/to/django/app/(.*) http://django-host:8080/$1 [P,L] In settings.py: VHOST_HEADER = "HTTP_X_BASE_PATH"

  • apache
  • virtualhost
  • mod_proxy
  • path
Read More

youtubize template tag

This snippet is based on djangos urlize filter. It converts http:// links to youtube into youtube-embed statements, so that one can provide a simple link to a youtube video and this filter will embed it. I used it for a fun blog app.

  • youtube
  • embed
  • urlize
Read More

split filter

**Usage** (*in template*): <img src="{{ MEDIA_URL }}2007/images/{% filter split:","|random %}theimage1.jpg,something2.jpg,thirdisthecharm.jpg{% endfilter %}" /> I decided to make it simple, because one template creator wanted to add random images to different places of templates. Creating something huge, like external image filename parsing was not necessary in this case.

  • filter
  • split
Read More

Timedelta template tag

This is tag similar to *timesince* and *timeuntil*, which work great until you starting giving timesince dates in the future or timeuntil dates in the past. Timedelta tag will humanize output correctly for both future and past dates using *now* as a default reference date (or a specified reference date as argument) now = Apr 27 2007 futuredate = May 5 2007 pastdate = Jan 5 2007 {{ futuredate|timedelta }} will output "in 1 week, 1 day" {{ pastdate|timedelta }} will output "3 months, 2 weeks ago"

  • template
  • date
  • time
  • humanize
Read More

newforms field callback helper

**Now redundant any anything >0.96**, as `form_for_*` methods now have a `fields` attribute `formfield_callback`s are a bit difficult to use, here's a helper method to create a callback function to use with the `form_for_instance` and `form_for_model` methods. Example usage: person_callback = new_callback(exclude=['password', 'can_add_staff', 'is_staff']) def form_for_person(person): return form_for_instance(person, formfield_callback=person_callback)

  • newforms
Read More

3110 snippets posted so far.