Login

All snippets written in Python

Snippet List

Paginator template tag using ObjectPaginator

This template inclusion tag provide a way to have multiple pagination blocks in the same page. Aditionnal parameters in "request.GET" are also automaticaly keeped in pagination links. Usage : **{% show_pagination users_paginator request "page_members" %}** The expected result : **[1] 2 3 … 14** Or : **1 … 5 6 [7] 8 9 … 14**

  • paginator
  • objectpaginator
Read More

a template tag to invoke a method on an object with a variable

The django templating language is quite nice, and specifically limited to guide people in to making their business logic in the view, not in the template itself. Sometimes it can be difficult to do certain things in the template even though it seems like the most appropriate place to do this. I have an application that is heavily influenced by the user that is logged in in several ways. For example let us say I have a list of forums. Each forum has several discussions full of posts. This system of forums and discussions has permissions such that some users can not see some entities. Now, I can produce in the view the query set of what forums a user is allowed to see, and I want this list to display the latest post in each of those forums, but I have to restrict that to the posts that they can see. Easy enough, I have a method on the Forum object that takes a user object and does the appropriate filter to return the latest post that the user can see. The trick is the template is passed the query set of forums, and then iterates through them using the template language. How can it invoke the method on the forum for the latest post that needs the 'user' variable from the template context? The template language lets me say 'forum.latest_post' but I need to pass in 'user'. 'forum.latest_post(user)' does not work because the template language does not allow it. This tag lets me specify an object, the method on that object to call, and the variable to pass to that method. It is not the prettiest thing but with this add on you can do: `{% method_arg forum latest_post user as post %}`

  • tags
Read More

Crop and scale image to a given size

Prerequisites: [Python Imaging Library](http://www.pythonware.com/products/pil/) This function scales a given image (provided as binary data in any format the PIL supports) to a specified size. If the force parameter is True, the function makes sure that the resulting image is exactly the specified size, cropping and scaling it as necessary (but never distorting it) to make sure the whole image area is filled out. If force is False, it simply uses the thumbnail function provided by the PIL, which preserves the image aspect ratio and does not increase the image dimensions beyond those of the original file, so you may not get an image that has the exact dimensions you specified. The result image is returned as JPEG data.

  • thumbnail
  • crop
  • scale
Read More
Author: rpw
  • 3
  • 22

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

2955 snippets posted so far.