Login

All snippets

Snippet List

Strip trailing .html extensions from URLs so that existing bookmarks work for a legacy site ported to Django

1) You've ported an existing web site to Django. 2) The new site URLs don't have html (or htm) extensions. 3) The old site had URLs with html extensions. 4) You want existing bookmarks to work. Use this middleware to removes trailing .htm and .html extensions from incoming URLs (GETs only) so that your new site honors existing bookmarks. Locate it in settings.MIDDLEWARE_CLASSES near CommonMiddleware because it has similar middleware stack location requirements. If an incoming URL has an html extension, ZapDotHtmlMiddleware strips it out and redirects.

  • middleware
  • url_rewrite
  • strip_dot_html_from_urls
Read More

Adding a field to a newform in __init__

When adding fields in the __init__ of a newform, and you don't want the fields to be added *after* the class-attribute fields, this is a possibility... This is a bad example as the email_from could just as well have been defined as a class variable!

  • newforms
  • field
  • init
Read More

Custom SQL via subquery

You can use custom SQL statements with the existing database API by creating a subquery specified via the `tables` parameter of `extra`. (This has only been tested with MySQL and may not work with all database back-ends.)

  • group-by
Read More

custom css classes for newforms

This isn't really any trick, its just that I didn't find any documentation of this or any references in searching. There are a few changes proposed for css classes which might make this redundant, but for now this works! If you want to add attributes for any fields, just include them in the widget constructor. They get written out in key value pairs when the input field is being created. e.g. in the example above, this will come out like: ` '<input type="text" name="start_date" id="id_start_date" class="vDateField required" size="10"/>' `

  • newforms
  • css
Read More

SQLite database vacuum script

This script can be run periodically (e.g. as a nightly cronjob) to keep a SQLite database with high churn from growing unnecessarily large.

  • db
  • utility
  • sqlite
Read More
Author: pbx
  • 4
  • 8

jquery autocomplete widget

newforms widget for autocompleting text fields using jquery autocomplete plugin: http://jquery.bassistance.de/autocomplete/ to be implemented: - store the pk value into an hidden field - handling ChoiceFields and many others massimo dot scamarcia at gmail.com

  • ajax
  • newforms
  • javascript
  • forms
  • jquery
  • widgets
Read More
Author: skam
  • 25
  • 145

Sphinx Search ORM

An ORM model for the Sphinx full-text search engine. See http://www.sphinxsearch.com/ for more information. It currently supports the following: class MyModel(models.Model): search = SphinxSearch() MyModel.search.query('query') MyModel.search.query('query').order_by('@weight', '@id', 'my_attribute') MyModel.search.query('query').filter(my_attribute=5) MyModel.search.query('query').filter(my_other_attribute=[5, 3,4]) MyModel.search.query('query').exclude(my_attribute=5)[0:10] MyModel.search.query('query').count() SphinxSearch().query('hello').on_index('model_myapp model_myotherapp') Returns an ordered list of the objects in your database. -- Update: New Methods: * count() * index_on(<str index>) * extra(<see django>) * all() (does nothing) * select_related(<see django>) * group_by(<str attribute>, <const function>[, <str sort>) * weights(<list weights>)

  • search
  • sphinx
  • full-text
Read More

several_random template filter

Allows the selection of one or more items from a list. The built-int `random` filter only allows you to select a single at a time, and repeated use can return the same item many times. **Example:** `{% for random_item in item_list|several_random:3 %} ... {% endfor%}` **Note:** If you're running this on an uncached QuerySet, it can result in many database queries... a future improvement might check to see if the passed object is a QuerySet and act accordingly.

  • filter
Read More

The chunkmaker

This is a general-purpose utility function, but since it uses lazy sequences via itertools, so it should be suitable for use with Querysets.

  • python
  • utility
  • sequence
  • itertools
Read More
Author: pbx
  • 4
  • 3

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

3110 snippets posted so far.